Hi,
I created an event receiver sandbox solution and deployed it to preprod. My code doesn't work and everytime I edit an item it gives me this error. I don't receive an error when I create an item.
Server encountered an internal error. For more information, turn off customErrors in the server's .config file.
at Microsoft.SharePoint.SPGlobal.HandleComException(COMException comEx) at Microsoft.SharePoint.Library.SPRequest.AddOrUpdateItem(String bstrUrl, String bstrListName, Boolean bAdd, Boolean bSystemUpdate, Boolean bPreserveItemVersion, Boolean bPreserveItemUIVersion,
Boolean bUpdateNoVersion, Int32& plID, String& pbstrGuid, Guid pbstrNewDocId, Boolean bHasNewDocId, String bstrVersion, Object& pvarAttachmentNames, Object& pvarAttachmentContents, Object& pvarProperties, Boolean bCheckOut, Boolean bCheckin,
Boolean bMigration, Boolean bPublish, String bstrFileName, ISP2DSafeArrayWriter pListDataValidationCallback, ISP2DSafeArrayWriter pRestrictInsertCallback, ISP2DSafeArrayWriter pUniqueFieldCallback) at Microsoft.SharePoint.SPListItem.AddOrUpdateItem(Boolean
bAdd, Boolean bSystem, Boolean bPreserveItemVersion, Boolean bNoVersion, Boolean bMigration, Boolean bPublish, Boolean bCheckOut, Boolean bCheckin, Guid newGuidOnAdd, Int32& ulID, Object& objAttachmentNames, Object& objAttachmentContents, Boolean
suppressAfterEvents, String filename, Boolean bPreserveItemUIVersion) at Microsoft.SharePoint.SPListItem.UpdateInternal(Boolean bSystem, Boolean bPreserveItemVersion, Guid newGuidOnAdd, Boolean bMigration, Boolean bPublish, Boolean bNoVersion, Boolean bCheckOut,
Boolean bCheckin, Boolean suppressAfterEvents, String filename, Boolean bPreserveItemUIVersion) at Microsoft.SharePoint.SPListItem.Update() at Microsoft.SharePoint.WebControls.SaveButton.SaveItem(SPContext itemContext, Boolean uploadMode, String checkInComment)
at Microsoft.SharePoint.WebControls.SaveButton.OnBubbleEvent(Object source, EventArgs e) at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
private bool HasFieldChanged(SPItemEventProperties properties, string sInternalName)
{
if (properties.ListItem[sInternalName] != null && properties.AfterProperties[properties.ListItem.Fields[sInternalName].StaticName] != null)
//before properties is not equal than after properties
return !properties.ListItem[sInternalName].ToString().Equals(properties.AfterProperties[properties.ListItem.Fields[sInternalName].StaticName].ToString(), StringComparison.OrdinalIgnoreCase);
return false;
}
public override void ItemUpdating(SPItemEventProperties properties)
{
base.ItemUpdating(properties);
try
{
if (HasFieldChanged(properties, "WHQ") && !HasFieldChanged(properties, "Employee Type"))
{
SPList oSPList = properties.Web.Lists["EPDEMails"];
SPListItem oSPItem = oSPList.AddItem();
SPListItem item = properties.ListItem;
string sAfterWHQDepartment = properties.AfterProperties[properties.ListItem.Fields["WHQ Department"].StaticName].ToString();
string sBeforeWHQDepartment = item[properties.ListItem.Fields["WHQ Department"].StaticName].ToString();
string sEmails = string.Empty;
string sGroups = string.Format("{0},{1},Editors", sAfterWHQDepartment, sBeforeWHQDepartment);
foreach (string group in sGroups.Split(','))
{
foreach (SPUser thisuser in properties.Web.Groups[group].Users)
sEmails += thisuser.Email + ";";
}
oSPItem["Subject"] = "Employee Transfer to a different WHQ Department";
oSPItem["To"] = sEmails;
oSPItem["Body"] = string.Format("{0} {1} has been transferred to {2} from {3}. Please update EPD ASAP", item["First Name"],
item["Last Name"], sAfterWHQDepartment, sBeforeWHQDepartment);
oSPItem.Update();
}
if (HasFieldChanged(properties, "Region") && !HasFieldChanged(properties, "Employee Type"))
{
SPList oSPList = properties.Web.Lists["EPDEMails"];
SPListItem oSPItem = oSPList.AddItem();
SPListItem item = properties.ListItem;
string sAfterRegion = properties.AfterProperties[properties.ListItem.Fields["Region"].StaticName].ToString();
string sBeforeRegion = item[properties.ListItem.Fields["Region"].StaticName].ToString();
string sEmails = string.Empty;
string sGroups = string.Format("{0},{1},Editors", sAfterRegion, sBeforeRegion);
foreach (string group in sGroups.Split(','))
{
foreach (SPUser thisuser in properties.Web.Groups[group].Users)
sEmails += thisuser.Email + ";";
}
oSPItem["Subject"] = "Employee Transfer to a different Region";
oSPItem["To"] = sEmails;
oSPItem["Body"] = string.Format("{0} {1} has been transferred to {2} from {3}. Please update EPD ASAP", item["First Name"],
item["Las tName"], sAfterRegion, sBeforeRegion);
oSPItem.Update();
}
if (HasFieldChanged(properties, "Employee Type"))
SPList oSPList = properties.Web.Lists["EPDEMails"];
SPListItem oSPItem = oSPList.AddItem();
SPListItem item = properties.ListItem;
string sChangedEmployeeType = properties.AfterProperties[properties.ListItem.Fields["Employee Type"].StaticName].ToString();
if (sChangedEmployeeType == "Region")
{
string sAfterRegion = properties.AfterProperties[properties.ListItem.Fields["Region"].StaticName].ToString();
string sBeforeWHQDepartment = item[properties.ListItem.Fields["WHQ Department"].StaticName].ToString();
string sEmails = string.Empty;
string sGroups = string.Format("{0},{1},Editors", sAfterRegion, sBeforeWHQDepartment);
foreach (string group in sGroups.Split(','))
{
foreach (SPUser thisuser in properties.Web.Groups[group].Users)
sEmails += thisuser.Email + ";";
}
oSPItem["Subject"] = "Employee Transfer to a Region";
oSPItem["To"] = sEmails;
oSPItem["Body"] = string.Format("{0} {1} has been transferred to {2} from {3}. Please update EPD ASAP", item["First Name"], item["Last Name"], sAfterRegion, sBeforeWHQDepartment);
oSPItem.Update();
}
else if (sChangedEmployeeType == "WHQ")
{
string sBeforeRegion = item[properties.ListItem.Fields["Region"].StaticName].ToString();
string sAfterWHQDepartment = properties.AfterProperties[properties.ListItem.Fields["WHQ Department"].StaticName].ToString();
string sEmails = string.Empty;
string sGroups = string.Format("{0},{1},Editors", sAfterWHQDepartment, sBeforeRegion);
foreach (string group in sGroups.Split(','))
{
foreach (SPUser thisuser in properties.Web.Groups[group].Users)
sEmails += thisuser.Email + ";";
}
oSPItem["Subject"] = "Employee Transfer to a WHQ Department";
oSPItem["To"] = sEmails;
oSPItem["Body"] = string.Format("{0} {1} has been transferred to {2} from {3}. Please update EPD ASAP",
item["First Name"], item["Last Name"], sAfterWHQDepartment, sBeforeRegion);
oSPItem.Update();
}
}
//send email
SmtpClient sendEmail = new SmtpClient();
MailMessage message = new MailMessage();
sendEmail.Host = "abc@yahoo.com";
message.IsBodyHtml = true;
message.From = new MailAddress("abc@yahoo.com");
message.To.Add("abc@yahoo.com");
message.Subject = "Test Email on Transfer Jan 13th";
message.Priority = MailPriority.Normal;
sendEmail.Send(message);
}
catch (Exception ex)
{
throw ex;
}
}
This is really frustrating. Any help is appreciated.
Talibah C