5

I am trying to apply a bunch of metadata to a bunch of files in SPO. Using Find-PnPFile I can find the files in the subfolders. What I cannot figure out is how to pass the results of the Find-PnPFile back to Set-PnPListItem to set the values of the documents. I don't have the Identity of the file. What would be AMAZING is if there is a way to do this in a batch so I don't have to update every item one after another. It just feels like there should be a way to pass an array of document IDs and the value I want to set.

$updatefiles = Find-PnPFile -Folder "Documents/$($_.Name)" -Match "*"
foreach ($file in $updatefiles){
  Set-PnPListItem -List "Documents" -Values @"MMSField"="foo|bar|$($folder.Name)"} -Identity <<HELP>>
}

3 Answers 3

0

try

$childItems = Get-ChildItem -FolderSiteRelativeUrl $folderURL;
foreach($childItems in $childItems)
{
    Set-PnPListItem -List "Documents" -Values @"MMSField"="foo|bar|$($folder.Name)"} -Identity
}
0

Think of document library metadata as a list. This is what I use:

Connect-PnPOnline -url $siteURL -UseWebLogin
$DocLibName = "<NameOfDocLib>"
$Allfiles = Get-PnPListItem -List $DocLibName
foreach( $file in $Allfiles)
{
    if($file.FieldValues.<Existing_x0020_Column> -eq "<Some Value>"){
        Set-PnPListItem -List $DocLibName -Identity $file.Id -Values @{"Column_x0020_Key" = "New Value"; "Second_x0020_Column_x0020_Key" = "New Value"}
    }
}

Connect-PnPOnline: https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/connect-pnponline?view=sharepoint-ps

Get-PnPListItem: https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/get-pnplistitem?view=sharepoint-ps

Set-PnPListItem: https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/set-pnplistitem?view=sharepoint-ps

2
  • As the question states, I need to use Find-PnPFiles I cannot enumerate every file in the library. Commented Jun 22, 2018 at 14:19
  • Sorry I missed the fact you cannot enumerate every file. Still you need to access the list item, and not the file. PnP handles them as two separate things. You cannot get the file, then use that object to update the list. The link I provided for Get-PnPListItem outlines how to get by ID.
    – Steige
    Commented Jun 25, 2018 at 14:22
0

To set metadata of an existing file, use:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$ListName = "Branding"
$SiteRelativeURL = "/Branding/Technical Design.docx"
$CreatedBy= "[email protected]"
$Title = "New Design Document V2.docx"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin

#Get the File from SharePoint
$File = Get-PnPFile -Url $SiteRelativeURL -AsListItem

#Update document properties
Set-PnPListItem -List $ListName -Identity $File.Id -Values @{"Title" = $Title; "Author" = $CreatedBy}

Source: https://www.sharepointdiary.com/2018/07/sharepoint-online-update-document-metadata-using-powershell.html#ixzz6NdSy9ZJE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.