Sunday, August 29, 2010

Querying for features with PowerShell

On my current project we have a feature with a model that adds some aspx files in a document library. My problem is when I add a new file to the model manifest the new aspx is not now added to the document library, on WSP solution upgrade. The solution for this is to “re-active” the feature with the model. This is easily done, either through the GUI or by PowerShell “Enable-SPFeature –Force”, but the problem is I don’t know where the feature is already activated and I don’t won to look through over all the webs to check if the feature is activated, so I need to query for where the feature is activated. This is done by calling a method called “QueryFeatures” http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfeaturequeryresultcollection.aspx. This method can be found on the SPContentDatabase, SPWebApplication, SPWebService, and the SPSite classes.

In my example I query the SPWebService for two features and then re-active by calling Enable-SPFeature –Force N.B. this don’t disable the feature and there for, if there are a feature receiver attach to the feature, with FeatureDeactivating method, this well not be call.

My PowerShell (Code update 11/9-2010)

$featureGuids = ("41ee3cfa-0863-4c1c-9f6b-41697ff44ee0", "7176542e-ec21-411e-86b3-24abb5251a0b")
$contentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$featureGuids | % { $contentService.QueryFeatures([Guid]$_) } | % {
    Enable-SPFeature -Identity $_.DefinitionId -Url $_.Parent.Url -Force
}

No comments:

Post a Comment