Enable Features Programatically in SP 2010

We're currently migrating from SP 2007 to SP 2010. We were using the SmartPart web part in 2007, and have downloaded the updated SmartPart dll. I need to loop through each site collection in the farm and activate the feature (we have hundreds of site collections).

I keep getting "Feature with Id [feature GUID] is not installed in this farm, and cannot be added to this scope."

The feature is, in fact, installed, and I can activate it manually through Site Settings.  Here is my code:

// Already inside the site collection
SPFeatureDefinition featureDefinition = SPFarm.Local.FeatureDefinitions["SmartParts_Feature1"];
thisSite.Features.Add(featureDefinition.Id,true,SPFeatureDefinitionScope.Site);

Open in new window


Any insight would be greatly appreciated.

-MJC
 
LVL 5
RainverseAsked:
Who is Participating?
 
Sushanta SahuPrincipal Specialist, Information ManagementCommented:
Powershell is the answer. Quick and easy!
0
 
RainverseAuthor Commented:
Powershell was indeed the answer, but a little more of an answer would have been helpful.
0
 
RainverseAuthor Commented:
Here's what I came up with:

$siteUrl = "http:\\ourExtranet.ourserver.com"
 
$rootSite = New-Object Microsoft.SharePoint.SPSite($siteUrl)
$spWebApp = $rootSite.WebApplication
 
foreach($site in $spWebApp.Sites)
{
    if ($site.Url.Contains("extranet/")){
        echo $site.Url
        Enable-SPFeature -Identity "SmartParts_Feature1" -URL $site.Url
        echo "Feature has been activated..."
    }
    $site.Dispose()
} 

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.