Ensure_Feature_Activated__SPWeb

SharePoint 2010's client object model doesn't address the ability to activate an SPWeb-scoped feature (it does handle SPSite scoped)... this function performs the activation via web requests (by emulating a users' browser)
Language:
PowerShell
Keywords:
Code Snippet

Function Ensure_Feature_Activated__SPWeb[Microsoft.SharePoint.Client.Web]$tWeb
                                        , [System.Guid]$tFeatureId
                                        ) {

    [Helper]::Context_Load($tWeb.Context, $tWeb)
    [Helper]::Context_Load($tWeb.Context, $tWeb.Features)
    $tWeb.Context.ExecuteQuery()

    if (($tWeb.Features | where { $_.DefinitionId -eq $tFeatureId }) -eq $null) {
        Write-Output([System.String]::Format("Feature [{0}] : Activating", $tFeatureId))

        $URL_ManageFeatures = $tWeb.Context.Url.TrimEnd('/'+ "/_layouts/ManageFeatures.aspx"

        $wc = New-Object System.Net.WebClient
        $wc.Credentials = [System.Net.CredentialCache]::DefaultCredentials
        $pageContents = $wc.DownloadString($URL_ManageFeatures)
        $wc.Dispose()

        # get FormDigest token per http://www.sbrickey.com/Tech/Blog/Post/Mission_Impossible_Remotely_terminating_a_SharePoint_Workflow
        $regex_Pattern_RequestDigest = "<input type=`"hidden`" name=`"__REQUESTDIGEST`" id=`"__REQUESTDIGEST`" value=`"(.*)`" />"
        if ($pageContents -match $regex_Pattern_RequestDigest -eq $true)
        { $__REQUESTDIGEST = $Matches[1] }

        $regex_Pattern_EventValidation = "<input type=`"hidden`" name=`"__EVENTVALIDATION`" id=`"__EVENTVALIDATION`" value=`"(.*)`" />"
        if ($pageContents -match $regex_Pattern_EventValidation -eq $true)
        { $__EVENTVALIDATION = $Matches[1] }

        $regex_Pattern_ViewState = "<input type=`"hidden`" name=`"__VIEWSTATE`" id=`"__VIEWSTATE`" value=`"(.*)`" />"
        if ($pageContents -match $regex_Pattern_ViewState -eq $true)
        { $__VIEWSTATE = $Matches[1] }


        # identify the "Activate" button control name for the correct feature
        $regex_Pattern_DivId = "<div(.*)id='" + $tFeatureId.ToString().ToLower() + "'(.*)>(\s*)<input type=`"button`" name=`"(.*)`" value=(.*)/>(\s*)</div>"
        if ($pageContents -match $regex_Pattern_DivId -eq $true)
        { $__EVENTTARGET  = $Matches[4] }

        $__EVENTARGUMENT  = ""
        
        
        ### POST BACK ###
        $webReq = [System.Net.HttpWebRequest]::Create($URL_ManageFeatures)
        $webReq.Method = "POST"
        $webReq.Credentials = [System.Net.CredentialCache]::DefaultCredentials
 
        $webReq_Contents = [System.String]::Format("__EVENTTARGET={0}&__EVENTARGUMENT={1}&__REQUESTDIGEST={2}&__EVENTVALIDATION={3}&__VIEWSTATE={4}",
                                                    [System.Web.HttpUtility]::UrlEncode( $__EVENTTARGET ),
                                                    [System.Web.HttpUtility]::UrlEncode( $__EVENTARGUMENT ),
                                                    [System.Web.HttpUtility]::UrlEncode( $__REQUESTDIGEST ),
                                                    [System.Web.HttpUtility]::UrlEncode( $__EVENTVALIDATION ),
                                                    [System.Web.HttpUtility]::UrlEncode( $__VIEWSTATE ) `
                                                  )
        $webReq_Contents_buffer = [System.Text.Encoding]::UTF8.GetBytes($webReq_Contents)
 
        $webReq.ContentType = "application/x-www-form-urlencoded"
        $webReq.ContentLength = $webReq_Contents_buffer.Length
 
        $webReq_Stream = $webReq.GetRequestStream()
        $webReq_Stream.Write($webReq_Contents_buffer, 0, $webReq_Contents_buffer.Length)
        $webReq_Stream.Flush()
        $webReq_Stream.Close()
 
        $webResp = $webReq.GetResponse()
 
        $webResp_Stream = New-Object System.Io.StreamReader($webResp.GetResponseStream())
        $webResp_Contents = $webResp_Stream.ReadToEnd()
    }
}

Example

Ensure_Feature_Activated__SPWeb $tWeb ( New-Object System.Guid("63e65207-0c9b-4d2b-9dec-2ef2beb10a97") )


Created 2014-02-27
comments powered by Disqus
Login