2

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, "SendItToMe@MyTestAddress.com", "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, "SendItToMe@MyTestAddress.com", "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

Matt Moriarty
  • 71
  • 3
  • 4

5 Answers5

2

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.

Sangeet
  • 422
  • 3
  • 5
1

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.

Steve
  • 111
  • 1
1

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.

0

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.

Augis
  • 1,903
  • 1
  • 16
  • 21
0

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

Quickzig
  • 66
  • 3
  • Hi Brian - thanks for your reply. However, the event receiver should be firing on both WebAdding AND WebProvisioned events but is not firing for any sub-sites. Even when a new event receiver was created just for the WebProvisioned enevt, it still didn't fire for subsites. – Matt Moriarty Apr 07 '11 at 08:48
  • Matt, can you explain "subsites (for example, teamsites created within other areas)". Are these actually site collections under the root level site collection. (Do they have a managed path (http://servername/sites/sitecollectionName). I'm thinking they might actually be a root web site of a new site collection and those events don't fire at that level. – Quickzig Apr 08 '11 at 02:02