0

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;
        }
    }
});}
5
  • Is it a remote event receiver? How are you attaching the receiver to the list? Commented Sep 9, 2016 at 19:50
  • Could you post your reciever code/how you added it, please Commented Sep 12, 2016 at 15:06
  • Sorry the delay, I missed the notifications. It's an on-premise install with a standard event receiver in a farm solution. I'll post the code in the next comment.
    – Brad
    Commented Sep 12, 2016 at 16:59
  • This is a very valid point. I think its good to keep it the way it is and look for some better option. Until then keep it the same.
    – techrama
    Commented Jun 14, 2019 at 11:59
  • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
    – Gaurravs
    Commented Jun 14, 2019 at 12:24

1 Answer 1

1

I would use SharePoint manager to (http://spm.codeplex.com/) to check if the event receiver has been registered for the list.

If there is a feature to add the event receiver to the list at the time of site creation it might have throne an error for some sites. You could try to deactivate and activate the feature to add the event receiver again.

1
  • Thanks for your response. I used PowerShell to verify the event receiver is there. Due to the nature of the feature (it provisions most of the site assets and it's a very "busy" site) I can't reactivate. However first thing in the morning, when the server guys are back, I'll try and remove/add via PowerShell. I believe we tried that originally but I'll check ULS to see if there are any errors while attempting to add.
    – Brad
    Commented Sep 12, 2016 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.