Búsqueda de sitios web

SharePoint Online: eliminar archivos adjuntos del elemento de la lista mediante PowerShell


Requisito: Eliminar el archivo adjunto del elemento de la lista de SharePoint Online.

¿Cómo eliminar archivos adjuntos de elementos de lista en SharePoint Online?

¿Está buscando una manera de eliminar archivos adjuntos de los elementos de la lista? Esta publicación de blog le mostrará cómo eliminar un archivo adjunto de un elemento de la lista de SharePoint. También compartiremos los scripts de PowerShell para eliminar archivos adjuntos de los elementos de la lista de SharePoint Online.

Para eliminar un archivo adjunto de los elementos de la lista de SharePoint, haga lo siguiente:

  1. Navegue a la lista de SharePoint Online >> Seleccione el elemento de la lista >> Haga clic en Editar desde el panel de herramientas.
  2. En el panel Editar, desplácese hacia abajo y haga clic en el pequeño ícono X al lado de cualquier archivo adjunto para eliminarlo del elemento de la lista.

  3. Haga clic en Guardar para confirmar sus cambios.

El archivo adjunto ahora se eliminará del elemento de lista de SharePoint Online. También puede eliminar archivos adjuntos usando PowerShell para automatizar el proceso de varios elementos de la lista.

Eliminar todos los archivos adjuntos de un elemento de la lista usando PowerShell

Eliminemos todos los archivos adjuntos de un elemento de la lista de SharePoint Online usando PowerShell.


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

#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 para eliminar archivos adjuntos del elemento de lista de SharePoint Online:

Envuelvamos el script dentro de una función reutilizable para eliminar un archivo adjunto específico de un elemento de lista determinado:


#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 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 para eliminar archivos adjuntos del elemento de lista de SharePoint Online

Esta es la forma PnP PowerShell de eliminar un archivo adjunto de un elemento de lista:


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

También puede utilizar el cmdlet PnP PowerShell 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

Artículos relacionados