forge

Creating SharePoint Sites from Global Templates with the Object Model

I recently had the pleasure of delivering the first SharePoint Workshop for Bamboo Solutions in Chicago, IL. One of the cool things about the project was the ability to put together what I believe are some of the best workshop materials out there. I may be biased (and the pope may be catholic) but I believe that the common thread that runs between the labs can really help you understand how all of the pieces of SharePoint fit together.

One of the challenging parts of putting together the workshop was building a tool which could quickly create the individual user ids and site collections for each of the students. One particularly quirky part was creating the site from a global site template through the object model. You see when you upload a template to a site collection and you want to create an instance of it via code you use templatename.stp in your code to refer to the template. This is what most folks document when demonstrating how to do it. However, because I needed to create separate site collections for the students, I couldn’t upload the site template that way, I needed to use STSADM –o addtemplate to add the template globally.

Help Your SharePoint User

When you upload a template globally, the template gets assigned an ID that starts with _GLOBAL_#0. The next template would be _GLOBAL_#1 and so on. None of this was new to me because I’ve used this format before. However, the interesting thing was that the code I wrote worked when I used one of the site definitions (STS#0) but not when I tried the template. In the end, I realized that the account I was using didn’t have access to the site. I had installed SharePoint with a service account and I was logged back in as the administrator. The administrator account didn’t have an entry in the Policy for Web Applications – once I put a policy entry in for the administrator the creation tool started working fine.