当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 将附件添加到列表项

作者:精品下载站 日期:2024-12-14 21:06:57 浏览:14 分类:玩电脑

SharePoint Online:使用 PowerShell 将附件添加到列表项


要求: 将附件添加到 SharePoint Online 列表项。

SharePoint Online:如何向列表项添加附件?

附件是将相关文档和文件与 SharePoint Online 中的列表项保存在一起的好方法。在这篇文章中,我们将引导您完成向 SharePoint Online 列表项添加附件的过程。

要将附件添加到 SharePoint Online 中的列表项,请执行以下步骤:

  1. 导航到 SharePoint Online 列表 >> 选择要添加附件的列表项
  2. 编辑列表项 >> 向下滚动并单击编辑窗格中的“添加附件”链接。

    [玩转系统] SharePoint Online:使用 PowerShell 将附件添加到列表项

  3. 这将打开浏览对话框窗口。选择要添加的附件。
  4. 单击“保存”即可完成向列表项添加附件。
SharePoint Online 列表项附件大小限制:SharePoint Online 列表附件的文件大小限制为 250 MB!

PowerShell 将附件添加到列表项:

如果您想自动执行将文件附加到列表的过程,PowerShell 可以使该过程变得简单。让我们引导您了解如何使用 PowerShell 将文件附加到 SharePoint Online 列表中的特定列表项。这是一种无需打开项目编辑器即可将多个文件附加到列表项目的便捷方法。

以下是将附件添加到列表项的 PowerShell 脚本:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\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

PnP PowerShell 将附件添加到列表项

让我们使用 PnP PowerShell 将附件添加到 SharePoint Online 列表项。


#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()

向列表项添加多个附件怎么样?让我们将给定文件夹中的所有文件添加到 SharePoint Online。


#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

您还可以使用 PnP PowerShell cmdlet 将文件附加到 SharePoint Online 中的列表项: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 列表下载附件的另一篇文章:如何使用 PowerShell 从 SharePoint Online 列表下载附件?

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯