Click here to Skip to main content
15,867,834 members
Articles / Programming Languages / C#

Automate Template Upload and Site Creation in SharePoint

Rate me:
Please Sign up or sign in to vote.
3.50/5 (3 votes)
20 Apr 2008CPOL3 min read 45.1K   259   22   5
This article describes how a site template can be uploaded and a new site can be created with the SharePoint Object Model.

Introduction

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.

Basics

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.AllWebs[string.empty] or 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 AllUnsafeUpdates to 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.

Code Analysis

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:

C#
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:

C#
SPSite.RootWeb

or:

C#
SPSIte.AllWebs[strirng.empty]

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 AllowUnsafeUpdates to 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.

Conclusion

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect ImpleVista Aps
Denmark Denmark
Sohel has more than six years of experience in professional software development with extensive involvement in Web based Object-Oriented, Multi-Tiered application design and development. He's Familiar with Test Driven Development (TDD) and refactoring techniques as well as having expertise in architecturing large enterprise applications. He has Experience in working with Content Management System and Portal Management System tools like SharePoint, DotNetNuke, Ektron.

Over last few years, he’s involved in development with projects on Microsoft SharePoint and received Microsoft MVP for SharePoint Server Development in the year 2011 and 2012. Currently he's working in a software company located Copenhagen,Denmark on a project integrating SharePoint and SAP. You can read his popular blog at: http://ranaictiu-technicalblog.blogspot.com

Comments and Discussions

 
GeneralUploaded into Gallery,But not finding in New sharepoint site creation under Custom list Pin
BalaramGiri29-Jun-09 8:52
BalaramGiri29-Jun-09 8:52 
I used your code and uploaded .stp files into Gallery but unable to find these templates in New sharepoint site creation under Custom list. Can you help me out??
GeneralRe: Uploaded into Gallery,But not finding in New sharepoint site creation under Custom list Pin
Sohel_Rana29-Jun-09 16:12
Sohel_Rana29-Jun-09 16:12 
GeneralRe: Uploaded into Gallery,But not finding in New sharepoint site creation under Custom list Pin
BalaramGiri30-Jun-09 6:46
BalaramGiri30-Jun-09 6:46 
AnswerNo need to hard code the Site Template Gallery in the above solution Pin
Shiva Kumar S31-Oct-08 7:40
Shiva Kumar S31-Oct-08 7:40 
GeneralRe: No need to hard code the Site Template Gallery in the above solution Pin
Sohel_Rana1-Nov-08 4:32
Sohel_Rana1-Nov-08 4:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.