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.
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.
-
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.
-
Create a new C# class library using Visual Studio 2008.
-
Add references to the Microsoft.SharePoint.dll and System.Web and System.Xml .NET
assemblies.
-
Sign the assembly with a strong-name key file. This is required as the dll is required
to be installed in GAC.
-
Delete the Class1.cs from Project in Solution Explorer which the project template
has created.
-
Create new class file named CustomizeSTSADM.cs.
-
Open the new created class.
-
Add the using statements for Microsoft.SharePonit, System.Web and System.Xml.
-
Add implementation reference to the “ISPStsadmCommand” interface to the class.
-
Now write the following code to implement the GetHelpMessage method of the interface.
-
/// <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();
}
}
-
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.
-
/// <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();
}
}
-
The method named AddProjectTaskListtoSite performs the operation for adding new project
task list to the specified site.
-
Code for the AddProjectTaskListtoSite method is as follos.
-
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;
}
}
-
The method named AddNoticeToSite adds the notice to the default page of the provided
site.
-
Code for the AddNoticeToSite method is as follows.
-
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;
}
}
-
Now build the project and resolve the error(s) if there any.
-
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.
-
Now you are ready to test your new command using STSADM command utility.
-
Open the console window.
-
Navigate to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\bin
folder.
-
Type STSADM to see whether your custom commands are listed or not. You will see that
your commands are listed.
-
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!!!”
-
Open your site and see, the notice is added to the default.aspx page.
-
Same way you can test the other command and can also make your own commands which
perform complex operations as well.
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.