4

I have a code from external developer that creates site in list event receiver.

 public override void ItemAdded(SPItemEventProperties properties)
        {
            bool EventFiringEnabledStatus = EventFiringEnabled;
            ReceiverParams param = new ReceiverParams(properties);
            EventFiringEnabled = false;
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(properties.SiteId))
                    {
                        using (SPWeb web = site.OpenWeb(properties.Web.ID))
                        {
                            if (!IsWebExists(web, name))
                            {
                                bool allowUnsafeUpdates = web.AllowUnsafeUpdates;
                                try
                                {
                                    web.AllowUnsafeUpdates = true;
                                    SPWebCollection collection = web.Webs;
                                    SPWeb projectWeb = collection.Add(name, title, description, culture_LCID, templateName, true, false);
                                    projectWeb.Update();
                                    isCreated = true;
                                }
                                finally
                                {
                                    web.AllowUnsafeUpdates = allowUnsafeUpdates;
                                }
                            }
                        }
                    }
                });
            }
            finally
            {
                EventFiringEnabled = EventFiringEnabledStatus;
            }
        }

I want to customize the site using WebProvisioned event receiver:

 public override void WebProvisioned(SPWebEventProperties properties)
        {
            System.Diagnostics.Debugger.Break();
            base.WebProvisioned(properties);
        }

This event fires when i create site from web interface or from my own code. But it does not fires when the site is created in list event receiver. There is only "Error loading and running event receiver : Thread was being aborted." in logs.

  • 1
    Please provide the details of list event receiver you use to create site. – Amit Kumawat Apr 9 '12 at 13:46
  • I have added the code that creates site with system user's permissions. – Ruslan Dayanov Apr 9 '12 at 14:16
  • 2
    Are you creating a subsite or a nwe root site in a site collection? – Anders Rask Sep 1 '12 at 9:08
1

You may want to check if the Event Receiver is registered (using SharePoint Manager) and verify if it needs to be scoped at Site level rather than at Web level (the Webprovisioned event does not get fired for the root web). Here's a good post giving details on WebProvisioned event receiver – a practical example.

UPDATE: The differences in the two scenarios may lie in the context (HTTPContext v. SPContext) - this can be verified by posting events in the ULS logs.

  • 1
    Thank you for answer. It has a site level scope. It really works when i am creating site. The actual question is what can prevent it from firing when site is created from list item event receiver? Can it be EventFiringEnabled=false in parent event receiver? – Ruslan Dayanov Apr 9 '12 at 13:52
  • I have updated my response above regarding the context (of your two scenarios). – Supriyo SB Chatterjee Apr 9 '12 at 14:00
  • 2
    Ruslan, you are on the right track with your comment. EventFiringEnabled=false quiets down the event model and is preventing WebProvisioned event from being raised during the list item event receiver. – bkwdesign Jul 3 '12 at 14:45
0

This row

EventFiringEnabled = false;

is telling the code to not fire the WebProvisioned event, so that is why your event receiver does not trigger.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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