SharePoint Online: PowerShell kullanarak Liste Öğesine Ek Ekleme
Gereksinim: SharePoint Çevrimiçi Liste Öğesine Ek Ekle.
SharePoint Online: Liste Öğesine Nasıl Ek Eklenir?
Ekler, ilgili belgeleri ve dosyaları SharePoint Online'daki liste öğelerinizle birlikte tutmanın harika bir yoludur. Bu gönderide, SharePoint Online liste öğesine ek ekleme sürecinde size yol göstereceğiz.
SharePoint Online'da bir liste öğesine ek eklemek için şu adımları izleyin:
- SharePoint Online listesine gidin >> Ek eklemek istediğiniz Liste Öğesini seçin
Liste Öğesini Düzenle >> Aşağı kaydırın ve Düzenleme bölmesindeki “Ek Ekle” bağlantısını tıklayın.
- Bu, göz atma iletişim penceresini açar. Eklenecek ekleri seçin.
- Liste öğesine ek eklemeyi tamamlamak için Kaydet'e tıklayın.
Liste Öğesine Ek Eklemek için PowerShell:
Bir listeye dosya ekleme işlemini otomatikleştirmek istiyorsanız PowerShell işlemi kolaylaştırabilir. PowerShell'i kullanarak SharePoint Online listenizdeki belirli bir liste öğesine nasıl dosya ekleyeceğinizi size açıklayalım. Bu, öğe düzenleyiciyi açmadan liste öğelerine birden fazla dosya eklemenin kullanışlı bir yoludur.
Liste öğesine ekler eklemek için PowerShell betiği aşağıda verilmiştir:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Function Add-AttachmentToListItem()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ListName,
[Parameter(Mandatory=$true)] [string] $ItemID,
[Parameter(Mandatory=$false)] [string] $AttachmentPath
)
Try {
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred
#Get the List & List Item
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$ListItem = $List.GetItemByID($ItemID)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()
#Get All existing attachments
$AttachmentFiles = $ListItem.AttachmentFiles
$Ctx.Load($AttachmentFiles)
$Ctx.ExecuteQuery()
#Check if attachment file name exists already
$FileName = Split-Path $AttachmentPath -Leaf
$AttachmentFile = $AttachmentFiles | where { ($_.FileName -eq $FileName) }
If($AttachmentFile -eq $Null)
{
#Get the Attachment file from local disk
[Byte[]]$Bytes = [System.IO.File]::ReadAllBytes($AttachmentPath)
$ContentStream = New-Object -TypeName System.IO.MemoryStream -ArgumentList @(,$Bytes)
#Create Attachment object
$AttachmentCreation = New-Object Microsoft.SharePoint.Client.AttachmentCreationInformation
$AttachmentCreation.ContentStream = $ContentStream
$AttachmentCreation.FileName = $FileName
[Void]$ListItem.AttachmentFiles.Add($AttachmentCreation)
$Ctx.ExecuteQuery()
write-host -f Green "Attachment Added to List Item!"
}
else
{
write-host -f Yellow "Attachment File Name Exists already!"
}
}
Catch {
write-host -f Red "Error Adding Attachment to List!" $_.Exception.Message
}
}
#Set Parameters
$SiteURL= "https://crescent.sharepoint.com"
$ListName="Projects"
$ItemID="5"
$AttachmentPath="C:\Projects\Proposal.docx"
#Call the function to copy list items
Add-AttachmentToListItem -SiteURL $SiteURL -ListName $ListName -ItemID $ItemID -AttachmentPath $AttachmentPath
Liste Öğesine Ek Eklemek için PnP PowerShell
PnP PowerShell ile SharePoint Online liste öğesine eklenti ekleyelim.
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$ListName = "Projects"
$ItemID ="2"
$FilePath ="C:\Docs\UserInfo.csv"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the List Item
$Item = Get-PnPListItem -List $ListName -Id $ItemID
#Get the File and Add to List Item Attachment
$FileStream = New-Object IO.FileStream($FilePath,[System.IO.FileMode]::Open)
$AttachmentInfo = New-Object -TypeName Microsoft.SharePoint.Client.AttachmentCreationInformation
$AttachmentInfo.FileName = Split-Path $FilePath -Leaf
$AttachmentInfo.ContentStream = $FileStream
$AttchedFile = $Item.AttachmentFiles.Add($AttachmentInfo)
Invoke-PnPQuery
$FileStream.Close()
Liste öğesine birden fazla ek eklemeye ne dersiniz? Belirli bir klasördeki tüm dosyaları SharePoint Online'a ekleyelim.
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/pmo"
$ListName = "Projects"
$ItemID ="2"
$FolderPath = "C:\Temp\Docs"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the List Item
$ListItem = Get-PnPListItem -List $ListName -Id $ItemID
#Function to Add all files as a attachment to list item
Function Add-AttachmentsFromFolder($ListItem, $FolderPath)
{
#Get All existing attachments from list item
$AttachmentFiles = Get-PnPProperty -ClientObject $ListItem -Property "AttachmentFiles"
#Get the File and Add to List Item Attachment
ForEach ($File in (Get-ChildItem $FolderPath -File))
{
$AttachmentFile = $AttachmentFiles | Where { ($_.FileName -eq $File.Name) }
If($AttachmentFile -eq $Null)
{
$AttachmentInfo = New-Object -TypeName Microsoft.SharePoint.Client.AttachmentCreationInformation
$AttachmentInfo.FileName = $File.Name
$AttachmentInfo.ContentStream = $File.OpenRead()
$AttchedFile = $ListItem.AttachmentFiles.Add($AttachmentInfo)
Invoke-PnPQuery
Write-host -f Green "Added Attachment File:"$File.FullName
}
Else
{
write-host -f Yellow "Attachment '$($File.Name)' Exists already!"
}
}
}
#Call the function to add all attachments from folder
Add-AttachmentsFromFolder $ListItem $FolderPath
Ayrıca PnP PowerShell cmdlet'ini kullanarak SharePoint Online'daki bir Liste Öğesine dosya ekleyebilirsiniz: Add-PnPListItemAttachment
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Retail"
$ListName = "Projects"
$ListItemID = 13
$AttachmentFile = "C:\Temp\MonthlyRpt.csv"
#Connect to SharePoint Online Site
Connect-PnPOnline -Url $SiteURL -Interactive
#Attach file to list item
Add-PnPListItemAttachment -List $ListName -Identity $ListItemID -Path $AttachmentFile
SharePoint Online listesinden ek indirme hakkındaki diğer yazım: PowerShell Kullanarak SharePoint Çevrimiçi Listesinden Ekler Nasıl İndirilir?
