Customize the STSADM Command

How to customze the STSADM command utility to add your custom commands. The article also tells you why you need to customize the STSADM command as it has a plenty of commands provided by Microsoft SharePoint.

Introduction

Each and every SharePoint Developer or SharePoint Administrator is familiar with STSADM command utility that is used to perform a variety of SharePoint Administrative functions from command prompt / Windows Console. But you might not know that you can add your custom commands to this utility. Here in this article I am going to explain you how to add custom command to the STSADM command with just a simple class library and an XML file.

You will think that, why we need STSADM command, as we can perform almost each and every administrative work from SharePoint Central Administration web site or using the standard Settings pages?
There are two reasons there. One is, this utility may be readily mastered by a system administrator who has a less knowledge or understanding of SharePoint. So, if you want to provide him with quick and simple access to some basic custom utilities, customizing STSADM may be just what you need. Also, STSADM provides a consistent interface across all commands, your customizations will be easier for the user to learn and remember.
The second reason is, STSADM command can be easily scheduled by using the Windows Task Scheduler. Though you should write console application, but customizing STSADM command enforces a consistency and simplicity that promotes easy way to use as well as improved maintainability.
So, in this article I’ll add to custom commands to the STSADM utility: one to add a new Project Tasks list and the second to add an announcement to the top of a web site’s default page. By these two examples you will see how easy to extend the STSADM command.

Assembly References

  • System.Web assembly

  • System.Xml assembly

  • Windows SharePoint Services assembly (Microsoft.SharePoint.dll)

Assumptions

I assume that reader if familiar with how to create class library project using Visual Studio 2008, also familiar with STSADM basic command. I’ve used C# as the programming language.

Starting up....

  1. Create and XML document called stsadmcommands.jatin.xml in the C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\Config folder to your SharePoint server. See the attached source code for xml file reference.

  2. Create a new C# class library using Visual Studio 2008.

  3. Add references to the Microsoft.SharePoint.dll and System.Web and System.Xml .NET assemblies.

  4. Sign the assembly with a strong-name key file. This is required as the dll is required to be installed in GAC.

  5. Delete the Class1.cs from Project in Solution Explorer which the project template has created.

  6. Create new class file named CustomizeSTSADM.cs.

  7. Open the new created class.

  8. Add the using statements for Microsoft.SharePonit, System.Web and System.Xml.

  9. Add implementation reference to the “ISPStsadmCommand” interface to the class.

  10. Now write the following code to implement the GetHelpMessage method of the interface.

  11. /// <summary>
    /// Implement the GetHelpMessage method to provide a response when user runs the new command
    /// with the -help flag.
    /// </summary>
    /// <param name="command"></param>
    /// <returns></returns>
    string ISPStsadmCommand.GetHelpMessage(string command)
    {
    command = command.ToLower();
    switch (command)
    {
    case "addprojecttasklisttosite":
    return "\n-site <full url to a site collection in SharePoint" +
    "\n-web <web name to add project tasks list to" +
    "\n-listname <name to give new project tasks list>" +
    "\n-description <description for new project tasks list>";
    case "addnoticetosite":
    return "\n-site <full url to a site collection in SharePoint" +
    "\n-web <web name to add the notice to" +
    "\n-text <text of notice to add>";
    default:
    throw new InvalidOperationException();
    }
    }

  12. Now write the following code to implement the Run method of the interface. This is the main method when a user executes and command and this method performs actual operations.

  13. /// <summary>
    /// Implement the run method to actually execute the commands defined in this class.
    /// </summary>
    /// <param name="command"></param>
    /// <param name="keyValues"></param>
    /// <param name="output"></param>
    /// <returns></returns>
    int ISPStsadmCommand.Run(string command, System.Collections.Specialized.StringDictionary keyValues, out string output)
    {
    command = command.ToLower();
    switch (command)
    {
    case "addprojecttasklisttosite":
    return AddProjectTaskListtoSite(keyValues, out output);
    case "addnoticetosite":
    return AddNoticeToSite(keyValues, out output);
    default:
    throw new InvalidOperationException();
    }
    }

  14. The method named AddProjectTaskListtoSite performs the operation for adding new project task list to the specified site.

  15. Code for the AddProjectTaskListtoSite method is as follos.

  16. private int AddProjectTaskListtoSite(System.Collections.Specialized.StringDictionary keyValues, out string output)
    {
    using (SPSite site = new SPSite(keyValues["site"]))
    {
    SPWeb web;
    if (keyValues["web"] == null)
    web = site.RootWeb;
    else
    web = site.OpenWeb(keyValues["web"]);
    web.AllowUnsafeUpdates = true;
    web.Lists.Add(keyValues["listname"], keyValues["description"], SPListTemplateType.GanttTasks);
    SPList list = web.Lists[keyValues["listname"]];
    list.OnQuickLaunch = true;
    list.Update();
    web.Update();
    web.Dispose();
    output = "";
    return 0;
    }
    }

  17. The method named AddNoticeToSite adds the notice to the default page of the provided site.

  18. Code for the AddNoticeToSite method is as follows.

  19. private int AddNoticeToSite(System.Collections.Specialized.StringDictionary keyValues, out string output)
    {
    using(SPSite site = new SPSite(keyValues["site"]))
    {
    SPWeb web;
    if (keyValues["web"] == null)
    web = site.RootWeb;
    else
    web = site.OpenWeb(keyValues["web"]);
    web.AllowUnsafeUpdates = true;
    SPLimitedWebPartManager webParts = web.GetLimitedWebPartManager("default.aspx",
    System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
    ContentEditorWebPart cewp = new ContentEditorWebPart();
    XmlDocument xmlDoc = new XmlDocument();
    XmlElement xmlElem = xmlDoc.CreateElement("Element");
    StringBuilder sb = new StringBuilder();
    sb.Append("<![CDATA[");
    sb.Append("<div align='center' ");
    sb.Append("style='font-family: Arial; font-size: medium;");
    sb.Append(" font-weight:bold;");
    sb.Append(" background-color:red; color: white; border: medium solid black;");
    sb.Append(" width: 50%; padding: 10px; margin: 10px;'>");
    sb.Append(keyValues["text"].ToString());
    sb.Append("</div>");
    sb.Append("]]>");
    xmlElem.InnerXml = sb.ToString();
    cewp.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.None;
    cewp.Content = xmlElem;
    webParts.AddWebPart(cewp,"Left",0);
    webParts.SaveChanges(cewp);
    web.Update();
    web.Dispose();
    output = "";
    return 0;
    }
    }

  20. Now build the project and resolve the error(s) if there any.

  21. Place your stsadmcommands.jatin.xml file to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\Config, and deploy your compiled assembly to GAC.

  22. Now you are ready to test your new command using STSADM command utility.

  23. Open the console window.

  24. Navigate to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\bin folder.

  25. Type STSADM to see whether your custom commands are listed or not. You will see that your commands are listed.

  26. Enter the STSADM addnoticetosite command with the following parameter values
    a. Site = http://jatinprajapati:20918 or your site address
    b. Web = Jatin
    c. Text = “This is my first custom developed stsadm command and I’m happy to make the announcement of this!!!”

  27. Open your site and see, the notice is added to the default.aspx page.

  28. Same way you can test the other command and can also make your own commands which perform complex operations as well.

You can download the workign source code from here.

Conclustion

This way we can customize the STSADM command which hlep us to deploy our SharePoint site or solution. This approach is very much useful when we are deploying the SharePoint Site or Solutions to multple servers or in the web farm. Hope you have enjoyed the article and will have get some nice imformation about STSADM command.



By Jatin Prajapati   Popularity  (1377 Views)
Picture
Biography - Jatin Prajapati
I think, most of the people are interested only in answers so no Biography provided... Want know more just write me at jatin.prajapati.er@gmail.com