2011-04-06 79 views
1

我有一個事件接收器(WebAdding和WebProvisioned),它適用於從網站集的根目錄創建的網站。但是,子網站(例如,在其他區域內創建的團隊網站)完全不會觸發代碼。Sharepoint 2010事件接收器不會觸發子網站

有沒有人有任何想法,爲什麼?

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); 
     }); 

    } 

    } 
} 

由於提前, 馬特

回答

0

此代碼顯示WebAdding事件,該事件發生在父網站上。

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

+0

嗨,布賴恩 - 謝謝你的回覆。但是,事件接收器應該在WebAdding和WebProvisioned事件上都觸發,但不會觸發任何子站點。即使爲WebProvisioned enevt創建了一個新的事件接收器,它仍然不會觸發子網站。 – 2011-04-07 08:48:33

+0

馬特,你能解釋「子網站(例如,在其他地區創建的團隊網站)」嗎?這些實際上是根級網站集下的網站集。 (他們是否有一個託管路徑(http:// servername/sites/sitecollectionName)。我認爲他們可能實際上是一個新網站集的根網站,並且這些事件不會在該級別激發。 – 2011-04-08 02:02:51

2

我想你不應該使用 '使用'。 您獲得的SPWeb對象引用來自正在傳遞給WebAdding方法的properties.Web。因此會遇到問題。

1

看看您的事件接收器是如何配置的 - 可能需要將範圍更改爲Site而不是Web。也許你可以在這裏張貼,這樣我們可以看到。

1

在我的網站上,我遇到了同樣的問題。仍然計算出xml文件,但在我的接收器的Elements.xml文件中,每個接收器都有相同的序列號。一旦我在Elements.xml文件中使它們唯一,WebProvisioned事件就開始啓動。不知道這是否是你遇到的同樣的問題。

0

嘗試更改接收者的範圍(在Elements.xml文件中添加屬性)。另外,請確保您的事件接收器的功能在您的網站中的網站功能中激活。