I've searched for an answer to this but nothing I came across was exactly like what I'm experiencing.
I have 1000's of site collections that have been created from the same site template which is from the same custom solution. Within that same solution I also have an event receiver that attaches to a single library. That event receiver includes ItemAdding, ItemAdded, and ItemUpdated events. In the majority of all site collections all three event receivers work correctly however in some site collections it appears that the ItemAdding event receiver doesn't fire. The purpose of the ItemAdding event receiver is to block files that aren't of a specific content type to be added at the root of the library. I've checked via PowerShell to confirm that the Event Receiver is in fact attached to the list.
Does anyone have any suggestions on where I might look to identify what's going on? It would be easier to figure out if it didn't work anywhere but unfortunately in most cases it works exactly as intended.
Thanks in advance!
-- Update(s) --
The following is the code from the ItemAdding event receiver. I've changed the names of the list and content types for privacy reasons. I've also removed the error messages in the catch block.
public override void ItemAdding(SPItemEventProperties properties){
base.ItemAdding(properties);
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(properties.Site.ID))
{
using (SPWeb web = site.RootWeb)
{
web.AllowUnsafeUpdates = true;
try
{
SPList list = web.Lists["DocumentLibrary"];
// Get URL of item being added
String addingItemUrl = properties.AfterUrl;
// Get Content Type ID of item being added as well as Content Type ID of allowed Content Types
String addingItemCtId = null;
try
{
if (properties.AfterProperties["ContentTypeId"] != null)
{
addingItemCtId = properties.AfterProperties["ContentTypeId"].ToString();
}
}
catch (Exception ex)
{
LoggingService.LogError("");
}
String ctOneId = list.ContentTypes["CT One"].Id.ToString();
String ctTwoId = list.ContentTypes["CT Two"].Id.ToString();
String ctThreeId = list.ContentTypes["CT Three"].Id.ToString();
// Split URL in to segements and load in to array
String[] addingItemSplitUrl = addingItemUrl.Split('/');
// Get number of segments in URL
int urlSegments = addingItemSplitUrl.Length;
// If URL segments are less than 3 than it is the root folder
if (urlSegments < 3)
{
if (addingItemCtId == null)
{
properties.ErrorMessage = "Error Message";
properties.Status = SPEventReceiverStatus.CancelWithError;
}
if (addingItemCtId != null)
{
if (addingItemCtId.Contains(ctOneId) != true && addingItemCtId.Contains(ctTwoId) != true && addingItemCtId.Contains(ctThreeId) != true)
{
properties.ErrorMessage = "Error Message.";
properties.Status = SPEventReceiverStatus.CancelWithError;
}
}
}
}
catch (Exception ex)
{
LoggingService.LogError("");
}
web.AllowUnsafeUpdates = false;
}
}
});}