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'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

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

                    ### 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");

                    $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


                    $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"

                    #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"
            #Notify user that the site is not a publishing site
            write-host $subweb.Title"is not a publishing site"
    #Dispose of Site object

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)

share|improve this question
Can you run this script in PowerGUI Script Editor, register a break point, and ensure that all the objects have values? The error seems to indicate you are passing a variable $comment that is in fact null at the time the line is executed. –  Robert Kaucher Mar 30 '12 at 0:34
I missed that when pasting data over. I have $comment = "Added Content Editor Webpart" at the top of the script. I just removed that for sake of this question. On a side note: I tried running it as with the string directly in it before I even tried assigning it to a variable (as show above), with the same result. I will go into PowerGUI now and check the values as I step through. –  snowBlind Mar 30 '12 at 14:30
I verified in PowerGUI that $comment contains my string, and I also did 'write-host $comment' in the line directly above my checkin, and it also spit out my string to the console. However, I still get the same error when I try to check the page in. –  snowBlind Mar 30 '12 at 14:37
Could you post more of your code? I'd like to see how you are working with the item before you execute this line. –  Robert Kaucher Mar 30 '12 at 15:24
I was able to get this working, but I had to alter my code as to not use ListItem. I found a blog post where someone else was having the same issue, and he listed the alternative method. –  snowBlind Mar 30 '12 at 20:13

1 Answer 1

I have done a little testing and I am nearly certain that at the time you hit that line the variable $publishingPage is null.

PS C:\Users\rkaucher> $item.File.CheckOut();
You cannot call a method on a null-valued expression.
At line:1 char:20
+ $item.File.CheckOut <<<< ();
    + CategoryInfo          : InvalidOperation: (CheckOut:String) [], Runtime
    + FullyQualifiedErrorId : InvokeMethodOnNull

And I receive the error because the $item variable has gone null for some reason after I tried to refresh its instance. Please post more code! And try doing something like printing one of the properties of $publishingPage to the console.

share|improve this answer
I posted my complete code above. However, by the time I seen your response, I had already altered my code to solve the problem. I commented out the 2 lines I was using before, so you can still see them. I found this article: osric.com/chris/accidental-developer/2011/11/… where the person was doing the same thing as I, and having a similar problem. So, after altering my code based on that, it checked in just fine... in which case I thought I was golden until trying to run it on the staging server! –  snowBlind Mar 30 '12 at 21:29
Make sure you post that as an answer. This way it shows up as an answered question for other people searching. –  Robert Kaucher Mar 31 '12 at 0:10

Your Answer


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.