Ensure_Feature_Deactivated__SPWeb

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

Function Ensure_Feature_Deactivated__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 }) -ne $null) {
        Write-Output([System.String]::Format("Feature [{0}] : Deactivating", $tFeatureId))

        $URL_ManageFeatures = [System.String]::Format("{0}/{1}?FeatureId={2}&Scope=Web&FeatureDefinitionScope=Farm",
                                                       $tWeb.Context.Url.TrimEnd('/'),
                                                       "/_layouts/DeactivateFeature.aspx".TrimStart('/'),
                                                       $tFeatureId.ToString().ToLower()
                                                     )

        $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] }


        $__EVENTTARGET  = "ctl00`$PlaceHolderMain`$lnkbtnDeactivate"
        $__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_Deactivated__SPWeb $tWeb ( New-Object System.Guid("63e65207-0c9b-4d2b-9dec-2ef2beb10a97") )


Created 2014-02-27
comments powered by Disqus
Login