So, this is a follow-up to a previous post on automating site collection where I gripe alot about how there's no provision for automating site creation. Guess I whined too soon, eh? This is the earlier post:
http://vanlaan.typepad.com/van_laan_on_sharepoint/2007/11/automating-site.html
So, all that griping aside, here's how to use a simple batch file and stsadm to create as many sites/subsites as you like.
Short Version:
- Create site template, save to template gallery.
- Install site template into global template gallery with command: stsadm -o addtemplate -title "TEMPLATETITLE" -filename FilenameLocation
- Create sites/subsites with command: stsadm -o createweb -url http://www.sandarbud.com/url/subsitename -sitetemplate _global_#0 -title sitename -description "Site Description" Can use a batch file and just have row after row of this command. Execute from cmd line to watch it process properly.
- One caveat here: You can certainly try to cut and paste cmd line commands from this post or any other web site. But if it doesn't work don't give up right away. Always, always try typing in the command manually, as spaces, dashes, and other characters don't always translate from html to the cmd prompt.
Long Version:
First, I'm emulating a client need here. The need is to create around 55 sites, all based on a custom template, on a single path beneath an existing site which hangs off of a site collection. The 55 sites names (and thus URLs) are all based on 3-letter codes that indicate for which city the particular site will be created. So, ATL for Atlanta, BOS for Boston, and so forth.
The first step was to modify an existing, simple team site and then Save that Site as a template in the site gallery. One thing I learned here is that sites cannot be saved as templates if the Publishing feature is turned on. Or at least that was what I experienced in this case. Turning on the Publishing feature (thus creating the Pages library with Approval turned on) removed the option from Site Actions, Site Settings to "Save Site as Template." Very strange. There's probably some logic behind that, but I can't hazard a guess as to what it would be.
So, anyways, Pusblishing feature off, I save the site as a template in the site gallery and then go to run my command from the command line which should create a site based on that template. I named the template "Stations" based on client needs. It looks nice and unique so it's obvious the sites created come from the template:
So the command to be run from the DOS prompt is:
stsadm -o createweb -url http://www.sandarbud.com/stations/ATL -sitetemplate stations -title "Atlanta Station" -description "Atlanta Station"
Huh, SharePoint doesn't seem to know what the file "stations" is, even though it's saved to my template gallery:
A little research leads me to the understanding that sites can only be created in this fashion if the site template has been uploaded to the global template gallery.
This can be verified by running the following command:
stsadm -o enumtemplates
Which, in my farm, returns:
Basically, just a previous global template I uploaded in earlier testing. But no stations template.
So, I need to upload my stations template to the global site template catalog.
This can be accomplished by first finding my template file in the site collection template gallery, right-clicking, and "save file as", just like you would any file off of a normal website:
Save the file in a nice easy path to reference from the command line. I chose to save my file as c:\temp\sitetemplate\stations.stp
Then, the template needs to be added to the global site template catalog via the following command:
stsadm -o addtemplate -title "TEMPLATETITLE" -filename FilenameLocation
Or, in my case:
stsadm -o addtemplate -title "Stations" -filename c:\temp\sitetemplate\stations.stp
Operation completed successfully--aslways like to see that. It also calls for an issreset, so going to input command iisreset -noforce now.
Now, my template should be available via the stsadm -o enumtempates command:
Ok, that's good results. As you can see, two templates were returned. One from an earlier template, and one from my recent stations template. Here's the output:
C:\Documents and Settings\sjomvl>stsadm -o enumtemplates
Dracula - Language: 1033 - Site Template: _GLOBAL_#0 - Template Id: 1
Stations - Language: 1033 - Site Template: _GLOBAL_#1 - Template Id: 1
Now, here's the interesting part. See that Site Template:_GLOBAL_#1 bit? That's the actual template name which SharePoint refers to to describe my Stations template! Not Stations, which would be nice and friendly, but _GLOBAL_#1! That's nice.
So, my command now becomes:
stsadm -o createweb -url http://www.sandarbud.com/stations/ATL -sitetemplate _GLOBAL_#1 -title "Atlanta Station" -description "Atlanta Station"
And I got an error:
"The Web site address "/stations/ATL" is already in use."
This is because my earlier attempt, while reserving URL and kind of creating the site, did reserve the space at /stations/ATL. If I navigate to that URL, I get a page telling me to choose my template. So, I go to Manage Content and Structure and navigate to that level and, sure enough, Atlanta Station exists. So I delete that site or site placeholder and then run my command again:
Operation completed successfully. Just what I like to see.
And the site is created and exists here:
Good times. Now to automate this process with good ol' notepad.
So, I'll take the above command and put it into notepad, then reproduce it 55 times over with different URLs, descriptions, and titles. This is what I came up with:
stsadm -o createweb -url http://www.sandarbud.com/stations/ATL -sitetemplate _GLOBAL_#1 -title "Atlanta Station" -description "Atlanta Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/BMI -sitetemplate _GLOBAL_#1 -title "Bloomington-Normal Station" -description "Bloomington-Normal Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/BOS -sitetemplate _GLOBAL_#1 -title "Boston Station" -description "Boston Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/BUF -sitetemplate _GLOBAL_#1 -title "Buffalo Station" -description "Buffalo Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/BWI -sitetemplate _GLOBAL_#1 -title "Baltimore-Washington International Station" -description "Baltimore-Washington International Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/CAK -sitetemplate _GLOBAL_#1 -title "Akron-Canton Station" -description "Akron-Canton Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/CHS -sitetemplate _GLOBAL_#1 -title "Charleston Station" -description "Charleston Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/CLT -sitetemplate _GLOBAL_#1 -title "Charlotte-Douglas Station" -description "Charlotte-Douglas Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/DAB -sitetemplate _GLOBAL_#1 -title "Daytona Beach Station" -description "Daytona Beach Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/DAY -sitetemplate _GLOBAL_#1 -title "Dayton Station" -description "Dayton Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/DCA -sitetemplate _GLOBAL_#1 -title "Reagan Station" -description "Reagan Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/DEN -sitetemplate _GLOBAL_#1 -title "Denver Station" -description "Denver Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/DFW -sitetemplate _GLOBAL_#1 -title "Dallas-Fort Worth Station" -description "Dallas-Fort Worth Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/DTW -sitetemplate _GLOBAL_#1 -title "Detroit Station" -description "Detroit Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/EWR -sitetemplate _GLOBAL_#1 -title "Newark Station" -description "Newark Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/FLL -sitetemplate _GLOBAL_#1 -title "Fort Lauderdale Station" -description "Fort Lauderdale Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/FNT -sitetemplate _GLOBAL_#1 -title "Flint Station" -description "Flint Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/GPT -sitetemplate _GLOBAL_#1 -title "Gulfport-Biloxi Station" -description "Gulfport-Biloxi Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/HOU -sitetemplate _GLOBAL_#1 -title "Houston Station" -description "Houston Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/HPN -sitetemplate _GLOBAL_#1 -title "Westchester County Station" -description" Westchester County Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/IAD -sitetemplate _GLOBAL_#1 -title "Dulles International Station" -description "Dulles International Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/ICT -sitetemplate _GLOBAL_#1 -title "Wichita Station" -description "Wichita Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/IND -sitetemplate _GLOBAL_#1 -title "Indianapolis Station" -description "Indianapolis Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/JAX -sitetemplate _GLOBAL_#1 -title "Jacksonville Station" -description "Jacksonville Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/LAS -sitetemplate _GLOBAL_#1 -title "McCarran Station" -description "McCarran Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/LAX -sitetemplate _GLOBAL_#1 -title "Las Vegas Station" -description "Las Vegas Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/LGA -sitetemplate _GLOBAL_#1 -title "La Guardia Station" -description "La Guardia Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/MCI -sitetemplate _GLOBAL_#1 -title "Kansas City Station" -description "Kansas City Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/MCO -sitetemplate _GLOBAL_#1 -title "Orlando Station" -description "Orlando Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/MDW -sitetemplate _GLOBAL_#1 -title "Midway Station" -description "Midway Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/MEM -sitetemplate _GLOBAL_#1 -title "Memphis Station" -description "Memphis Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/MIA -sitetemplate _GLOBAL_#1 -title "Miami Station" -description "Miami Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/MKE -sitetemplate _GLOBAL_#1 -title "Milwaukee Station" -description "Milwaukee Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/MLI -sitetemplate _GLOBAL_#1 -title "Moline Station" -description "Moline Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/MSP -sitetemplate _GLOBAL_#1 -title "Minneapolis-Saint Paul Station" -description "Minneapolis-Saint Paul Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/MSY -sitetemplate _GLOBAL_#1 -title "New Orleans Station" -description "New Orleans Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/PBI -sitetemplate _GLOBAL_#1 -title "Palm Beach Station" -description "Palm Beach Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/PHF -sitetemplate _GLOBAL_#1 -title "Newport News Station" -description "Newport News Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/PHL -sitetemplate _GLOBAL_#1 -title "Philadelphia Station" -description "Philadelphia Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/PHX -sitetemplate _GLOBAL_#1 -title "Phoenix Station" -description "Phoenix Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/PIT -sitetemplate _GLOBAL_#1 -title "Pittsburgh Station" -description "Pittsburgh Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/PNS -sitetemplate _GLOBAL_#1 -title "Pensacola Station" -description "Pensacola Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/PWM -sitetemplate _GLOBAL_#1 -title "Portland Station" -description "Portland Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/RDU -sitetemplate _GLOBAL_#1 -title "Raleigh-Durham Station" -description "Raleigh-Durham Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/RIC -sitetemplate _GLOBAL_#1 -title "Richmond Station" -description "Richmond Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/ROC -sitetemplate _GLOBAL_#1 -title "Rochester Station" -description "Rochester Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/RSW -sitetemplate _GLOBAL_#1 -title "Fort Myers Station" -description "Fort Myers Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/SAN -sitetemplate _GLOBAL_#1 -title "San Diego Station" -description "San Diego Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/SAT -sitetemplate _GLOBAL_#1 -title "San Antonio Station" -description "San Antonio Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/SAV -sitetemplate _GLOBAL_#1 -title "Savannah Station" -description "Savannah Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/SEA -sitetemplate _GLOBAL_#1 -title "Seattle-Tacoma Station" -description "Seattle-Tacoma Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/SFO -sitetemplate _GLOBAL_#1 -title "San Francisco Station" -description "San Francisco Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/SJU -sitetemplate _GLOBAL_#1 -title "San Juan Station" -description "San Juan Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/SRQ -sitetemplate _GLOBAL_#1 -title "Sarasota-Bradenton Station" -description "Sarasota-Bradenton Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/STL -sitetemplate _GLOBAL_#1 -title "Saint Louis Station" -description "Saint Louis Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/SWF -sitetemplate _GLOBAL_#1 -title "Newburgh Station" -description "Newburgh Station"
stsadm -o createweb -url http://www.sandarbud.com/stations/TPA -sitetemplate _GLOBAL_#1 -title "Tampa Station" -description "Tampa Station"
Wow. Let me just say that that was not a straightforward process. It involved notepad.exe, excel and lots of data entry-type busy work. That portion definitely needs some automation, and even in winword crtl find-replace I didn't really find it.
Suffice it to say, the above text was saved as stations.bat, and then executed:

And off it goes. Again, I ran the stations.bat file by navigating to that directory and running stations.bat from the cmd line directly, which offered me a line by line status, as opposed to navigating to the folder and double-clicking the file. This will give me a cmd prompt window on each line as it goes. Each site is being created one by one, and when it finishes, I have this:
Good times.
Caveats: The global site navigation comes up turned off by default, which is a bummer. Also, there's no means to provision security in this method--it comes up inheriting parent's security by default. Otherwise, good times.
Batch file:
Download Station.bat