Saturday, July 2, 2011

Create subsite programmatically when a new item is created in a SharePoint list

Code snippet to create sub site when a new item in list. Create a feature and bind this event handler to itemadding event.

public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);

string sitename = string.Empty;
string newSiteURL = string.Empty;
string siteShortName = string.Empty;
string strTemplateName = "Blank Site Template";

try
{
sitename = properties.AfterProperties["Title"].ToString();
siteShortName = GetAcronym(sitename.Trim());

if (properties.ListTitle == "Projects")
{
SPWeb website = properties.OpenWeb();
website.AllowUnsafeUpdates = true;

using (SPSite site = website.Site)
{
//to check if the site name/URL already exists
SPWeb web = site.AllWebs[website.ServerRelativeUrl + "/" + siteShortName];
int i = 0;

//if site exists, loop through with appending number at the end of site URL
while (web.Exists)
{
i = i + 1;
web = site.AllWebs[website.ServerRelativeUrl + "/" + siteShortName + i];
}

if (i != 0)
{
siteShortName = siteShortName + i;
}

//Create subsite
newSiteURL = CreateSubSite(website.Url, siteShortName, sitename, strTemplateName);

//Updating the sub site URL in the list's URL field
if (!string.IsNullOrEmpty(newSiteURL))
{
properties.AfterProperties["URL"] = newSiteURL;
}
website.AllowUnsafeUpdates = false;
web.Dispose();
}
}
}
catch (Exception ex)
{
//handle exception
}
}


/// [summary]
/// Method to create subsite
/// [summary]
/// [param name="parentSiteURL"]Parent site URL [param]
/// [param name="siteURLRequested"]site name acronym to be appended in subsite URL [param]
/// [param name="siteTitle"]Site Name [param]
/// [param name="siteTemplateName"]Site Template Name [param]
/// [returns]site URL [returns]
private string CreateSubSite(string parentSiteURL, string siteURLRequested, string siteTitle, string siteTemplateName)
{
const Int32 LOCALE_ID_ENGLISH = 1033;
string newSiteURL = string.Empty;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite siteCollection = new SPSite(parentSiteURL))
{
using (SPSite site = new SPSite(parentSiteURL))
{
using (SPWeb parentWeb = site.OpenWeb())
{
SPWebTemplateCollection Templates = parentWeb.GetAvailableWebTemplates(Convert.ToUInt32(LOCALE_ID_ENGLISH));
SPWebTemplate siteTemplate = Templates[siteTemplateName];

//create subsite
using (SPWeb newSite = parentWeb.Webs.Add(siteURLRequested, siteTitle, "", Convert.ToUInt32(LOCALE_ID_ENGLISH), siteTemplate, false, false))
{
if (!string.IsNullOrEmpty(newSite.Url))
newSiteURL = newSite.Url.ToString();
}
}
}
}
});
return newSiteURL;
}
catch (Exception ex)
{
//handle exception
}
}


/// summary>
/// Function to get the acronym of the site name
/// summary>
/// [param name="strSiteName"]Site Name [param]
/// [returns]acronym value[/returns]
private string GetAcronym(string strSiteName)
{
string shortName = string.Empty;
string[] txtArray = strSiteName.Split(' ');
try
{
//if site name is a single word, get the first three characters of the site name
if (txtArray.Length == 1)
{
return strSiteName.Substring(0, 3);
}

//Get the first letter of each word in the site name
foreach (string word in txtArray)
{
shortName += word.Substring(0, 1);
}
return shortName;
}
catch (Exception ex)
{
// handle exception
}
}

No comments:

Post a Comment