I am working on SharePoint server 2013 web application. And I need to create an event receiver on my lists items. The event receiver will check if the user who create an item is within a specified user group and update some items fields accordingly. Here is the event receiver I am planning to have:-
public override void ItemUpdated(SPItemEventProperties properties)
{
DisableEventFiring();
base.ItemUpdated(properties);
bool findany = false;
using (SPSite site = new SPSite(properties.SiteId))
{
using (SPWeb web = properties.OpenWeb())
{
try
{
web.AllowUnsafeUpdates = true;
SPListItem currentItem = properties.ListItem;
SPGroup group = web.SiteGroups[webname];
}
SPUser user = properties.Web.CurrentUser;
if (currentItem["Team"] == null || currentItem["Team"] == string.Empty)
{
foreach (var user2 in group.Users)
{
if (user2.ToString() == user.ToString())
{
currentItem["Team"] = "CM";
findany = true;
break;
}
}
if (!findany)
{
currentItem["Team"] = "IT";
//currentItem["Action"] = "Item Added";
currentItem.SystemUpdate();
}
}
currentItem.SystemUpdate();
}
catch (Exception ex)
{
throw ex;
}
finally
{
EnableEventFiring();
}
}
}
}
Now my problem is how I can re-use my event receiver on multiple subsites and list. Currently I have the following architecture for my web application:-
- I have a team site collection on the root
http://servername/
- Inside it I have a team sub-site
http://servername/customers/
- Inside the
/customers/
subsite I have many subsites;http://servername/customers/customerA
,http://servername/customers/customerB
, etc… - Inside each of the customerA, customerB , etc subsites I have 4 issue tracking lists.
- Each list uses different site content type.
- Now the event receiver I want to use will be targeting all the customers’ subsites. And only on one issue tracking list which is named “Tracking”.
- In another words I want I event receiver to be targeting all the lists named “Tracking” found inside all the subsites under the
/customers/
sub site. Or to be targeting all the lists “whatever are their name” that uses a site content type named “CustomersTracking”.
So I want to target the event receiver to a group of lists which have these things in common :-
- Have the same list name (Tracking) and are under the parent subsite /customers/
- Share the same site content type and all are under the parent subsite /customers/
So can anyone advice how I can target my event receiver to those lists only?
Thanks