Thursday, August 6, 2015

Apply web.config modifications to all web applications on Sharepoint farm at once

As you probably know we may use SPWebConfigModifications class for applying changes in web.config of particular web application using it’s SPWebApplication.WebConfigModifications property. There are many examples available and I won’t duplicate it here (see e.g. this). However with this approach we may only change web.config of single web application. But what if we need to change web.configs of all web applications including those which are currently exist in the farm and which will be created in future (if we don’t want to continuously monitor created web applications)? Is it possible? The answer is yes: in order to do that we need apply web config modifications to Microsoft.SharePoint.Administration.SPWebService.ContentService using SPWebService.WebConfigModifications property, e.g. as shown here How to turn on and off the multiple metadata formats for JSON in SharePoint Server 2013:

   1: $configOwnerName = "JSONLightDependentAssembly"
   2:  
   3: $spWebConfigModClass ="Microsoft.SharePoint.Administration.SPWebConfigModification"
   4:  
   5: $dependentAssemblyPath =
   6: "configuration/runtime/*[local-name()='assemblyBinding' and namespace-uri()='urn:schemas-microsoft-com:asm.v1']"
   7:  
   8: $dependentAssemblyNameStart ="*[local-name()='dependentAssembly'][*/@name='"
   9: $dependentAssemblyNameEnd = "'][*/@publicKeyToken='31bf3856ad364e35'][*/@culture='neutral']"
  10:  
  11: $dependentAssemblyValueStart = "<dependentAssembly><assemblyIdentity name='"
  12: $dependentAssemblyValueEnd =
  13: "' publicKeyToken='31bf3856ad364e35' culture='neutral' /><bindingRedirect oldVersion='5.0.0.0' newVersion='5.6.0.0' /></dependentAssembly>"
  14:  
  15: $edmAssemblyName ="Microsoft.Data.Edm"
  16: $odataAssemblyName ="Microsoft.Data.Odata"
  17: $dataServicesAssemblyName ="Microsoft.Data.Services"
  18: $dataServicesClientAssemblyName ="Microsoft.Data.Services.Client"
  19: $spatialAssemblyName ="System.Spatial"
  20:  
  21:  
  22: $assemblyNamesArray = $edmAssemblyName,$odataAssemblyName,$dataServicesAssemblyName,
  23: $dataServicesClientAssemblyName, $spatialAssemblyName
  24:  
  25:  
  26: Add-PSSnapin Microsoft.SharePoint.Powershell
  27: $webService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
  28:  
  29:  
  30: ################ Adds individual assemblies ####################
  31:  
  32: For ($i=0; $i -lt 5; $i++)  
  33: {
  34:     echo "Adding Assembly..."$assemblyNamesArray[$i]
  35:  
  36:     $dependentAssembly = New-Object $spWebConfigModClass
  37:     $dependentAssembly.Path=$dependentAssemblyPath
  38:     $dependentAssembly.Sequence =0 # First item to be inserted
  39:     $dependentAssembly.Owner = $configOwnerName
  40:     $dependentAssembly.Name =$dependentAssemblyNameStart + $assemblyNamesArray[$i] +
  41: $dependentAssemblyNameEnd
  42:     $dependentAssembly.Type = 0 #Ensure Child Node
  43:     $dependentAssembly.Value = $dependentAssemblyValueStart + $assemblyNamesArray[$i] +
  44: $dependentAssemblyValueEnd
  45:  
  46:     $webService.WebConfigModifications.Add($dependentAssembly)
  47: }
  48:  
  49: ###############################################################
  50:  
  51: echo "Saving Web Config Modification"
  52:  
  53: $webService.Update()
  54: $webService.ApplyWebConfigModifications()
  55:  
  56: echo "Update Complete"

As result the following assembly binding redirections will be added to all existing web applications and to all new web applications which will be created in future:

   1: <runtime>
   2:   <assemblyBinding>
   3:     ...
   4:     <dependentAssembly>
   5:       <assemblyIdentity name="Microsoft.Data.Edm" publicKeyToken="31bf3856ad364e35"
   6: culture="neutral" />
   7:       <bindingRedirect oldVersion="5.0.0.0" newVersion="5.6.0.0" />
   8:     </dependentAssembly>
   9:     <dependentAssembly>
  10:       <assemblyIdentity name="Microsoft.Data.Odata" publicKeyToken="31bf3856ad364e35"
  11: culture="neutral" />
  12:       <bindingRedirect oldVersion="5.0.0.0" newVersion="5.6.0.0" />
  13:     </dependentAssembly>
  14:     <dependentAssembly>
  15:       <assemblyIdentity name="Microsoft.Data.Services.Client"
  16: publicKeyToken="31bf3856ad364e35" culture="neutral" />
  17:       <bindingRedirect oldVersion="5.0.0.0" newVersion="5.6.0.0" />
  18:     </dependentAssembly>
  19:     <dependentAssembly>
  20:       <assemblyIdentity name="Microsoft.Data.Services"
  21: publicKeyToken="31bf3856ad364e35" culture="neutral" />
  22:       <bindingRedirect oldVersion="5.0.0.0" newVersion="5.6.0.0" />
  23:     </dependentAssembly>
  24:     <dependentAssembly>
  25:       <assemblyIdentity name="System.Spatial" publicKeyToken="31bf3856ad364e35"
  26: culture="neutral" />
  27:       <bindingRedirect oldVersion="5.0.0.0" newVersion="5.6.0.0" />
  28:     </dependentAssembly>
  29:   </assemblyBinding>
  30: </runtime>

This approach may simplify administrative efforts because it won’t be necessary to execute separate script for each web applications and monitor created web applications for executing script for them.

No comments:

Post a Comment