Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am trying to activate a Site scoped feature using a powershell script. I have another scrip which deploys my solution and I see my feature is installed correctly in Site Collection feature gallery. On executing the below function I get "feature not found" as my custom message. I am confused between what URL I should pass in the my function.

Here are the URLs

WebApplication : http://myWebapplication

Site Collection: http://myWebapplication/sites/mySiteCollection

Web Site: http://myWebapplication/sites/mySiteCollection/myWebSite

function ActivateWebFeatures() 
    { 

    if ($webLevelFeatures -ne $null) --Arrary that stores feature names
    { 

        $spSiteCollection = Get-SPSite "http://myWebapplication/sites/mySiteCollection" 

     if($spSiteCollection -ne $null)
     {
        #Cycle through all webs in the collection and activate all the features 
        foreach($spWeb in $spSiteCollection.AllWebs) 
        {   
            foreach($webFeatureName in $webLevelFeatures) 
            {         

                $webFeature = Get-SPFeature -Web $spSiteCollection | where-object {$_.displayname -eq $webFeatureName}  -ErrorAction:Inquire
                if ($webFeature -ne $null)  
                { 
                    InformationMessage "Trying to activate $webFeatureName at $spWeb.url"
                    Enable-SPFeature –identity $webFeatureName -URL $spSiteCollection -Confirm:$false -ErrorAction:SilentlyContinue
                } 
            else
          {
           InformationMessage "$webFeatureName feature not found"
          }
            } 
        } 
}
share|improve this question

2 Answers 2

Ok so the Get-SPFeature works differently depending on what parameters you pass it. The following line is asking which features are enabled at the Web Scoped level.

$webFeature = Get-SPFeature -Web $spSiteCollection | Where-Object....

However, if you want to see if your feature is installed then you need to use Get-SPFeature without passing any scope to it. So you would do:-

$featureInstalled = Get-SPFeature -Identity $webFeatureName;

Once you know that the feature is installed then you need to check to see if the feature is enabled. We need to check whether the feature is enabled at the site scoped level rather than web level because as you mention your feature is site scoped. If it hasn't been enabled then we will get a null value back so we check for the null value and then enable the feature as you were already doing.

So the code would be something like this:-

$featureEnabled = = Get-SPFeature -Site $spSiteCollection -Identity $featureName -ErrorAction SilentlyContinue;
if($featureEnabled-eq $null)
{
   Enable-SPFeature -Identity $featureName -Url $spSiteCollection.Url;
}
else
{
   Write-Host "Feature $featureName already enabled";
}
share|improve this answer
    
Also you don't need to loop through your webs if the feature is site scoped. –  Simon Doy Aug 2 '13 at 7:19

I think you need to refer to web url instead of site collection url in the following line, since you are looping through each web in the site collection

InformationMessage "Trying to activate $webFeatureName at $spWeb.url"
Enable-SPFeature –identity $webFeatureName -URL $spSiteCollection -Confirm:$false -ErrorAction:SilentlyContinue

It should be

Enable-SPFeature –identity $webFeatureName -URL $spWeb.url -Confirm:$false -ErrorAction:SilentlyContinue

EDIT

This line also needs to be updated to compare web level features:

$webFeature = Get-SPFeature -Web $spSiteCollection | where-object {$_.displayname -eq $webFeatureName}  -ErrorAction:Inquire

Here you need to use $spWeb.url instead of $spSiteCollection

share|improve this answer
    
yea I was using $spWeb.url. But when i try to find the feature with $webFeature = Get-SPFeature -Web $spSiteCollection | where-object {$_.displayname -eq $webFeatureName} it wont find it and exit. –  V.B Aug 2 '13 at 10:11
    
See my Edit above. –  Nadeem Yousuf Aug 2 '13 at 12:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.