Saturday, October 19, 2013

Enable Publishing Feature recursively in SP 2010 using PowerShell

So now that we saw an inconsistency among multiple sites, with some having Publishing feature enabled while others not, we decided to run a script which combs through each site within a site-collection and enables this feature, if not already enabled.

Given the situation that we had approx. 25-50 sites/sub-sites in each of the 7 site-collections, this appeared fair-enough to tackle one site-collection at-a-time.

To meet this requirement, we had to perform the below 2 high-level tasks:

1. Check and enable the [SharePoint Server Publishing Infrastructure] at the root site-collection level, if not already enabled. (Can be seen from “Site collection features” on “/_layouts/settings.aspx” page).

image

2. Check and enable the [SharePoint Server Publishing] at the site-level, if not already enabled. (Can be seen from “Manage site features” on “/_layouts/settings.aspx” page).

image

Enabling these features, creates the below 4 Lists & Libraries.

image

image

Now the script:

Step 1 was achieved by the script below:

# The URL to the Site Collection
$spSite = Get-SPSite “http://siteurl/”
Write-Host "Root Site Collection: " $spSite

$PublishingSitefeature = Get-SPFeature PublishingSite
$siteCollection = Get-SPSite $siteUrl

#Check and Enable the feature on the site collection
if((Get-SPFeature $PublishingSitefeature -Site $siteCollection.Url -ErrorAction SilentlyContinue) -eq $null )
   {
       write-host -foregroundcolor Yellow "Activating the PublishingWeb feature on " $siteCollection.Url
       Enable-SPFeature $PublishingSitefeature -Url $siteCollection.Url   
       write-host -foregroundcolor Green "Activated the PublishingWeb feature on " $siteCollection.Url
   }
   else
   {
      write-host -foregroundcolor red "PublishingWeb feature already activated on : " $siteCollection.Url
   }

Step 2 was achieved by the following script:

$siteCollection | Get-SPWeb -limit all | ForEach-Object{
   if((Get-SPFeature "PublishingWeb" -Web $_.Url -ErrorAction SilentlyContinue) -eq $null )
   {
      write-host -foregroundcolor Yellow "Activating the PublishingWeb feature on " $_.Url
      Enable-SPFeature -Identity "PublishingWeb" -Url $_.Url
      write-host -foregroundcolor Green "Activated the PublishingWeb feature on " $_.Url
   }
   else
   {
      write-host -foregroundcolor red "PublishingWeb feature already activated on : " $_.Url
   }
}

But as every project is seen over-running the estimates. here too we did not have luck to our side.

After Step 1 was performed successfully, we hit the first issue when started the Step 2.

But in my case, I got the below error, and could see that only [Pages] library got created (just few mins ago) and [Images] library pre-existed ( not sure who created that 6months ago).

“Enable-SPFeature : Provisioning did not succeed. Details: Failed to create the 'Images' library. OriginalException: The feature failed to activate because a list at 'PublishingImages' already exists in this site. Delete or rename the list and try activating the feature again. At C:\Users\abcd\Documents\PS\ActivatePublishingFeatureonAllSites.ps1:86 char:23
+ Enable-SPFeature <<<< -Identity "PublishingWeb" -Url $_.Url #where the PublishingWeb is the internal name of the SharePoint Server Publishing feature + CategoryInfo : InvalidData: (Microsoft.Share...etEnableFeature:SPCmdletEnableFeature) [Enable-SPFeature], SPException
+ FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletEnableFeature”

So I deleted this [Images] library using SPD 2010.

image

image

Deleting [Pages] required me to delete the [Forms] sub-folder and then all the sub-folders and files as below within the [Forms] sub-folder:

image

The above files were deleted using SPD 2010, but couldn’t delete the [Forms] folder. So I renamed the [Pages] library to [Pages2] and tried to re-run the PS script again. But this did not solve the issue. This time I received the below error:

“Enable-SPFeature : Provisioning did not succeed. Details: Failed to create the 'Pages' library. OriginalException: There can only be one instance of this list type in a web. An instance already exists. At C:\Users\abcd\Documents\PS\ActivatePublishingFeatureonAllSites.ps1:86 char:23
+ Enable-SPFeature <<<< -Identity "PublishingWeb" -Url $_.Url #where the PublishingWeb is the internal name of the SharePoint Server Publishing feature + CategoryInfo : InvalidData: Microsoft.Share...etEnableFeature:SPCmdletEnableFeature) [Enable-SPFeature], SPException
+ FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletEnableFeature”.

The reason for this being that you cannot delete few libraries like [Pages] in a site which have the publishing feature enabled. For the case of libraries like the [Images], [Documents] or even the [Site Collection Documents], it is not possible to remove them from the lists settings page or SPD or the manage content and structure tool.

It appears that these lists and libraries have a property AllowDeletion, which has been set as False.

We can use the below script to verify this:

$web = Get-SPWeb “http://siteurl/abcd/”
$lib = $web.Lists[“Pages2”]
$lib.AllowDeletion

This returns “False” as seen below:

image

So what do we do?

PowerShell to rescue again. This allows you to set the property to “True” and then successfully delete the library.

Use the below script to set the property to “True”:

$web = Get-SPWeb “http://siteurl/abcd/”
$lib = $web.Lists[“Pages2”]
$lib.AllowDeletion = $True
$list.Update()

I could then easily use SPD or the browser to delete the [Pages] library (in my case I renamed it to [Pages2]).

Finally I re-executed my script successfully and got the feature enabled.

No comments: