I've got a script that goes through subwebs of a sie and adds a webpart to the page. Everything is functioning correctly, until it's time to check the file in. I get the following error:
You cannot call a method on a null-valued expression. At D:\scripts\AddWebPart.ps1:73 char:54 + $publishingPage.ListItem.File.CheckIn <<<< ($comment) + CategoryInfo : InvalidOperation: (CheckIn:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
This is the line that blows up:
$publishingPage.ListItem.File.CheckIn("Added Content Editor Webpart")
Also tried the other overload:
$publishingPage.ListItem.File.CheckIn("Added Content Editor Webpart", [Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)
With the same result. What am I missing? Everything seems legit to me.
Adding more code as requested. This is the working version*. [It works on my dev environment, but blows up on the staging environment due to something unrelated to my original question.
#Add SharePoint assemblies
[system.reflection.assembly]::loadwithpartialname("microsoft.sharepoint")
[system.reflection.assembly]::loadwithpartialname("microsoft.sharepoint.publishing")
function add-ContentEditorWebPart($siteUrl, $webUrl)
{
#Set up default variables
$siteUrl = $siteUrl.ToString()
$webUrl = $webUrl.ToString()
#$content is going to be the contents of the content editor webpart [google remarketing tag in this case]
$content = '<!--script here-->'
$webpartzone = "bottomWebPart"
$index = 0
$comment = "Google Remarketing Script Added"
#Get site and web objects
$site = new-Object Microsoft.SharePoint.SPSite($siteUrl)
$web = $site.OpenWeb($webUrl)
foreach ($subweb in $web.Webs)
{
if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($subweb)) {
write-host "Reviewing pages in"$subweb.Title"site...."
#Get the Publishing Web and pages within it
$publishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($subweb)
$publishingPages = $publishingWeb.GetPublishingPages()
foreach ($publishingPage in $publishingPages)
{
$file = $subweb.GetFile($publishingPage.Uri.ToString())
#Check to ensure the page isn't checked out
if ($publishingPage.ListItem.File.CheckOutStatus -eq "None")
{
Write-Host "Checking out page: " $publishingPage.Title
$file.CheckOut()
#$publishingPage.ListItem.File.CheckOut()
### add webpart to page here
$webpartmanager = $subweb.GetLimitedWebPartManager($publishingPage.Uri, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$webpart = new-object Microsoft.SharePoint.WebPartPages.ContentEditorWebPart
$webpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::None;
$webpart.Title = 'Google Remarketing'
$docXml = New-Object System.Xml.XmlDocument
$contentXml = $docXml.CreateElement("div");
$contentXml.set_InnerText($content);
$docXml.AppendChild($contentXml);
$webpart.Content = $contentXml;
$webpartmanager.AddWebPart($webpart, $webpartzone, $index); #the code in the brakets adds the $webpart to the mentioned zone and sets the sorting of the webpart on the first place
$webpartmanager.Dispose();
$file.Update()
$file.CheckIn($comment, [Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)
$file.Approve("Approved automatically by PowerShell script.")
write-host "Added Content Editor Webpart and checked in file: " $publishingPage.Title "`n"
}
else
{
#Notify user that the page is checked out and cannot be modified
write-host "Page"$publishingPage.Title"is currently checked out to"$publishingPage.ListItem.File.CheckedOutBy"and cannot be modified"
}
}
$subweb.Dispose()
}
else
{
#Notify user that the site is not a publishing site
write-host $subweb.Title"is not a publishing site"
}
}
#Dispose of Site object
$web.Dispose()
$site.Dispose()
}
As I said above, this runs great on my dev environment, but when trying to run it from the staging environment it complains about the foreach loop on line 26:
An exception was thrown when trying to enumerate the collection: " At D:\scripts\AddContenteditor.ps1:26 char:17 + foreach <<<< ($publishingPage in $publishingPages)