1

I'm trying to build a PS script for a SharePoint 2007 which will loop through a list of site collections and update files within the 'Style Library' folder.

foreach ($file in $files) {
    $stream = $file.OpenRead()
    $done = $list.RootFolder.SubFolders["FolderName"].Files.Add($file.Name, $stream, $true)
    Write-Host $done.Name "Uploaded" -BackgroundColor Green
}

This loop tries to upload each file to the library but errors with the following message:

Exception calling "Add" with "3" argument(s): "The file "..." is not checked out. You must first check out this document before making changes."

I'm struggling to find a way to programmatically check out documents, upload the new copy and then check that in. Is there any way of working around this?

  • 1
    Have you tried $file.CheckOut() – Amal Hashim Apr 23 '15 at 15:44
  • Thanks Amal, looping through the live files and running $file.CheckOut() seems to have worked perfectly - thanks! – Ben Short Apr 23 '15 at 15:56
1

You can use CheckOut as below

foreach ($file in $files) {
    $file.CheckOut()
    $stream = $file.OpenRead()
    $done = $list.RootFolder.SubFolders["Hertel"].Files.Add($file.Name, $stream, $true)
    Write-Host $done.Name "Uploaded" -BackgroundColor Green
}
| improve this answer | |
  • Thanks Amal, I had to run a seperate loop as $files in this scenario is actually a local library on the C:\ drive. Instead I ran a loop on the folder in the list and checked out each file, once that loop completed it then uploaded the files. – Ben Short Apr 23 '15 at 16:01
  • Sounds Ok. I believe you can manage it in a single loop. Anyway nice to hear issue is resolved. – Amal Hashim Apr 23 '15 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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