SharePoint Online: agregar un archivo adjunto al elemento de la lista usando PowerShell
Requisito: Agregar un archivo adjunto al elemento de la lista de SharePoint Online.
SharePoint Online: ¿Cómo agregar un archivo adjunto a un elemento de la lista?
Los archivos adjuntos son una excelente manera de mantener documentos y archivos relacionados junto con los elementos de su lista en SharePoint Online. En esta publicación, lo guiaremos a través del proceso de agregar un archivo adjunto a un elemento de la lista de SharePoint Online.
Para agregar un archivo adjunto a un elemento de la lista en SharePoint Online, siga estos pasos:
- Navegue a la lista de SharePoint Online >> Seleccione el elemento de la lista al que desea agregar archivos adjuntos
Editar elemento de la lista >> Desplácese hacia abajo y haga clic en el enlace "Agregar archivos adjuntos" en el panel Editar.
- Esto abre la ventana de diálogo de exploración. Seleccione los archivos adjuntos para agregar.
- Haga clic en Guardar para completar la adición de un archivo adjunto al elemento de la lista.
PowerShell para agregar un archivo adjunto al elemento de la lista:
PowerShell puede facilitar el proceso si desea automatizar el proceso de adjuntar archivos a una lista. Vamos a explicarle cómo adjuntar un archivo a un elemento de lista específico en su lista de SharePoint Online usando PowerShell. Esta es una forma práctica de adjuntar varios archivos a elementos de lista sin abrir el editor de elementos.
Aquí está el script de PowerShell para agregar archivos adjuntos al elemento de la lista:
#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
PnP PowerShell para agregar un archivo adjunto al elemento de la lista
Agreguemos un archivo adjunto al elemento de la lista de SharePoint Online con PnP PowerShell.
#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()
¿Qué tal agregar varios archivos adjuntos al elemento de la lista? Agreguemos todos los archivos de una carpeta determinada a 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
También puede adjuntar un archivo a un elemento de lista en SharePoint Online usando el cmdlet PnP de PowerShell: 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
Mi otra publicación sobre la descarga de archivos adjuntos de la lista de SharePoint Online: ¿Cómo descargar archivos adjuntos de la lista de SharePoint Online usando PowerShell?
