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