Matt Moriarty
Matt Moriarty

Reputation: 71

Sharepoint 2010 Event receiver not firing for subsite

I have an event receiver (WebAdding and WebProvisioned) which works just fine for sites created off the root of the site collection. However, subsites (for example, teamsites created within other areas) do not trigger the code at all.

Does anyone have any idea as to why?

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.Text;

namespace TestEventReceiver.EventReceiver1
{
  /// <summary>
  /// Web Events
  /// </summary>
  public class EventReceiver1 : SPWebEventReceiver
  {
    /// <summary>
    /// A site is being provisioned.
    /// </summary>
    public override void WebAdding(SPWebEventProperties properties)
    {
      base.WebAdding(properties);

      using (SPWeb web = properties.Web)
      { 
        StringBuilder output = new StringBuilder();
        output.AppendFormat("Web Adding");
        output.AppendFormat("<br>Web title: {0}",web.Title);
        SendMyEmail(web, "[email protected]", "Web Adding", output.ToString());
      }
    }

    /// <summary>
    /// A site was provisioned.
    /// </summary>
    public override void WebProvisioned(SPWebEventProperties properties)
    {
      base.WebProvisioned(properties);
      using (SPWeb web = properties.Web)
      {
        StringBuilder output = new StringBuilder();
        output.AppendFormat("Web Provisioned");
        output.AppendFormat("<br>Web title: {0}", web.Title);
        SendMyEmail(web, "[email protected]", "Web Provisioned", output.ToString());
      }
    }

    private void SendMyEmail(SPWeb Web, String toAddress, String subject, String message)
    {
      bool appendHtmlTag = false;
      bool htmlEncode = true;
      SPSecurity.RunWithElevatedPrivileges(delegate()
      {
        SPUtility.SendEmail(Web, appendHtmlTag, htmlEncode, toAddress, subject, message);
      });

    }

  }
}

Thanks in advance, Matt

Upvotes: 2

Views: 7733

Answers (5)

Augis
Augis

Reputation: 1963

Try to change scope of your receiver (in Elements.xml file add attribute ). Also, make sure that the feature of your Event receiver is activated in you site features in the subsite.

Upvotes: 0

Mark Collins
Mark Collins

Reputation: 11

On my site I had the same issue. Still figuring out the xml files, but in my Elements.xml file for the Receivers, each receiver had the same sequence number. Once I made them unique within the Elements.xml file, the WebProvisioned event started firing. Don't know if this is the same issue you were having.

Upvotes: 1

Steve
Steve

Reputation: 111

Have a look at how your event receiver is provisioned - it may be the scope needs to be changed to Site rather than Web. Perhaps you could post here so we can see.

Upvotes: 1

Sangeet
Sangeet

Reputation: 422

I think you should not be using 'Using' . The SPWeb object reference you get is from properties.Web which is being passed to the WebAdding method. You will run into issues because of this.

Upvotes: 2

Quickzig
Quickzig

Reputation: 66

This code is showing the WebAdding event and that event is occurring on the parent Web.

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spwebeventreceiver.webadding.aspx

Upvotes: 0

Related Questions