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 had a script I was working on a while back that got put on the back burner, but now I'm trying to finish it up, and I thought I had reached that point. I was running this script on our development environment with the desired results. However, once I switched to our staging environment it blows up on the foreach that goes through all subwebs of the specified Web. What gives? I don't see why it would blow up, but I'm also new to PowerShell.. so maybe it's something obvious?

#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 = '<!--scripts goes 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()
}

The error that I get is the following:

An exception was thrown when trying to enumerate the collection: "The method or operation is not implemented.". At D:\scripts\AddContentEditor.ps1:44 char:17 + foreach <<<< ($publishingPage in $publishingPages)

Note: I removed the 'content' as to not post possibly sensitive information. So the line number of the error isn't correct.. but you can find that line by finding ($publishingPage in $publishingPages).

share|improve this question
add comment

2 Answers 2

Usually this kind of error is the result of your current user not having access to one of the sub webs. You should either elevate permissions or use the sub webs for the current user.

share|improve this answer
    
That very well could be, but I'm logged in as our 'service account' which is supposed to have the highest privelages. I'm not sure how to check though. –  snowBlind Apr 19 '12 at 18:24
    
I just realized I forgot to post the error along with the code. –  snowBlind Apr 19 '12 at 19:24
add comment

$publishingWeb.GetPublishingPages() may return $null so you should check for that before looping through the pages

share|improve this answer
    
I tried to do a write host on $publishingPages, which was the variable that I stored whatever $publishingWeb.GetPublishingPages() returned, but I get the following error: Write-Host : The method or operation is not implemented. At D:\scripts\AddContentEditor.ps1:45 char:14 + write-host <<<< $publishingPages –  snowBlind Apr 23 '12 at 17:51
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.