How To: Custom STSADM Commands 

Here's a brief rundown on how to create a custom stsadm command.

  1. Create your command class. You must implement the Microsoft.SharePoint.StsAdmin.ISPStsadmCommand interface which consists of 2 methods, GetHelpMessage and Run. Inside the run method you have full control of the stsadm thread running in the end user's security context. You can perform any SharePoint API actions, connect to external databases, etc. The sky is the limit. Here's an example.
    public class SomeCommand : ISPStsadmCommand {
        public string GetHelpMessage(string command) { 
            return "stsadm\n -o somecommand -url [<Url>]"; 
        }
    
        public int Run(string command, StringDictionary args, out string output) {        
            // Do some stuff here with the SharePoint API        
            return 0;
        }
    }
    The args parameter is a name value pair where the key is the command line option and the value is the argument to that options. For example: stsadm -o somecommand -url http://spsimon.blogspot.com  would yield url as a key and http://spsimon.blogspot.com as a value. The output parameter is a reference to a string that the frame work will write to the screen when the command is complete. If you have invalid input or an error occurs, you will need to se the output value accordingly.

  2. Register your command with STSADM. Now we need to tell stsadm.exe how to run our custom command. The xml stsadm configuration files are located in the 12\Config directory and each file begins with "stsadmcommands". To register our class we might create an xml file called stsadmcommands.somecommand.xml. Feel free to open the other xml command definition files in this directory. stsadm finds these files and associates the corresponding command name with your custom command class. The command assembly must be in the Global Assembly Cache. Here's an example.
    <command name="somecommand"
        class="Commands.SomeCommand, Commands, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a1df43608d33992f" />
Comments
No Comments Available
Add a New Comment
Name

Email Address

Url

Comment