We usually upload a site template manually. We follow the manual process even in site creation. But the SharePoint Object Model provides interfaces to automate these processes. Months ago, our client asked us to automate the site creation process. Then, I started the work and finally found that the process is much easier to implement.
In SharePoint, Site Templates are stored in a list named “Site Template Gallery”. To upload a template to a site, all you need to do to upload a file in the list “Site Template Gallery”. The galley only exists in the RootWeb. In SharePoint, you can get the root web by accessing
site.RootWeb. So you need to add the template to the root web’s “Site Template Gallery”. To do so, you have to have the site collection administrator’s credentials. In the sample application, you need to provide the site collection administrator’s user name. I have used a function
GetUserToken to get the site collection administrator’s token.
To create a site, you also need to add a web to the
AllWebs collection. But before that, you might set the
true which ensure that the update works under an HTTP GET request and without requiring the security validation. During site creation, you need to specify the template name. If you don’t provide the template name, the site will still be created but if you try to access the site, you’ll be prompted to select a site template.
Every Windows application uses a credential while it executes. For example, in the case of a desktop application, the credential of the Windows user currently logged in is used. In the case of a web application, the credential of the application pool user is used. When you open a SharePoint site using a code like below, the credential of the current user is used:
SPSite site = new SPSite(siteUrl);
However, you can pass the
SPUserToken of the SharePoint user as the second parameter. To get the
SPToken of a user, I have written a function
GetUserToken. If the SharePoint site uses Windows authentication and you run the application with the site collection administrator’s credential, then the code will run swimmingly without passing the
SPUserToken. However, if you try to execute the code with a user, not a site collection administrator, then you need to specify the site collection administrator name, and the
GetUserToken function will return the
SPUserToken of the site collection administrator.
When you use the SharePoint Object Model to upload a template, you need to know that the site template is stored on the root web. To get the root web from an
SPSite object, you’ll use:
In the root web, the site template is stored in the list “Site Template Gallery”. So, the site template upload actually is the process of adding the template file in the “Site Template Gallery” list.
To create a site, it’s better to set
true. This ensures any update to the site without security validation. In the
SPSite.AllWebs.Add method, I have passed 1033 as the Locale ID, which specifies the English locale. You can specify in the
Add method whether to use unique permission or not. If you set the unique permission to
true, then no security settings will be inherited in the new site form the parent site. In this case, you need to add different groups and users to the new site. If you inherit security settings instead of unique permissions, then the parent site’s user will get the same permission in the current new site.
SharePoint Object Model is a rich interface to communicate with SharePoint functionalities. We can also use the SharePoint provided Web Services to implement most of these functionalities. To run this console application, you need to run the application where SharePoint is installed, but with SharePoint Web Services, you can perform these functionalities from anywhere.