This article will help you to create your own custom site provisioning that integrates your business process and will show you how to add some custom functionalities.
One of the great things about SharePoint is how easy it is to create a new site. The user selects a template he wants, provides a title, description, and URL name for the site… and voila, a new site is born! This out-of-the box functionality allows users, a quick and easy way to create a new site. This is good and easy, but more likely it does not meet with your customer requirements. Particularly in large organizations, when you need to implement a business process or you want to enforce some functionality to save time in support and training.
This article will help you to create your own custom site provisioning that integrates your business process and will show you how to add some custom functionalities.
Below is a diagram showing the entire process and all of the various components and steps that make up the entire process.
After gathering the requirements you need to be able to answer these questions:
- What type of site do you need, a site collection or a sub site?
- Sites are meant for collaboration or for publishing?
- Is approval process needed?
- How does the navigation will look like?
- Where will your sites be created?
a. A new sub site under the root site collection e.g. http://server/teamsite/project1
b. A new site collection at the root level of your Web Application paths e.g. http://server/project1
c. A new site collection under managed paths e.g. http://server/projects/project1
d. A new site collection under a different Web Application e.g. http://server-projects/project1 Search - Search – one of the most important part, how users will find the sites?
These questions are important to help you define your provision process.
The Request Form – Keep it simple
The request form should be a simple - easy to use form. When a user wants to create a new site, he needs to fill out some fields, like site title, description, keywords and most important the right template. The site provision process will be based on the chosen template.
Creating the Request Form
As a SharePoint developer, you have a wide variety of methods and techniques to build your request form. You can choose:
- InfoPath form
- Web Part
- A SharePoint List entry
- ASPX page
- Web Control
Personally I will recommend building a Web Control and adding this control to a custom aspx page in your application pages (_layouts folder). The advantage of application page, is that exposed in any site collection or sub site, for example, http://server/_layouts/CreateSite.aspx, and unlike content pages, cannot be edited by users.
Site Request List
The second component that your site provision will use is a SharePoint List. This list will help the site administrator to monitor all site requests, and also will give you the possibility to associate custom or out of the box workflows to approve new site requests. Note: If you choose not to associate the request with workflow, Add a SPLongOperation object, because your creation process will takes a while.
using (SPLongOperation longOperation = new SPLongOperation(this.Page))
{
longOperation.Begin();
SPWeb newSite = CreateSite(); //this function will start your site provison hendler
longOperation.End(newSite.Url);
}
{
longOperation.Begin();
SPWeb newSite = CreateSite(); //this function will start your site provison hendler
longOperation.End(newSite.Url);
}
New SharePoint Site
When you create a new SharePoint site there are two options:
1. A new Site Collection - use the Add method of the SPWebCollection class.
2. A new Sub Site - use the Add method of the Webs collection of the SPWeb class.
The following examples create a new Site Collection and a new Sub Site that is based on information gathered from the request form and stored in the Site Request List.
1. A new Site Collection - use the Add method of the SPWebCollection class.
2. A new Sub Site - use the Add method of the Webs collection of the SPWeb class.
The following examples create a new Site Collection and a new Sub Site that is based on information gathered from the request form and stored in the Site Request List.
Create Sub Site
private string CreateSubSite(SPListItem item)
{
SPWeb newWeb = null;
string url = string.Empty;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(form.HostUrl))
{
site.AllowUnsafeUpdates = true;
using (SPWeb web = site.OpenWeb ())
{
string relativeUrl = item["ID"].ToString().PadLeft(6, '0');
web.AllowUnsafeUpdates = true;
newWeb = web.Webs.Add(relativeUrl,
item["Title"].ToString(),
item["Description"].ToString(),
1033,
item["Template"].ToString(),
true, false);
url = newWeb.Url;
newWeb.Dispose();
}
}
});
}
catch (Exception ex)
{
// handle exception
} finally
{
if (newWeb == null)
{
newWeb.Dispose();
}
} return url;
}
Create Site Collection {
SPWeb newWeb = null;
string url = string.Empty;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(form.HostUrl))
{
site.AllowUnsafeUpdates = true;
using (SPWeb web = site.OpenWeb ())
{
string relativeUrl = item["ID"].ToString().PadLeft(6, '0');
web.AllowUnsafeUpdates = true;
newWeb = web.Webs.Add(relativeUrl,
item["Title"].ToString(),
item["Description"].ToString(),
1033,
item["Template"].ToString(),
true, false);
url = newWeb.Url;
newWeb.Dispose();
}
}
});
}
catch (Exception ex)
{
// handle exception
} finally
{
if (newWeb == null)
{
newWeb.Dispose();
}
} return url;
}
private static string CreateSiteCollection(SPListItem item)
{
SPSite site = null;
SPWeb newWeb = null;
string url = string.Empty;
try {
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWebApplication webApplication = null;
webApplication = SPWebApplication.Lookup(new Uri (YourHostUrl));
string relativeUrl = "";
relativeUrl = item["ID"].ToString().PadLeft(6, '0');
site = webApplication.Sites.Add(YourHostUrl + "/" + relativeUrl,
item ["Title"].ToString (),
item["Description"].ToString (),
1033,
item["Template"].ToString (),
siteAdministratorLoginName,
siteAdministratorName,
; siteAdministratorEmail);
newWeb = site.OpenWeb();
url = newWeb.Url;
web.Dispose ();
site.Dispose ();
});
}
catch (Exception ex)
{
// handle exception
}
finally
{
if (newWeb!= null)
{
newWeb.Dispose();
}
if (site != null)
{
site.Dispose();
}
}
return url;
}
{
SPSite site = null;
SPWeb newWeb = null;
string url = string.Empty;
try {
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWebApplication webApplication = null;
webApplication = SPWebApplication.Lookup(new Uri (YourHostUrl));
string relativeUrl = "";
relativeUrl = item["ID"].ToString().PadLeft(6, '0');
site = webApplication.Sites.Add(YourHostUrl + "/" + relativeUrl,
item ["Title"].ToString (),
item["Description"].ToString (),
1033,
item["Template"].ToString (),
siteAdministratorLoginName,
siteAdministratorName,
; siteAdministratorEmail);
newWeb = site.OpenWeb();
url = newWeb.Url;
web.Dispose ();
site.Dispose ();
});
}
catch (Exception ex)
{
// handle exception
}
finally
{
if (newWeb!= null)
{
newWeb.Dispose();
}
if (site != null)
{
site.Dispose();
}
}
return url;
}
Apply Business Process
Finally we can apply our business process. To help you to apply the business process, you will need to develop a Site Provision handler class, which contains your custom methods.
Developing an Site Provisioning Handler
Depending of your requirements you will develop your handler. Here is a list; to give you an idea of what kind of methods you can implements in your handler:
• Create unique permission levels – You can define a new permission set that contains your custom roles.
• Create new groups - You can create a new site group to include the rights you created.
• Add users or groups - You may want to add user to your new groups.
• Activate Features – Activate the right features for each template.
• Add Content – Add web parts, create pages edit content and etc.
• Set site quota
• Change StyleSheet
• …
When you develop the site provision handler make sure…
• You dispose SPSite and SPWeb and be sure there are no memory leaks.
• You test it with different user accounts that have only Reader rights.
• You handel collaboration sites and publishing sites differently (publishing site pages need to be checked out before editing)
• Error handling – You provide feedback to the user and log error information.
Depending of your requirements you will develop your handler. Here is a list; to give you an idea of what kind of methods you can implements in your handler:
• Create unique permission levels – You can define a new permission set that contains your custom roles.
• Create new groups - You can create a new site group to include the rights you created.
• Add users or groups - You may want to add user to your new groups.
• Activate Features – Activate the right features for each template.
• Add Content – Add web parts, create pages edit content and etc.
• Set site quota
• Change StyleSheet
• …
When you develop the site provision handler make sure…
• You dispose SPSite and SPWeb and be sure there are no memory leaks.
• You test it with different user accounts that have only Reader rights.
• You handel collaboration sites and publishing sites differently (publishing site pages need to be checked out before editing)
• Error handling – You provide feedback to the user and log error information.
Created Sites List
Last but not least, create a list that contains all sites that were created with your site provision. This list will be useful to monitor exiting sites and most importantly you could execute CAML queries to retrieve sites from the list.
For example, if you want to display the last five sites that were created. You can do it simply by using this CAML query:
For example, if you want to display the last five sites that were created. You can do it simply by using this CAML query:
SPListItemCollection itemColl = null;
using (SPSite site = new SPSite (SPContext.Current.Site.Url))
{
using (SPWeb web = site.OpenWeb ())
{
SPList list = web.Lists [“CreatedSitesList”];
SPQuery qry = new SPQuery();
qry.Query = "";
qry.ViewFields = "";
qry.RowLimit = 5;
itemColl = list.GetItems(qry);
}
}
using (SPSite site = new SPSite (SPContext.Current.Site.Url))
{
using (SPWeb web = site.OpenWeb ())
{
SPList list = web.Lists [“CreatedSitesList”];
SPQuery qry = new SPQuery();
qry.Query = "";
qry.ViewFields = "";
qry.RowLimit = 5;
itemColl = list.GetItems(qry);
}
}
Conclusion
This article gives you some tips to create SharePoint sites programmatically, which enables custom business process automation. Hopefully it gave you a good start point for your site provision.
This article gives you some tips to create SharePoint sites programmatically, which enables custom business process automation. Hopefully it gave you a good start point for your site provision.
Please remove all republished eggheadcafe.com content from your blog. We did not grant you permission to copy the work we paid our authors to do. A request has been submitted to google to force its removal.
ReplyDelete