none
PowerShell and SharePoint 2007: Cannot find an overload for “Add” and the argument count: “2”

    Question

  • I am trying to write a PowerShell script to enable a feature on all subwebs of a given site collection and everything was going swimmingly until I tried to get the actual feature to be enabled. I am successful in using the SharePoint API everywhere, but when i get to this:

        $activatedFeature = $web.Features.Add($Feature.Id, $true)

    I am getting a ridiculous exception that I cant seem to figure out.

        System.Management.Automation.MethodException: Cannot find an overload for "Add" and the argument count: "2".
        at System.Management.Automation.Adapter.FindBestMethod(String methodName, MethodInformation[] methods, Object[] arguments, Boolean& expandParamsOnBest)
        at System.Management.Automation.DotNetAdapter.MethodInvokeDotNet(String methodName, Object target, MethodInformation[] methodInformation, Object[] arguments)
        at System.Management.Automation.Adapter.BaseMethodInvoke(PSMethod method, Object[] arguments)
        at System.Management.Automation.ParserOps.CallMethod(Token token, Object target, String methodName, Object[] paramArray, Boolean callStatic, Object valueToSet)
        at System.Management.Automation.MethodCallNode.InvokeMethod(Object target, Object[] arguments, Object value)
        at System.Management.Automation.MethodCallNode.Execute(Array input, Pipe outputPipe, ExecutionContext context)
        at System.Management.Automation.AssignmentStatementNode.Execute(Array input, Pipe outputPipe, ExecutionContext context)
        at System.Management.Automation.StatementListNode.ExecuteStatement(ParseTreeNode statement, Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)

    I cant say that this exception makes much sense to me, nor can I find anything helpful from resources here or searching on key terms.

    Has anyone experienced something like this before? If so, how do i correct it.

    Friday, February 24, 2012 10:47 PM

Answers

All replies

  • What does $web.Features | GM show?
    Friday, February 24, 2012 11:20 PM
  • Hey JS,

    If you can post script here then it will be easy to understand what wrong with it rather then putting single line of code.  You may find following link helpful to achieve your target.

    http://bable.cybermarshall.com/2009/01/17/using-powershell-to-activate-a-feature-across-all-sharepoint-2007wss-30-sites-and-subsites/

    This is just for your knowledge. Alternativly you can try "Enable-SPfeature" command to make it easy. However it works with SP 2010 and not sure about SP 2007. 

    Refer this link: http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2011/01/04/activating-and-deactivating-features-with-powershell-in-sharepoint-2010.aspx

    Hope this helps...!!!


    Thanks & Regards
    Bhavik Solanki

    Please click “Mark as Answer” if this post answers your question and click "Vote as Helpful if this Post helps you.


    Saturday, February 25, 2012 2:26 AM
  • Hi Bhavik. I got my original idea from that first link. The reason I am not using it is that its very slow. There are a couple hundred subwebs and it has to be called several times. Using the sharepoint api is way faster.

     

    Anyways, here is the script:

    Function Enable-SPFeatureOnAllSubwebs([string]$FeatureID,
               [string]$FeatureName,
               [string]$SiteURL,
               [switch]$help,
               [switch]$debug)
    {
     if($help)
     {
      Enable-SPFeatureOnAllSubWebs-Help
      return
     } # end if
     if(-not (($FeatureID) -and -not ($FeatureName)) -and -not ($SiteURL))
     {
      Write-Host "No Parameters given. Please use the -FeatureID or -FeatureName parameter aswell as -SiteURL. Type enter Enable-SPFeatureOnAllSubWebs -help if you require assistance."
      return
     }
     if(-not (($FeatureID) -and -not ($FeatureName)))
     {
      Write-Host "No feature specified. Please use the -FeatureID or -FeatureName parameter. Type enter Enable-SPFeatureOnAllSubWebs -help if you require assistance."
      return
     } # end if
     if(-not ($SiteURL))
     {
      Write-Host "No site collection specified. Please use the -SiteURL parameter. Type enter Enable-SPFeatureOnAllSubWebs -help if you require assistance."
      return
     }

     ## Reference to SharePoint DLL
     [System.Reflection.Assembly]::Load(“Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
     [System.Reflection.Assembly]::Load(“Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
     
     # Creating an URI variable to be used on the next method 
        $uri = New-Object Uri($SiteURL) 
     
     if([Microsoft.SharePoint.SPSite]::Exists($uri))
     { 
      $stsadm = "$env:programfiles\Common Files\Microsoft Shared\Web Server Extensions\12\BIN\STSADM.EXE"
      
      $spsite= new-object Microsoft.SharePoint.SPSite($SiteURL);
       
      $Feature = GetFeature -FeatureID $FeatureID -FeatureName $FeatureName
         
      if($Feature.Scope -eq [Microsoft.SharePoint.SPFeatureScope]::Web)
      {
       Write-Host "You can only use web scoped features with this command."
       return
      }
      
      foreach($web in $spsite.AllWebs)
      {
        $websiteurl= $web.url
       
       try
       {   
        [Microsoft.SharePoint.SPFeature]$activatedFeature = $web.Features.Add($Feature.Id, $true)
        
    #    if(-not $activatedFeature -or -not ($activatedFeature.Definition))
    #    {
    #     Write-Host "Failed to resolve feature after activation"
    #    }
    #    else
    #    {
    #     Write-Host "Activated feature $Feature.ID on $websiteurl"
    #    }
       }
       catch [System.Exception]
       {
        if($debug)
        {
         Write-Host $_.Exception.ToString()
        }
        Write-Host "Failed to activate feature $Feature on $websiteurl"
       }
       
       $web.Dispose()
       
       #&stsadm -o activatefeature -name $Feature -url $websiteurl
       #Enable-SPFeature -Identity $GUID -Url $websiteurl -Force
      } # end foreach
      
      $spsite.Dispose()
     }
     else
     {
      Write-Host "Url doesnt exist."
     } # end else
    } # end Enable-SPFeatureOnAllSubwebs

    Like I said, everything else works. Its just that one line, it thinks the function doesnt exist and im not sure why.

    Monday, February 27, 2012 4:00 PM
  • @Will Steele I didnt know about that.

    It gives me a surprisingly short list of methods and properties. Definitely not those that are associated with an SPFeatureCollection.

    This is awfully dissapointing.

    This is what I get:

    TypeName : Microsoft.SharePoint.SPFeature

    Name : Equals

    MemberType : Method

    Definition : bool Equals(System.Object obj)

     

    TypeName : Microsoft.SharePoint.SPFeature

    Name : GetHashCode

    MemberType : Method

    Definition : int GetHashCode()

     

    TypeName : Microsoft.SharePoint.SPFeature

    Name : GetType

    MemberType : Method

    Definition : type GetType()

     

    TypeName : Microsoft.SharePoint.SPFeature

    Name : ToString

    MemberType : Method

    Definition : string ToString()

     

    TypeName : Microsoft.SharePoint.SPFeature

    Name : Definition

    MemberType : Property

    Definition : Microsoft.SharePoint.Administration.SPFeatureDefinition Definition {get;}

     

    TypeName : Microsoft.SharePoint.SPFeature

    Name : DefinitionId

    MemberType : Property

    Definition : System.Guid DefinitionId {get;}

     

    TypeName : Microsoft.SharePoint.SPFeature

    Name : Parent

    MemberType : Property

    Definition : System.Object Parent {get;}

     

    TypeName : Microsoft.SharePoint.SPFeature

    Name : Properties

    MemberType : Property

    Definition : Microsoft.SharePoint.SPFeaturePropertyCollection Properties {get;}


    However, if I call [Microsoft.SharePoint.SPFeatureCollection].GetMethods() I get the list of methods, and I see two Add methods, both return an SPFeature.
    • Edited by js_consult Monday, February 27, 2012 4:54 PM
    Monday, February 27, 2012 4:34 PM
  • "$web.Features" is type of  "Microsoft.SharePoint.SPFeature" and its not collection. Thats the reason you are getting error at that particular line of code.

    Refer this link: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfeature_members.aspx


    Thanks & Regards
    Bhavik Solanki

    Please click “Mark as Answer” if this post answers your question and click "Vote as Helpful if this Post helps you.


    Tuesday, February 28, 2012 5:38 AM
  • Additionally, try something like this. This should work

    $spsite.Features.Add($Feature.Id, $true)
    

    Refer this link: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsite_members(v=office.12).aspx

    Hope this help...!!!


    Thanks & Regards
    Bhavik Solanki

    Please click “Mark as Answer” if this post answers your question and click "Vote as Helpful if this Post helps you.

    Tuesday, February 28, 2012 6:16 AM