I am a self-confessed command line junkie.

Sure I see value in GUIs, and GUIs are great to get accustomed to a tool, but once I start crawling, I like to walk, run, and then fly. And when I fly, a GUI’s sluggishness in getting tasks done becomes seriously annoying.

In this article, I’ll introduce you to my best friend in SharePoint land, Stsadm.exe. No, this article is not a reference on Stsadm, neither is it an introduction to SharePoint. Instead, I introduce you to a few neat tricks using Stsadm that I like to keep in my back pocket.

Managing Sites and Site Collections

SharePoint organizes its data in sites and site collections. When you design a site for a client, you must evaluate a number of pros and cons as you choose between a site and a site collection for a particular set of data. Irrespective of what you picked, you’ll encounter times where you wished you had picked the other. Or sometimes, you just need to shuffle or move things around because the requirements changed, the databases grew, or any other reason. Stsadm is incredibly helpful in such scenarios.

When you import and export single sites, you need to pay extra attention to site GUID identifiers and versions of items/documents in the site.

You may backup a site collection using the following command:

stsadm -o backup -url <siteurl>
-filename <filename>

Similarly, you may restore a site collection using the following command:

stsadm -o restore -url <siteurl>
-filename <filename>

Now note that I mentioned “site collection” and not site. In order to backup a site, you may use the following command:

stsadm -o export -url <siteurl>
-filename <filename>

Similarly, in order to import a site you may use the following command:

stsadm -o export -url <siteurl>
-filename <filename>

To import and export single sites need you to pay extra attention to site GUID identifiers, and versions of items/documents in the site.

Now, this brings up some interesting permutations and combinations. Using the above, you could:

  • Move site collections to/from servers, or within the same website.
  • Move sites up or down in the hierarchy of a site collection
  • Convert a site into a site collection.

Now, why would you ever want to convert a site into a site collection? Because, you cannot split a site collection between multiple content databases, and sometimes content databases get too huge to manage. As it turns out, Stsadm can also help you manage your content databases.

Managing Content Databases

Sometimes you might land into a project where you have a content database that is bursting its seams. Usually if a content database grows over 50GB, a SharePoint architect’s warning lights should turn yellow. If it crosses 200GB for high I/O sites, or 500GB for read-only sites, the warning lights should glow red. Note that you can tweak these soft limits beyond the numbers I mentioned, but sooner or later, you must face the problem of taking an existing website with numerous sites or site collections, and splitting them up into manageable chunks of numerous content databases.

In order to do so, you may use the following command to force create a new site collection in a new content database.

stsadm -o createsiteinnewdb
-url <newsiteurl>
-owneremail <owneremail>
-ownerlogin <ownerlogin>
-databasename <newcontentdb>

After the above command has run successfully, you may verify under Central Administration > Application Management > Content Databases that indeed a new content database has been added, and a single site collection specified in the command above, has been created.

Now, you can use the following sequence of commands to move a site collection from <oldUrl> to <newUrl>. The <newURL> being the URL for the site collection above.

stsadm -o backup -url <oldUrl>
-filename <filename>
    
stsadm -o deletesite -url <oldUrl>
    
stsadm -o restore -url <newUrl>
-filename <filename> -overwrite

As you can see, I backed up the site from the old URL, deleted it, and restored it at the new URL, which exists in a new content database. All of this via Stsadm which supports a number of other commands that let you work with content databases. I recommend reading the MOSS SDK on the various possibilities.

Speeding up Your Development

If only I had a penny for every second spent in front of the SharePoint spinner. It’s true! I spend a lot of my life watching progress bars. For instance, the “my retirement” application is stuck on 2% for a very long time now. I use Stsadm to help speed up my development in two main ways.

The AutoPilot Mode

While Stsadm won’t speed up SharePoint, it will let you script common and repetitive tasks so you can execute a command and go grab a cup of coffee while the command executes.

For instance, the following sequence of commands deletes whatever I have done on a given URL, and recreates the application and site for me based on an out-of-the-box template.

stsadm -o deletesite -url <oldUrl>
    
stsadm -o createsite -url <newsiteurl>
-owneremail <owneremail>
-ownerlogin <ownerlogin>
-sitetemplate <templatename>

I know what you are thinking! What good is the above sequence of commands if your starting point always has to be one of the built-in site definitions?

Well, whenever I am ready to save a snapshot of my work that I’d like to revert to frequently, I export it as a site template. Then I use stsadm -o addtemplate to add that to a site on a different port, and then I use some backup/restore wizadry, all via Stsadm, to revert me right back to the snapshot after I have run the script above. I leave it up to you to write such a script. You might ask, why not just use backup and restore? Well, sometimes I’d rather see my custom features deployed, just as I would deploy them in production, so I tend to script out Stsadm, just as if I was running it in production.

As I mentioned, this doesn’t speed up SharePoint, but while this script runs, it gives me an opportunity to feverishly check my e-mail and see if I won any Internet lotteries. Maybe that will bump my retirement progress bar a bit?

The Time-Squishing Mode

Frequently I find myself writing code or developing functionality against some timer job within SharePoint that runs every few minutes, or worse, every few days or longer. I could keep twiddling with the system date and time to kick the job in, but my virtual machines like to sync their local time with the host time. If I changed both my host time and the virtual machine time, I’d find myself confusing all my co-workers with my e-mails because I’d end up sending an e-mail without fixing my system time.

Lucky for me, I found a mechanism to squish time. Well, not quite! I found myself a way to squish such timer jobs to a schedule that runs every few minutes. I’ve written a generic way of squishing time for any service/job which you can find posted on my blog at http://blah.winsmarts.com/2007-6-Developer_tip_-_Testing_SharePoint_timer_jobs_in_a_development_environment.aspx.

However, if all you wished to do is to change the schedule of information management policy to minutes, instead of daily, you could use the following Stsadm command.

stsadm -o setpolicyschedule -schedule
"every 1 minutes between 0 and 59"

Now the information management policy will run every 1 minute, so you can test your settings without waiting until tomorrow. Similarly, if you wish to change the frequency at which SharePoint sends alerts from every 5 minutes to 1 minute, you could use the following Stsadm command.

stsadm -o setproperty -propertyname
job-immediate-alerts -url <url> -
propertyvalue "every 1 minutes
between 0 and 59"

Feel free to explore what other properties exist and how you can use them.

Writing Custom Stsadm Commands

Last but not the least, while Stsadm has been around for a while, SharePoint 2007 comes with a number of new Stsadm commands. While Stsadm out of the box won’t do everything you need it to, luckily you can extend Stsadm and write custom operations for it. You can find further details at http://sharepointsolutions.blogspot.com/2006/09/extending-stsadmexe-with-custom.html.

Conclusion

I hope you enjoyed meeting my best friend in SharePoint land, the Stsadm.exe command. This article didn’t even scratch the surface of this iceberg. You can use Stsadm for a whole lot more. For instance, you could ease your deployment pains by packing your code in features, and activating them via the command line. You could deploy your code as solutions. Or you could manage your shared service providers. I highly encourage you to check out Stsadm.exe-it will definitely make your SharePoint life simpler.