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

[玩转系统] SharePoint Online:使用 PowerShell 从列表项中删除附件

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

SharePoint Online:使用 PowerShell 从列表项中删除附件


要求:从 SharePoint Online 列表项中删除附件。

如何从 SharePoint Online 中的列表项中删除附件?

您是否正在寻找从列表项中删除附件的方法?这篇博文将向您展示如何从 SharePoint 列表项中删除附件。我们还将分享用于从 SharePoint Online 列表项中删除附件的 PowerShell 脚本。

要从 SharePoint 列表项中删除附件,请执行以下操作:

  1. 导航到 SharePoint Online 列表 >> 选择列表项 >> 从工具窗格中单击“编辑”。
  2. 在“编辑”面板中,向下滚动到底部,然后单击任何附件旁边的小 X 图标,将其从列表项中删除。

    [玩转系统] SharePoint Online:使用 PowerShell 从列表项中删除附件

  3. 单击“保存”以提交您的更改。

该附件现在将从 SharePoint Online 列表项中删除。您还可以使用 PowerShell 删除附件,以自动执行多个列表项的过程。

使用 PowerShell 删除列表项中的所有附件

让我们使用 PowerShell 删除 SharePoint Online 列表项中的所有附件。


#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"

#Set Parameters
$SiteURL= "https://crescent.sharepoint.com"
$ListName="Projects"
$ItemID="4"

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()
 
    ForEach($AttachmentFile in $AttachmentFiles)
    {
        $AttachmentFile.DeleteObject()
        $Ctx.ExecuteQuery()
 
        write-host  -f Green "Attachment File '$AttachmentFile' Removed from List Item!"
    }
}
Catch {
    write-host -f Red "Error Removing Attachment From List!" $_.Exception.Message
}

PowerShell 从 SharePoint Online 列表项中删除附件:

让我们将脚本包装在可重用函数中,以从给定列表项中删除特定附件:


#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 Remove-AttachmentFromListItem()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $ItemID,
        [Parameter(Mandatory=$false)] [string] $AttachmentFileName
    )    
    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
        $AttachmentFile = $AttachmentFiles | where { ($_.FileName -eq $AttachmentFileName) }
        If($AttachmentFile -ne $Null)
        {
            $AttachmentFile.DeleteObject()
            $Ctx.ExecuteQuery()

            write-host  -f Green "Attachment File '$AttachmentFileName' Removed from List Item!" 
        }
        else
        {
            write-host -f Yellow "Attachment File '$AttachmentFileName' Doesn't Exist!"
        }
    }
    Catch {
        write-host -f Red "Error Removing Attachment From List!" $_.Exception.Message
    }
}

#Set Parameters
$SiteURL= "https://crescent.sharepoint.com"
$ListName="Projects"
$ItemID="64"
$AttachmentFileName="ProjectProposal.docx"

#Call the function to copy list items
Remove-AttachmentFromListItem -SiteURL $SiteURL -ListName $ListName -ItemID $ItemID -AttachmentFileName $AttachmentFileName

PnP PowerShell 从 SharePoint Online 列表项删除附件

以下是删除列表项附件的 PnP PowerShell 方法:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName ="Projects"
$ListItemTitle = "SharePoint 2016 Upgrade"
$AttachmentFileName= "Project Proposal.docx"

#Connect PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Context
$Context = Get-PnPContext
 
#Get the List Item from "Title" field value
$Query = "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>$ListItemTitle</Value></Eq></Where></Query></View>"
$ListItem = Get-PnPListItem -List $ListName -Query $Query -PageSize 1
 
#Get List attachment by its file name
$Attachment = $ListItem.AttachmentFiles.GetByFileName($AttachmentFileName)

#Delete the attachment
$Attachment.DeleteObject() 
$Context.ExecuteQuery()

您还可以使用 PnP PowerShell cmdlet Remove-PnPListItemAttachment


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Retail"
$ListName = "Projects"
$ListItemID = 13
$AttachmentFile = "MonthlyRpt.csv"

#Connect to SharePoint Online Site
Connect-PnPOnline -Url $SiteURL -Interactive

#Remove Attachment file from a list item
Remove-PnPListItemAttachment -List $ListName -Identity $ListItemID -FileName $AttachmentFile -Force -Recycle

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

取消回复欢迎 发表评论:

关灯