SharePoint 2013: Activate, Deactive Multiple Feature on Number of Site Collection in Single Go

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.

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <Configuration Environment="DEV" Version="1.0.0.0">  
  3.     <GlobalWebApplications>  
  4.         <GlobalWebApplication url="http://mydev.sharepoint.com/">  
  5.             <SiteCollections>  
  6.                 <SiteCollection relativeURL="sites/TestSite1 " />  
  7.                 <SiteCollection relativeURL="sites/TestSite2" />  
  8.             </SiteCollections>  
  9.             <CustomFeaturesToActivate>  
  10.                 <Feature id="b176b379-ea64-4a10-b48f-dedfb6bddf81">  
  11.                     <Name>Global.SharePoint.Infrastructure Site Columns Feature</Name>  
  12.                 </Feature>  
  13.                 <Feature id="f1a196ad-72bc-4950-a8f1-830155e2389f">  
  14.                     <Name>Global.SharePoint.Infrastructure Content Types Feature</Name>  
  15.                 </Feature>  
  16.                 <Feature id="00ab0f8b-d183-4103-82af-4e331118aa31">  
  17.                     <Name> Global.SharePoint.Infrastructure Site Lists</Name>  
  18.                 </Feature>  
  19.                 <Feature id="00a51d3d-874e-4b46-a972-b151468940c7">  
  20.                     <Name> Global.SharePoint.Core Event Receivers</Name>  
  21.                 </Feature>  
  22.             </CustomFeaturesToActivate>  
  23.             <CustomFeaturesToDeActivate>  
  24.                 <Feature id="040a8ee5-5dcd-4a6b-a258-b010e0a975aa">  
  25.                     <Name> Global.SharePoint.MyProject Site Lists</Name>  
  26.                 </Feature>  
  27.                 <Feature id="9a6f5d6a-d54c-4378-a195-06bcd21ed8d2">  
  28.                     <Name> Global.SharePoint.MyProject Event Receivers</Name>  
  29.                 </Feature>  
  30.             </CustomFeaturesToDeActivate>  
  31.         </GlobalWebApplication>  
  32.     </GlobalWebApplications>  
  33. </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.

  1. #check to see  
  2. if the PowerShell Snapin is added  
  3. if ((Get - PSSnapin | Where {  
  4.     $_.Name - eq "Microsoft.SharePoint.PowerShell"  
  5. }) - eq $null) {  
  6.     Add - PSSnapin Microsoft.SharePoint.PowerShell;  
  7. }  
  8.  
  9. #Add SharePoint DLL[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")  
  10. $global: currentPhysicalPath = Split - Path((Get - Variable MyInvocation - Scope 0).Value).MyCommand.Path  
  11.  
  12. #Read configuration.xml file[xml] $xmlinput = (Get - Content "$global:currentPhysicalPath\Configuration.xml")  
  13.   
  14. function Global_ActivateDeactivateFeatures([xml] $xmlinput)   
  15. {  
  16.     foreach($configWebApp in $xmlinput.Configuration.GlobalWebApplications.GlobalWebApplication) {  
  17.         $webApp = Get - SPWebApplication $configWebApp.url - ErrorAction silentlycontinue  
  18.         if ($webApp - eq $null)   
  19.         {  
  20.             Write - host Web Application at url: $configWebApp.url does not Exists.. - foregroundcolor Red  
  21.         }  
  22.         else   
  23.         {  
  24.             foreach($siteCollection in $xmlinput.Configuration.GlobalWebApplications.GlobalWebApplication.SiteCollections.SiteCollection)   
  25.             {  
  26.                 $siteCollUrl = $($webApp.url + $siteCollection.relativeUrl)  
  27.                 $site = Get - SPSite - identity $siteCollUrl - ErrorAction SilentlyContinue  
  28.                 if ($site - ne $null)   
  29.                 {  
  30.   
  31.                     Write - Host - ForeGroundColor Yellow " - Activate site collection Custom Features ($($siteCollUrl)) "  
  32.                     ForEach($feature in $xmlinput.Configuration.GlobalWebApplications.GlobalWebApplication.CustomFeaturesToActivate.Feature)   
  33.                     {  
  34.                         Global_ActivateFeature $feature $siteCollUrl  
  35.                     }  
  36.   
  37.                     Write - Host - ForeGroundColor Yellow " - DeActivate site collection Custom Features ($($siteCollUrl))"  
  38.                     ForEach($feature in $xmlinput.Configuration.GlobalWebApplications.GlobalWebApplication.CustomFeaturesToDeActivate.Feature)   
  39.                     {  
  40.                         Global_DeActivateFeature $feature $siteCollUrl  
  41.                     }  
  42.                 }   
  43.                 else   
  44.                 {  
  45.                     Write - host - ForeGroundColor Red "Site Collection with this name does not exist. Please check if you have typed the URL correctly."  
  46.                 }  
  47.   
  48.             }  
  49.         }  
  50.     }  
  51. }#EndRegion  
  52.  
  53. #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)   
  54. {  
  55.     $identity = $feature.id  
  56.     $description = $feature.Name  
  57.     Write - Host - ForegroundColor Gray " - " - NoNewLine  
  58.     Write - Host $description - NoNewLine  
  59.   
  60.     Enable - SPFeature–identity $identity - URL $url - Force - ErrorAction SilentlyContinue - Confirm: $false  
  61.   
  62.     if (!$ ? )   
  63.     {  
  64.         if ($error[0].Exception.Message.Contains("is already activated at scope"))   
  65.         {  
  66.             Write - Host - ForeGroundColor Yellow " -- already activated.So Deactivating feature and Activating again"  
  67.             Write - Host - ForeGroundColor Yellow " -- Deactivating feature"  
  68.             Disable - SPFeature–identity $identity - URL $url - Force - ErrorAction SilentlyContinue - Confirm: $false  
  69.             Write - Host - ForeGroundColor Yellow " -- Deactivated and now activating again"  
  70.             Enable - SPFeature–identity $identity - URL $url - Force - ErrorAction SilentlyContinue - Confirm: $false  
  71.             Write - Host - ForeGroundColor Yellow " -- Activated."  
  72.         }   
  73.         else   
  74.         {  
  75.             Write - Host - ForegroundColor Red " error activating"  
  76.             Write - Host - ForegroundColor Red $error[0].Exception.Message  
  77.             Write - Host  
  78.             throw $error[0].Exception.Message  
  79.         }  
  80.     }   
  81.     else   
  82.     {  
  83.         Write - Host - ForegroundColor Green " -- activated."  
  84.     }  
  85. }  
  86. #EndRegion  
  87.  
  88.  
  89. #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)   
  90. {  
  91.     $identity = $feature.id  
  92.     $description = $feature.Name  
  93.     Write - Host - ForegroundColor Gray " - " - NoNewLine  
  94.     Write - Host $description - NoNewLine  
  95.   
  96.     Disable - SPFeature–identity $identity - URL $url - Force - ErrorAction SilentlyContinue - Confirm: $false  
  97.   
  98.     if (!$ ? )   
  99.     {  
  100.         if ($error[0].Exception.Message.Contains("is not activated at this scope"))   
  101.         {  
  102.             Write - Host - ForeGroundColor Yellow " -- already deactivated"  
  103.         }   
  104.         else   
  105.         {  
  106.             Write - Host - ForegroundColor Red " error deactivating"  
  107.             Write - Host - ForegroundColor Red $error[0].Exception.Message  
  108.             Write - Host  
  109.             throw $error[0].Exception.Message  
  110.         }  
  111.     }  
  112.     else   
  113.     {  
  114.         Write - Host - ForegroundColor Green " -- deactivated."  
  115.     }  
  116. }  
  117. #EndRegion  
  118.  
  119. #Region  
  120. function call  
  121. start - transcript - path.\Configuration_Output.txt  
  122. Global_ActivateDeactivateFeatures $xmlinput  
  123. stop - transcript  
  124. #EndRegion   
Ebook Download
View all
Learn
View all