Monday, November 16, 2009

Automate Activation/Deactivation of SharePoint Features

Click, wait, click, click, wait, click, wait, wait, click, wait, … Life as someone working with SharePoint development when you’re trying to update features you’re working on or testing. It sucks.

Here’s a little Watir on Ruby script to ease your pain. You can launch this from the command line with a “-a” or “-d” arg to activate or deactivate. Edit the @siteroot and @*Features variables to match your needs.

This uses some XPath-fu to find the Activate or Deactivate button for the feature named in the two *Features variables.

No, it’s not pretty. Yes, there are <x> different more betterer ways it could be done. It skinned the cat I needed skinned and I’m happy.

UPDATED: Refactored it a bit.

require 'watir'
require 'optparse'

@siteroot = "http://w2k3rs/"
@collectionFeatures = [ "Telligent Enterprise Menu Items",
            "Telligent Enterprise WebParts"]
@siteFeatures = ["SharePoint Integration for Telligent Enterprise",
            "Telligent Enterprise Search Replacement for WSS v3" ]

$options = {}

opts = OptionParser.new 
opts.banner = "Usage: SpiFeatures [$options]"
    
$options[:activate] = false
opts.on( "-a", "--activate", "Activate SharePoint Integration" ) do
    $options[:activate] = true
end
$options[:deactivate] = false
opts.on( "-d", "--deactivate", "Deactivate SharePoint Integration" ) do
    $options[:deactivate] = true
end
opts.parse(ARGV)    
   

$action = "Activate" if $options[:activate]
$action = "Deactivate" if $options[:deactivate]

$browser = Watir::Browser.start @siteroot + "/_layouts/ManageFeatures.aspx?Scope=Site"
$browser.speed = :fast

def toggle_feature(feature)
    $browser.button(:xpath, 
        "//table[@class='ms-propertysheet']//table//table//tr[td[h3[contains(.,'" + 
        feature +
        "')]]]/../../../..//input[@value='" + 
        $action + "']").click
    if $options[:deactivate] then 
        $browser.link(:text, "Deactivate this feature").click 
    end
end

@collectionFeatures.each do |@feature|
    toggle_feature(@feature)
end

$browser.goto(@siteroot + "/_layouts/ManageFeatures.aspx")

@siteFeatures.each do |@feature|
    toggle_feature(@feature)
end

$browser.close

2 comments:

  1. What is this? Are you actually writing real code (even if it is for testing)????

    ReplyDelete
  2. @Jon

    Dunno if you'd call anything I write "real" code... :D

    ReplyDelete