Hi,
STSADM commands are a rich set of tools to carry out various administrative tasks in any sharepoint environment. Microsoft provides various out of box commands to carry out such tasks. This can be referred at :
However what to do if we need to carry out some task which is not provided by these out of box STSADM commands. In this article I will show you how to create your own STSADM commands. These are very simple to create and deploy. We will create a STSADM command that will provide us webTemplate and webTemplate used by a site, our command will look like STSADM –o getwebtemplate –url . Lets start doing this by creating a .Net assembly project in C# I am using visual studio here.

When the project is ready to be worked on follow these steps:

1. Add a reference to Microsoft.SharePoint.dll this can be located here C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\ISAPI
2. Add these references in your code:

using Microsoft.SharePoint;
using Microsoft.SharePoint.StsAdmin;
using System.Collections.Specialized;
We will use SPWeb and SPSite objects in our code hence the first refernce is required. We will extend an interface
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
public interface ISPStsadmCommand


You can get more information about this interface here:
Hence to extend this interface we need to use the second reference usingMicrosoft.SharePoint.StsAdmin;

We have included using System.Collections.Specialized because we are going to use StringDictionary.
Now here is our entire code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.StsAdmin;
using System.Collections.Specialized;
 
namespace RahulSTSADM
{
    public class RahulSTSADM :ISPStsadmCommand
    {
        protected string GetHelpMessage(string message)
        {
            return "-url <The full url of the site whose tempalte details you want to know>" ;
 
        }
        protected int Run(string command, StringDictionary keys,out string result)
        {
            if (!keys.ContainsKey("url"))
            {
                throw new InvalidOperationException("The url of the site is not provided");
            }
            string url = keys["url"];
 
            try
            {
                using (SPSite site = new SPSite(url))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                         string webTemplate = web.WebTemplate;
                         int webTemplateID = web.WebTemplateId;
                         web = null;
                         result = "The site at the url " + url + " is using the template " + webTemplate + " and the template ID is " + webTemplateID.ToString();
                                       }
                }
                return 0;
            }
            catch (Exception e)
            {
                throw new InvalidOperationException(e.Message);
            }}
 
string ISPStsadmCommand.GetHelpMessage(string command)
{
return this.GetHelpMessage(command);
}
int ISPStsadmCommand.Run(string command, StringDictionarykeyValues, out string
output)
{
return this.Run(command, keyValues, out output);
}
 
        }
 
       
    }

Now we will go through the code.

I have written this method:

protected string GetHelpMessage(string message)
       {
           return "-url <The full url of the site whose tempalte details you want to know>" ;
 
       }

This is used by the STSADM command line utility to provie help message about the operation. If you execute STSADM –help getwebtemplate you will get the output as -url <The full url of the site whose tempalte details you want to know> . Hence this method is used to return the help text that is being return by the STSADM utility.

Now the next method is very important where real action takes place. Have a look at the syntax:

protected int Run(string command, StringDictionary keys, outstring result)

In the above method the StringDictionary object is very important this stores the values of all parameters we supply in the command. Hence in the next section of this mehod we first verify whether it contains the necessary parameter or not. If not we are throwing an exception that will halt the execution of this command. The result is the string message that we provide once the command execution is completed.

No once the parameters are verified we are moving to the try block where I have placed the logic to get the webtemplate of the site. We have wrapped this in try catch block to show exception to the command window if anything goes wrong.

No this assembly needs to be signed and then should be deployed to the GAC. Now to use this as a STSADM command we need to create an xml file. The contents should be like this:

<?xml version="1.0" encoding="utf-8" ?>
<commands>
<command
name="getwebtemplate"
class="RahulSTSADM.RahulSTSADM, RahulSTSADM, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=f7d7392d7f65f4ef" />
</commands>


Now this should be saved at this location C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\CONFIG. The filename should be stsadmcommands.getwebtemplate.