Thursday, November 21, 2013

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


When you try to add web part on the page using  SPLimitedWebPartManager AddWebPart function you get the following error:

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



The error appears when  the page is checked out AFTER the SPLimitedWebPartManager is 
retrieved.

....................................................

$spWebPartManager = $spWeb.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

$page.CheckOut()
....................................................

So the page must be checked out BEFORE the SPLimitedWebPartManager is retrieved.

$page.CheckOut()


$spWebPartManager = $spWeb.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)