Consider a scenario where you have a list of SharePoint Feature and you want to activate the same in number of site collection consider 200 site collections. What you will do ? Surely we won’t be doing it manually; we will be automating the process. PowerShell script is the answer for the same. In this article I will be showing you how to activate a list of features on number of site collection.
Here I have created an XML configuration file; where we are defining the list of features and list of Site collection and web applications we are targeting for this activity. Please find below is the structure of the XML file.
- <?xml version="1.0" encoding="utf-8" ?>
- <Configuration Environment="DEV" Version="1.0.0.0">
- <GlobalWebApplications>
- <GlobalWebApplication url="http://mydev.sharepoint.com/">
- <SiteCollections>
- <SiteCollection relativeURL="sites/TestSite1 " />
- <SiteCollection relativeURL="sites/TestSite2" />
- </SiteCollections>
- <CustomFeaturesToActivate>
- <Feature id="b176b379-ea64-4a10-b48f-dedfb6bddf81">
- <Name>Global.SharePoint.Infrastructure Site Columns Feature</Name>
- </Feature>
- <Feature id="f1a196ad-72bc-4950-a8f1-830155e2389f">
- <Name>Global.SharePoint.Infrastructure Content Types Feature</Name>
- </Feature>
- <Feature id="00ab0f8b-d183-4103-82af-4e331118aa31">
- <Name> Global.SharePoint.Infrastructure Site Lists</Name>
- </Feature>
- <Feature id="00a51d3d-874e-4b46-a972-b151468940c7">
- <Name> Global.SharePoint.Core Event Receivers</Name>
- </Feature>
- </CustomFeaturesToActivate>
- <CustomFeaturesToDeActivate>
- <Feature id="040a8ee5-5dcd-4a6b-a258-b010e0a975aa">
- <Name> Global.SharePoint.MyProject Site Lists</Name>
- </Feature>
- <Feature id="9a6f5d6a-d54c-4378-a195-06bcd21ed8d2">
- <Name> Global.SharePoint.MyProject Event Receivers</Name>
- </Feature>
- </CustomFeaturesToDeActivate>
- </GlobalWebApplication>
- </GlobalWebApplications>
- </Configuration>
As shown in file above define your environment DEV/UAT/PROD (DEV in my case),GlobalWebApplication URL (http://mydev.sharepoint.com/) ,list of site collections, list of CustomFeaturesToActivate and list of CustomFeaturesToDeActivate with their Ids and Name to avoid confusion.
PowerShell script is as follows.
- #check to see
- if the PowerShell Snapin is added
- if ((Get - PSSnapin | Where {
- $_.Name - eq "Microsoft.SharePoint.PowerShell"
- }) - eq $null) {
- Add - PSSnapin Microsoft.SharePoint.PowerShell;
- }
- #Add SharePoint DLL[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
- $global: currentPhysicalPath = Split - Path((Get - Variable MyInvocation - Scope 0).Value).MyCommand.Path
- #Read configuration.xml file[xml] $xmlinput = (Get - Content "$global:currentPhysicalPath\Configuration.xml")
- function Global_ActivateDeactivateFeatures([xml] $xmlinput)
- {
- foreach($configWebApp in $xmlinput.Configuration.GlobalWebApplications.GlobalWebApplication) {
- $webApp = Get - SPWebApplication $configWebApp.url - ErrorAction silentlycontinue
- if ($webApp - eq $null)
- {
- Write - host Web Application at url: $configWebApp.url does not Exists.. - foregroundcolor Red
- }
- else
- {
- foreach($siteCollection in $xmlinput.Configuration.GlobalWebApplications.GlobalWebApplication.SiteCollections.SiteCollection)
- {
- $siteCollUrl = $($webApp.url + $siteCollection.relativeUrl)
- $site = Get - SPSite - identity $siteCollUrl - ErrorAction SilentlyContinue
- if ($site - ne $null)
- {
- Write - Host - ForeGroundColor Yellow " - Activate site collection Custom Features ($($siteCollUrl)) "
- ForEach($feature in $xmlinput.Configuration.GlobalWebApplications.GlobalWebApplication.CustomFeaturesToActivate.Feature)
- {
- Global_ActivateFeature $feature $siteCollUrl
- }
- Write - Host - ForeGroundColor Yellow " - DeActivate site collection Custom Features ($($siteCollUrl))"
- ForEach($feature in $xmlinput.Configuration.GlobalWebApplications.GlobalWebApplication.CustomFeaturesToDeActivate.Feature)
- {
- Global_DeActivateFeature $feature $siteCollUrl
- }
- }
- else
- {
- Write - host - ForeGroundColor Red "Site Collection with this name does not exist. Please check if you have typed the URL correctly."
- }
- }
- }
- }
- }#EndRegion
- #Region Activate Custom Features# === === === === === === === === === === === === === === === === === === === === === === === === === === === == #Func: Activate Custom Features#Desc: Activates custom features across multiple site collections## === === === === === === === === === === === === === === === === === === === === === === === === === === === == function Global_ActivateFeature([System.Xml.XmlElement] $feature, [String] $url)
- {
- $identity = $feature.id
- $description = $feature.Name
- Write - Host - ForegroundColor Gray " - " - NoNewLine
- Write - Host $description - NoNewLine
- Enable - SPFeature–identity $identity - URL $url - Force - ErrorAction SilentlyContinue - Confirm: $false
- if (!$ ? )
- {
- if ($error[0].Exception.Message.Contains("is already activated at scope"))
- {
- Write - Host - ForeGroundColor Yellow " -- already activated.So Deactivating feature and Activating again"
- Write - Host - ForeGroundColor Yellow " -- Deactivating feature"
- Disable - SPFeature–identity $identity - URL $url - Force - ErrorAction SilentlyContinue - Confirm: $false
- Write - Host - ForeGroundColor Yellow " -- Deactivated and now activating again"
- Enable - SPFeature–identity $identity - URL $url - Force - ErrorAction SilentlyContinue - Confirm: $false
- Write - Host - ForeGroundColor Yellow " -- Activated."
- }
- else
- {
- Write - Host - ForegroundColor Red " error activating"
- Write - Host - ForegroundColor Red $error[0].Exception.Message
- Write - Host
- throw $error[0].Exception.Message
- }
- }
- else
- {
- Write - Host - ForegroundColor Green " -- activated."
- }
- }
- #EndRegion
- #Region DeActivate Custom Features# === === === === === === === === === === === === === === === === === === === === === === === === === === === == #Func: DeActivate Custom Features#Desc: DeActivates custom features across multiple site collections# === === === === === === === === === === === === === === === === === === === === === === === === === === === == function Global_DeActivateFeature([System.Xml.XmlElement] $feature, [String] $url)
- {
- $identity = $feature.id
- $description = $feature.Name
- Write - Host - ForegroundColor Gray " - " - NoNewLine
- Write - Host $description - NoNewLine
- Disable - SPFeature–identity $identity - URL $url - Force - ErrorAction SilentlyContinue - Confirm: $false
- if (!$ ? )
- {
- if ($error[0].Exception.Message.Contains("is not activated at this scope"))
- {
- Write - Host - ForeGroundColor Yellow " -- already deactivated"
- }
- else
- {
- Write - Host - ForegroundColor Red " error deactivating"
- Write - Host - ForegroundColor Red $error[0].Exception.Message
- Write - Host
- throw $error[0].Exception.Message
- }
- }
- else
- {
- Write - Host - ForegroundColor Green " -- deactivated."
- }
- }
- #EndRegion
- #Region
- function call
- start - transcript - path.\Configuration_Output.txt
- Global_ActivateDeactivateFeatures $xmlinput
- stop - transcript
- #EndRegion

Join the conversation! Your thoughts help the community grow.