April 24, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Extend Your SharePoint Control with Custom Stsadm Commands, Page 3

  • June 18, 2009
  • By Gustavo Velez
  • Send Email »
  • More Articles »

Adding a Recursive Site Delete Command

The second class method solves a default SharePoint issue: If there is a main web site with sub-sites, it is impossible to delete the main site directly. Instead, you must delete each sub-site before deleting the main site. This can be a messy and lengthy process if there are many sub-sites. The EliminateWebRecursively method deletes sub-sites recursively:

private int EliminateWebRecursively(
   StringDictionary keyValues, out string output)
{
   if (!keyValues.ContainsKey("url"))
   {
      throw new InvalidOperationException(
         "The URL parameter is not specified");
   }
   String myUrl = keyValues["url"];
   SPSite mySite = null;
   SPWeb myWeb = null;
   StringBuilder myOutput = new StringBuilder();
   try
   {
      mySite = new SPSite(myUrl);
      myWeb = mySite.OpenWeb();
      if (myWeb == null)  
         myOutput.AppendLine(
         "Web '" + myUrl + "' not found");
      else
      {
         if (mySite.Url == myWeb.Url)
         {
            throw new InvalidOperationException(
                  "Web '" + myUrl + "' not found");
         }
         RecursionEliminateWebs(myWeb.Webs);
         if (myWeb.Url == myUrl)
            myWeb.Delete();// This is the Top level Web 
      }
   }
   catch (Exception ex)
   {
      throw new InvalidOperationException(
         "Error searching the URL '" + myUrl + 
         "'. Check the URL format and ensure that " +
         "the site exists." + "Details: " + ex.Message);
   }
   finally
   {
      if (mySite != null) mySite.Dispose();
      if (myWeb != null) myWeb.Dispose();
      myOutput.AppendLine("EliminateWebRecursively is finished");
   }
   output = myOutput.ToString();
   return 0;
}

As you can see, the structure is very similar to the first method. It checks to ensure that the StringDictionary contains a url key and associated value, and that the value points to a valid site. After passing these checks, it calls the RecursionEliminateWebs function to eliminate all the sub-sites from the main site, from top to button:

private void RecursionEliminateWebs(SPWebCollection AllWebs)
{
   foreach (SPWeb oneWeb in AllWebs)
   {
      if (oneWeb.Webs.Count > 0)
      {
         RecursionEliminateWebs(oneWeb.Webs);
      }
      oneWeb.Delete();
   }
}

After the recursion completes, the calling code deletes the main site. The final block, again, disposes of the created objects and generates a success message.

Compile the class, signing it with a safe name, and copy the DLL to the Global Assembly Cache (GAC). To ensure that stsadm treats the code as one of its own operations, create an XML file in the directory C:\Program Files\Common Files\Microsoft shared\Web Server Extensions\12\CONFIG. The name of the file must have the form stsadmcommands.[UniqueName].xml. The example uses the name stsadmcommands.ToolsSpsadm.xml. The [UniqueName] portion may be any string or GUID and cannot be repeated in the directory. The file indicates the operation name and the class that contains the code. For this example, the XML file would look like this:

<?xml version="1.0" encoding="utf-8" ?>
<commands>
 <command
   name="EnumerateFeatures"
   class="ToolsSpsadm.ClassToolsSpsadm, ToolsSpsadm, 
      Version=1.0.0.0, Culture=neutral, 
      PublicKeyToken=98de5564275127d9"/>
 <command
   name="EliminateWebRecursively"
   class="ToolsSpsadm.ClassToolsSpsadm, ToolsSpsadm,
      Version=1.0.0.0, Culture=neutral,
      PublicKeyToken=98de5564275127d9"/>
  </commands>

Replace the name, class, Version, and PublicKeyToken with the appropriate values for your own code. At this point, stsadm should recognize the new methods and display the new operations in its list.

Author's Note: Avoid modifying the default XML file with custom command registration, because SharePoint Service Packs can overwrite that file, thus deleting your custom registration information.

One way to simplify the process is to create a custom solution to install the tools in SharePoint. The solution manifest copies the assembly to the GAC, and the XML configuration file to the correct directory:

<?xml version="1.0" encoding="utf-8" ?> 
<Solution xmlns="http://schemas.microsoft.com/sharepoint/" 
  SolutionId="5470D10C-573C-6ba3-920D-ABCD2DC73325"
  DeploymentServerType="WebFrontEnd">
  <Assemblies>
    <Assembly DeploymentTarget="GlobalAssemblyCache" 
      Location="ToolsSpsadm.dll" />
  </Assemblies>
  <RootFiles>
    <RootFile Location="CONFIG\stsadmcommands.ToolsSpsadm.xml"/>
  </RootFiles>
</Solution>

Install the Solution using stsadm and activate it from SharePoint Central Administration, or by using stsadm again.

As you can see, stsadm is the tool par excellence for SharePoint administrators and developers, letting them manage and configure the system in a swift and straightforward manner. Stsadm is not an interactive tool—it has no interface—but it works well as a command-line program, and may be used in combination with scripts to automate SharePoint tasks. By default, stsadm contains an extensive list of commands that cover almost all administrative tasks, but as this article has shown, if you find yourself performing tasks that stsadm lacks, you can create custom commands to fulfill those needs.

About the Author

Gustavo Velez is a Manager in the Solutions Development department at Avanade Netherlands, a global consultancy company. He has many years experience developing Windows and Office applications, and more than seven years of daily programming experience with SharePoint. The author's articles can be found in many of the leading trade magazines in English, Dutch, German and Spanish. He is also pleased to be Webmaster of http://www.gavd.net, the only Spanish-language site dedicated to SharePoint. Spanish-language readers may want to consult Velez's book, Programación con SharePoint 2007. His latest book, Workflows and SharePoint: Going with the Flow went on the market in March, 2009. Velez has been awarded MVP status for SharePoint MOSS.





Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel