Event receiver worker class – the actual event receiver

namespace KMS_ER_ItemAdding_RS.FeatureCode
{

class KMS_ER_Itemadding_RS_Worker : SPItemEventReceiver

{
public override void ItemAdding(SPItemEventProperties properties)
{

using (SPWeb t_site = properties.OpenWeb())
{

SPList t_aList = t_site.Lists[“Related Systems”];

if (t_site.Name.ToString() == properties.AfterProperties[“RelatedSystem”].ToString())
{
properties.Cancel = true;
properties.ErrorMessage = “You can not relate a system to itself!”;
}

foreach (SPListItem t_listitems in t_aList.Items)
{

if (t_listitems[“RelatedSystem”].ToString() == properties.AfterProperties[“RelatedSystem”].ToString())
{
properties.Cancel = true;
properties.ErrorMessage = “This item already exists in this list!”;
}

}

}
}
}
}

Comments are closed.