Monday, 28 June 2010

Add SharePoint farm solution and bulk activate feature using PowerShell

There are two parts to this PowerShell script – the first part is fairly straightforward as it simply adds and installs a solution file into the farm. I have added a line to the end of the script to wait until the solution has fully deployed before continuing:

#Set up Web Application variable
#Only needed if solution contains Web Application scoped resources
$webApp = Get-SPWebApplication -Identity http://portal

#Add solution file to the farm
$solution = Add-SPSolution -LiteralPath "C:\Install\Solution.wsp"

#Deploy solution
#Add -WebApplication $webApp to the line below if solution contains Web Application scoped resources
Install-SPSolution $solution -Force -GACDeployment

#Wait for solution to be deployed
do {Start-Sleep -s 1} while ($solution.Deployed -eq $false)

If you have multiple solution files to deploy, then it is worth checking out this script from Gary Lapointe, which enables you to install and deploy solution files in bulk using an XML configuration file.

The second part of the script activates a site or site collection feature, which is also pretty standard, except that I have provided four scenarios:

  • Activate site scoped feature in one site collection

$site = Get-SPSite http://portal
Enable-SPFeature -Identity "FeatureName" -Url $site.Url
$site.Dispose()

  • Activate site scoped feature for all site collections in a Web Application

$webApp = Get-SPWebApplication -Identity http://portal
$webApp | Get-SPSite -limit all | ForEach-Object {Enable-SPFeature -Identity "FeatureName" -Url $_.Url}

  • Activate web scoped feature in one site

$web = Get-SPWeb http://portal
Enable-SPFeature -Identity "FeatureName" -Url $web.Url
$web.Dispose()

  • Activate web scoped feature for all sites in a site collection

$site = Get-SPSite http://portal
$site | Get-SPWeb -limit all | ForEach-Object {Enable-SPFeature -Identity "FeatureName" -Url $_.Url}
$site.Dispose()

11 comments:

  1. Hello,
    Thank for sharing all these articles...
    I have a question about an error occurred when executing enable-spfeature for a feature with a event receiver attached: "Enable-SPFeature : Failed to load receiver assembly... ..System.IO.FileNotFoundExeception...". I make the build again without attached event receiver and this error disappear. What is strange is the fact that I can enable the feature from web without problems (with the event receiver)

    ReplyDelete
    Replies
    1. Did you ever solve this? I am having the same issue and would like to see if you were able to fix it and how.

      Delete
  2. Thanks! Is there a way to configure the parameters that my feature needs to connect to a external server? It is a form in the configuration of the site and is empty in every new site, i have a lot of sites. Im glad for your help.

    ReplyDelete
  3. Great stuff dude!
    I used the bulk activation of web scoped features and added to a complete deployment script. Funny thing is, that the server memory fills up to 1,5GB per web scoped feature that is activated. And I activate several, so you can imagine I'm having performance issues. :)
    Any thoughts on how to clear memory after each run of activating a web scoped feature without killing the powershell.exe process

    ReplyDelete
  4. Hi Mark - Are you disposing of the web object after activating features?

    ReplyDelete