Hi!
I have a eventreceiver that creates a new site after the the item is updated. Pretty simple code no fancy stuff at all. This code was first created for an on premise SP 2010 but know i have made an sanbox (for MS O365 enviroment) version (with a few modifications) with the sam code. But! Somtimes the event dosent seem to fire. And something i the code produses an error wich is inreasing my Resource Usage.
All user on the site have full permisions. On the om premise i was using the RunWithElevatedPrivileges but this is not available in O365 (sandbox).
I have tryed to log any exceptions on a list, but that dosent work.
Any suggestions what can be done to make this work better?
usingSystem;
usingSystem.Security.Permissions;
usingMicrosoft.SharePoint;
usingMicrosoft.SharePoint.Utilities;
usingMicrosoft.SharePoint.Workflow;
usingSystem.Text.RegularExpressions;
namespaceSharePointProjectTest.TestCreateSite
{
/// <summary>
/// List Item Events
/// </summary>
publicclassTestCreateSite:SPItemEventReceiver
{
/// <summary>
/// An item was added.
/// </summary>
publicoverridevoidItemAdded(SPItemEventProperties properties)
{
try
{
using(SPSite siteCollection =newSPSite(properties.SiteId))
{
using(SPWeb oSPWeb = properties.OpenWeb())
{
oSPWeb.AllowUnsafeUpdates=true;
SPList list = oSPWeb.Lists[properties.ListId];
SPListItem oListItem = list.Items.GetItemById(properties.ListItemId);
SPWebTemplateCollection webTemplates = oSPWeb.GetAvailableWebTemplates(1053);
//SPWebTemplateCollection webTemplates = siteCollection.GetCustomWebTemplates(Convert.ToUInt32("1053"));
string sYear =DateTime.Now.Year.ToString();
string sSiteName =GenerateSlug(getFieldValue(oListItem,"Rubrik")+"-eventID-"+ sYear + properties.ListItemId.ToString());
if(getFieldValue(oListItem,"Sidmall").Length>0)
{
SPWebTemplate siteTemplate =null;
foreach(SPWebTemplate tp in webTemplates)
{
if(tp.Title== getFieldValue(oListItem,"template"))
siteTemplate = tp;
}
//SPWebTemplate siteTemplate = webTemplates[getFieldValue(oListItem, "Sidmall")];
SPWeb newSite = oSPWeb.Webs.Add(sSiteName, getFieldValue(oListItem,"Rubrik"),"eventid:"+ properties.ListItemId.ToString(),1053, siteTemplate,false,false);
properties.ListItem["Webbplats"]= newSite.Url;
properties.ListItem.SystemUpdate();
}
else
{
properties.Cancel=true;
properties.ErrorMessage="errmessage";
}
oSPWeb.AllowUnsafeUpdates=false;
}
}
//base.ItemAdded(properties);
}
catch(Exception ex)
{
//using (SPSite siteCollection = new SPSite(properties.SiteId))
//{
// using (SPWeb site = properties.OpenWeb())
// {
// SPList logslist = site.Lists["EXCEPTION_LIST"];
// SPListItem listItem = logslist.AddItem();
// listItem["Title"] = "error";
// listItem["ErrorDescription"] = ex.InnerException.ToString();
// listItem["ErrorURL"] = "";
// listItem.Update();
// }
//}
}
}
//Helper
privatestaticboolCheckSPSite(string siteName)
{
Boolean bValue;
try
{
using(SPSite site =newSPSite(siteName))
{
using(SPWeb web = site.OpenWeb())
{
if(!web.Exists)
{
bValue = false;
}
else
{
bValue = true;
}
}
}
}
catch
{
}
finally
{
bValue =false;
}
return bValue;
}
//Helper slug
publicstaticstringGenerateSlug(string phrase)
{
string str =string.Empty;
try
{
str = phrase.ToLower();
str =Regex.Replace(str,@"[^a-z0-9\s-]","");// invalid chars
str =Regex.Replace(str,@"\s+"," ").Trim();// convert multiple spaces into one space
str = str.Substring(0, str.Length<=45? str.Length:45).Trim();// cut and trim it
str =Regex.Replace(str,@"\s","-");// hyphens
}
catch
{
}
return str;
}
// HELPER
publicstaticstring getFieldValue(SPListItem listItem,string fieldName)
{
string text =string.Empty;
if(fieldName ==string.Empty)
{
return text;
}
try
{
object myObj = listItem[fieldName];
return((myObj !=null)? myObj.ToString():string.Empty);
}
catch
{
returnstring.Empty;
}
}
}
}