Codebanking.com Bank your code here...
HOME REGISTER TECHNICAL Q&A GENERAL Q&A SUBMIT CODE SNIPPET LOGIN
 
  How to Create Custom STSADM Command line Tool  
   
  STSADM Command line tool provides command prompt interface for administrators to manage SharePoint servers and sites. We can also extend stsadm utility tool to increase
OOB functionality and custom activity on SharePoint user Interface. Here we see how developers can create custom stsadm command utility and its deployment procedure and scenarios which can be implemented.
 
     
 

The STSADM utility enables many administrative operations in Windows SharePoint Services that cannot be done with the Central Administration application. With Windows SharePoint Services 3.0 you can extend the functionality of the STSADM utility by adding your own operations and command line parameters with simple projects using any .NET language.

Microsoft.SharePoint.StsAdmin namespace is used for the creation of stsadm command line which interacts with SharePoint API. Then we need to implement Interface ISPStsadmCommand which consists of two methods GetHelpMessage and Run .

 

public class class name : ISPStsadmCommand

{

 

public string GetHelpMessage(string command)

{

}

 

public int Run(string command, System.Collections.Specialized.StringDictionary keyValues, out string output)

{

 

}

}

 

The GetHelpMessage method returns a string value containing help and usage information for the custom operation.

 

The Run method will take a StringDictionary as argument from stsadm.exe which consists of key/value pairs containing all named parameters, values and flags input from the administrator. The Run method also takes an out type parameter which will be used to return message to console window during execution.

One class for each custom stsadm.exe operation, and each of these classes must implement the ISPStsadmCommand interface. Each class that implements the ISPStsadmCommand interface is required to contain a GetHelpMessage method and a Run method.

For Deployment we need a assembly which is our complied code has to place in Global assembly cache and a configuration file which is a simple xml file starts with <commands> element as root element and <command> as sub elements.

Command element has attributes which are name and class.

 

Name attribute is for operationcommand name and class indicates the class name, assembly name, version and token key.

 

Configuration files must follow the well known naming convention stsadmcommands.customcommands.xml where customcommands is arbitrary but must be unique. Through this naming convention for configuration files, stsadm.exe implements a pluggable architecture for declaring custom operations.

 

And Place this configuration file in 12 hives CONFIG folder. We can begin extending OOB stsadm.exe functionality with our own custom operations.

 

Scenario which we have used here is to change the OOB list's newpage, editpage and display page. There is no OOB command for changing those properties. And we can change using SharePoint designer in order to avoid the manual process we can use

this custom stsadm command.
 
     
  using Microsoft.SharePoint;
using Microsoft.SharePoint.StsAdmin;

namespace ListURLChangeCustomSTSADM
{
public class ChangeNewEditPageOfList : ISPStsadmCommand
{
#region Contatns
string URL = "url";
string LIST_NAME = "listname";
string CONTENTTYPE = "contenttype";
string NEW_PAGE = "newpage";
string EDIT_PAGE = "editpage";
string DISPLAY_PAGE = "displaypage";
string ERROR1 = "Please secify the URL & List Name";
string ERROR2 = "Error while accessing the List Content Type. Error Msg -";
#endregion

#region ISPStsadmCommand Members

public string GetHelpMessage(string command)
{
throw new Exception("The method or operation is not implemented.");
} public int Run(string command, System.Collections.Specialized.
StringDictionary keyValues, out string output)
{
if (keyValues.ContainsKey(URL) && keyValues.ContainsKey(CONTENTTYPE)
&& keyValues.ContainsKey(LIST_NAME))
{

ListHelper(keyValues, out output);

}
else
{
output = ERROR1;

}
return 0;
}

#endregion
#region Helper Method

private void ListHelper(System.Collections.Specialized.StringDictionary
keyValues, out string output)
{
output = string.Empty;
try
{
string url = keyValues[URL];
string listname = keyValues[LIST_NAME];
string contenttype = keyValues[CONTENTTYPE];
string newpageurl = string.Empty;
string editpageurl = string.Empty;
string displaypageurl = string.Empty;

if (keyValues.ContainsKey(NEW_PAGE))
{
newpageurl = keyValues[NEW_PAGE];
}
if (keyValues.ContainsKey(EDIT_PAGE))
{
editpageurl = keyValues[EDIT_PAGE];
}
if (keyValues.ContainsKey(DISPLAY_PAGE))
{
displaypageurl = keyValues[DISPLAY_PAGE];
}
using (SPSite site = new SPSite(url))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;

try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPList olist = web.Lists[listname];
olist.ContentTypes[contenttype].NewFormUrl = newpageurl;
olist.ContentTypes[contenttype].EditFormUrl = editpageurl;
olist.ContentTypes[contenttype].DisplayFormUrl = displaypageurl;
olist.ContentTypes[contenttype].XmlDocuments.
Delete("http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url");
olist.ContentTypes[contenttype].Update();
olist.Update();
});

}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
output = ERROR2 + ex.Message;
}
web.AllowUnsafeUpdates = false;

}
}
}
catch (Exception exCreateList)
{
Console.WriteLine(exCreateList.Message);
output = exCreateList.Message;
}
}
#endregion
}

 
     
  Configuration XML file

<?xml version="1.0" encoding="utf-8" ?>
<commands>
<command name="ChangeNewEditPageOfCalendarList" class="ListURLChangeCustomSTSADM.
ChangeNewEditPageOfList,ListURLChangeCustomSTSADM,Version=*.*.*.*,
Culture=neutral,PublicKeyToken=**************"></command>
</commands>

Note: Naming convention should be like this stsadmcommands.anycustomname.xml

 
     
     
  Note: After change the URL it automatically concatenate with the default querystring.  
  Need to configure file in this path.

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\CONFIG
 
     
 
 2010 codebanking.com      contactus@codebanking.com