Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a powershell script that inserts webparts onto a page. The problem is that if I don't have the page checked out it will throw the following error:

Exception calling "AddWebPart" with "3" argument(s): "The file is not checked out. You must first check out this document before making changes." At C:\temp\AddWebParts.ps1:49 char:27

Is it possible to checkout a page using PowerShell?

share|improve this question
add comment

4 Answers

up vote 2 down vote accepted

Found this after doing a little more research:

$Site = Get-SPWeb $siteurl
$Site.GetFile($myFile).CheckOut()

Hope this helps someone!

share|improve this answer
add comment

yes, if you have SPListItem, you may call

$file = $listItem.File;
$file.CheckOut();
share|improve this answer
    
Thanx! This works great. How do you check in the same file again? –  Benny Skogberg Apr 23 '12 at 13:20
add comment

I think the POSH script excerpts above (by Abe & Ashish) would work but also verify the CheckOut & Lock status property of the file prior to checking it out: $fooFile.CheckOutType - this returns enum type showing online, offline & none. To check the file property LockType - $fooFile.LockType which returns enum SPLockType (exclusive, shared & none).

share|improve this answer
    
The code will be similar to the following: 1. $fooWeb = Get-SPWeb("FooWebURL"); 2. $fooFile = $fooWeb.GetFile("FooFile"); 3. if($fooFile.CheckOutType -eq "None" -And $fooFile.LockType -eq "None") 4. { $fooFile.CheckOut() 5. Write-Host $fooFile.Name Checked out 6. } else 7. { Write-Host $fooFile.Name already Checked out or locked } 8. $fooWeb.Dispose() –  Supriyo SB Chatterjee Dec 16 '11 at 21:21
add comment

Below script will help you to checkout file

$urlWeb = "http://mysite"
$urlWebWP =  "http://mysite/pages/default.aspx"

$web = Get-SPWeb 
$urlWeb $page =  $web.GetFile($urlWebWP)
$page.CheckOut()
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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