6

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"
          }
            } 
        } 
}

3 Answers 3

8

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";
}
1
  • Also you don't need to loop through your webs if the feature is site scoped.
    – Simon Doy
    Commented Aug 2, 2013 at 7:19
2

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

2
  • 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
    Commented Aug 2, 2013 at 10:11
  • See my Edit above. Commented Aug 2, 2013 at 12:30
0

I have tried with below powershell script and activated all subsites feature - By Ditto

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Try

{

$webApplicationURL = "https://uatpoliciesandprocedures.altisource.com"

$featureFolderName = "204fea82-7f1c-4c14-b415-5355c1ded75a"

$webApp = Get-SPWebApplication $webApplicationURL

if($webApp -ne $null)

{

 foreach($siteColl in $webApp.Sites)

 {

   if($siteColl -ne $null)

   {

   foreach($subWeb in $siteColl.AllWebs)

   {

    if($subWeb -ne $null)

    {

     # Print each Subsite

     Write-Host $subWeb.Url

     #Get Feature ID based on the Feature Name

     $FeatureID = Get-SPFeature | Where {$_.id -eq $featureFolderName}

     if($FeatureID -ne $null)

     {

      #Check whether Feature to be activated is already activated for this subsite

      if (Get-SPFeature -Web $subWeb.Url | Where {$_.ID -eq $FeatureID.Id})

      {

       Write-Host $featureFolderName "is already activated at :" $subWeb.Url

                   Enable-SPFeature -Identity $featureFolderName -URL $subWeb.Url -Force 

                   Write-Host $featureFolderName "is Re-activated at :" $subWeb.Url

      }

      else

      {

       Enable-SPFeature -Identity $featureFolderName -Confirm:$false -Url $subWeb.url

       Write-Host $featureFolderName "has been activated at :" $subWeb.url

      }

     }

     $subWeb.Dispose()

    }

    else

    {

     Echo $subWeb "does not exist"

    }

   }

   $siteColl.Dispose()

  }

  else

  {

   Echo $siteColl "does not exist"

  }

 }

}

else

{

Echo $webApplicationURL "does not exist "

}

}

catch

{

   Write-Host -Object "Error occurred while running this script "

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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