Renaming a file in SharePoint Online using Powershell….
Wasn’t as simple as I initially thought, you need to make use of MoveTo …
# Call the SharePoint DLL Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll" #ASSUMES CONNECTION TO SHAREPOINT ONLINE # Get reference to the site / library $web = Get-Web $list = $web.Lists.GetByTitle('Documents') $ctx.Load($list) $ctx.ExecuteQuery() $list.ItemCount # Pull back 5 documents $camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery $camlQuery.ViewXml = " 5 " $items = $list.GetItems($camlQuery) $ctx.Load($items) $ctx.ExecuteQuery() # Foreach file...rename foreach($file in $items) { $file1 = $file.File $ctx.Load($file1) $ctx.ExecuteQuery() $docName = $file1.name $newDocName = [String]::Concat($docName, "-Test-") $filePath = "/Documents/" + $newDocName # Make use of MoveTo to rename the file... $file1.MoveTo($filePath, [Microsoft.SharePoint.Client.MoveOperations]::Overwrite) $ctx.ExecuteQuery() }
Advertisements