I am working on SharePoint 2013 team site collection, and I have the following architecture -
- A team site collection which contain 15 sub sites. Each subsite represents one of our customers.
- On each subsite I have an issue tracking list.
- All the issue tracking lists inside the subsite share the same permission and the same content type.
- But on each subsite’s tracking list, the “assigned to” column (at the list level) is linked to a unique user group representing the customer’s client account managers. Group name is "Customer name client managers"
Now I have the following requirements:-
- If the user how adds an issue tracking list item is defined inside the related “Customer name client managers” group, then to set a column named “Important” to Yes.
To test this I wrote an event receiver , and I set it as follow:-
- I specify the site url to be equal to a subsite url.
- Specific to have a farm solution
- The event receiver type is “List item events”
- Which will trigger when an item was created .
Now the above worked well on the intended subsite. But i will be facing these limitations:-
I will have to apply 11++ different event receivers representing 11 different sub-sites, since I have deploy the event receiver to the related sub-site url.
Also inside the event receiver I have explicitly defined the related group name "CustomerA" in the below code. So my question is if there is a way to improve my approach to allow reusing the same event receiver among all the sub-sites. For example to apply the event receiver to the site content type instead to the subsite, and inside the event receiver to dynamically select the related group name based on the current subsite? Where I can name the group to be equal to the subsite name,, In this case I will have a single event receiver which should work on all the subsites ??
here is the code for the event receiver:-
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
bool findany = false;
using (SPSite site = new SPSite(properties.SiteId))
{
using (SPWeb web = properties.OpenWeb())
{
try
{
SPListItem currentItem = properties.ListItem;
SPUser user = properties.Web.CurrentUser;
SPGroup group = web.SiteGroups["CustomerA"];
foreach (var user2 in group.Users)
{
if (user2.ToString() == user.ToString())
{
currentItem["Important"] = "Yes";
currentItem.SystemUpdate();
findany = true;
break;
}
}
if (!findany)
{
currentItem["Important"] = "No";
currentItem.SystemUpdate();
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}