Activating the site collection feature on all site collections with PowerShell

With our custom branding solution deployed to the SharePoint farm, we need to activate the site collection feature. The simplest method to activate the site collection feature on all site collections is using PowerShell.

How to do it...

Follow these steps to activate the feature on each site collection in the farm:

  1. Assign the SiteCollectionBranding feature ID to a PowerShell variable, using the following command:
    $brandingFeatureId = [GUID]"19e46226-efb9-4761-b09a-cb8711fd503a"
    
  2. Use the Get-SPWebApplication Cmdlet to get the content web applications and iterate through them as follows:
    foreach ($webApp in (Get-SPWebApplication))
  3. Iterate through each site collection in the web application using the following code:
    foreach ($site in $webApp.Sites)
  4. Ensure the site collection is in 2013 mode.
    if ($site.CompatibilityLevel –eq 15)
  5. Verify that the SiteCollectionBranding feature is in the collection of activated features on the site collection. If the feature is not activated, add it to the collection as follows:
    if ($site.Features[$brandingFeatureId] -eq $null) 
    {
    
    $site.Features.Add($brandingFeatureId)
    
    }
  6. Use the Dispose method to discard the SPSite object.
    $site.Dispose()

How it works...

In this recipe, we retrieved all of the content web applications with the Get-SPWebApplication Cmdlet. For each web application we then iterated through each site collection in the Sites property. Lastly, we checked to ensure the site collection feature was activated on each site collection.

Adding or removing features is accomplished by adding or removing them from the collection of features exposed with the Features property of the web application, site collection, or site.

There's more...

Activating the site collection feature on all site collections can also be done with code using the server-side object model. Follow these steps to activate the site collection feature with code using the server-side object model:

  1. Assign the SiteCollectionBranding feature ID to a variable as follows:
    var featureBrandingSiteCollectionId = new Guid("19e46226-efb9-4761-b09a-cb8711fd503a");
  2. Iterate through each content web application.
    foreach (SPWebApplication webApp in SPWebService.ContentService.WebApplications)
  3. Iterate through each site collection in the web application.
    foreach (SPSite site in webApp.Sites)
  4. Ensure the site collection is in 2013 mode.
    if (site.CompatibilityLevel == 15)
  5. Verify if the SiteCollectionBranding feature is in the collection of activated features on the site collection. If not, add the feature to the collection as follows:
    if (site.Features[featureBrandingSiteCollectionId] == null)
    site.Features.Add(featureBrandingSiteCollectionId);

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.9.197