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).