Posted by: R Manimaran | July 21, 2010

Creating /Extending Custom STSADM command in SharePoint

SharePoint‘s command line tool stsadm.exe located in 12Hive/bin folder has set of useful operations and properties. Apart from the default operation we can extend the STSADM operation my writing our own code and achieve the operations.

Here is the step by step procedure to create a sample operation.

  • In Visual Studio 2008 create a class library project.
  • Change the namespace and class as you wish.
  • Add reference to Microsoft.Sharepoint.dll.
  • Add the following using statements in the class file.
  • using Microsoft.SharePoint;
  • using Microsoft.SharePoint.StsAdmin;
  • Inherit the class with the interface ISPStsadmCommand
  • Implement the interface methods
    • public string GetHelpMessage(string command)
    • public int Run(string command, System.Collections.Specialized.StringDictionary keyValues, out string output)
  • In GetHelpMessage method, return the help string about how to use the operation in stsadm command

  • public string GetHelpMessage(string command)

    {

    command = command.ToUpperInvariant();

    switch (command)

    {

    case "ENUMERATEFEATURES":

    return "-url <URL  of the Sharepoint site>";

    default:

    throw new InvalidOperationException();

    }

    }

  • Run method is the main method which gets called on calling the operation. Call our enumerate feature methods to list the features.

  • public int Run(string command, System.Collections.Specialized.StringDictionary keyValues, out string output)
    {
    command = command.ToUpperInvariant();
    switch (command)
    {
    case "ENUMERATEFEATURES":
    return this.EnumerateFeatures(keyValues, out output);
    default:
    throw new InvalidOperationException();
    }
    }

    • Construct  the EnumerateFeatures method


    private int EnumerateFeatures(
    System.Collections.Specialized.StringDictionary keyValues,
    out string output)
    {
    if (!keyValues.ContainsKey("url"))
    throw new InvalidOperationException("The URL parameter was not specified.");

    string URL = keyValues["url"];
    SPFeatureCollection features = null;
    StringBuilder builder = new StringBuilder();
    try
    {
    using (SPSite site = new SPSite(URL))
    {
    using (SPWeb web = site.OpenWeb())
    {
    features = web.Features;
    builder.AppendLine("**************************************");
    builder.AppendLine("Web Features in '" + web.Url + " ':\n");
    builder.AppendLine("**************************************\n");
    foreach (SPFeature feature in features)
    { builder.AppendLine(feature.Definition.DisplayName );
    } } } }
    catch (Exception ex)
    {
    throw new InvalidOperationException("Error Searching Url'" + URL
    + "'. Check the URL format and ensure site exists");
    }
    output = builder.ToString();
    return 0;
    }

  • Sign the project with the .sn key.
  • Build the project.
  • To make our operation available in stsadm we need to create an commands file and needs to place in 12Hive\Config folder.
  • Create a new xml file named “stsadmcommands.extendEnum.xml”.
  • Here stsadmcommands should not be changed. It should be present.
  • extenEnum is the Unique name on that folder.
  • Copy the dll to GAC and stsadmcommands.extendEnum.xml to 12Hive\Config folder.
  • Now open the command prompt and root to 12Hive/Bin folder.
  • Type stsadm –o EnumerateFeatures –url http://MossServer:81/sites/Sample
  • The list of features will be displayed in the console.
  • Advertisements

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s

    Categories

    %d bloggers like this: