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.
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
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;
}