WebProvisioned事件是SharePoint 2010新增加的事件处理器,可以在Web创建之后发生的事件同步或步事件,在中文版的SharePoint环境中, WebProvisioned事件叫做“已配置网站”事件。可以在 Elements.xml文件中设置同步(Synchronous)或异步( Asynchronous)。

在VS2010开发环境中可以直接创建基于Web网站的WebProvisioned事件,如图1所示:

SharePint 2010已配置网站事件

图1

在图1中选择“Web事件”接收器,此时“事件源”是不让选择的,因为此事件只有对Web网站有作用,所以无需选择网站模板,只要是创建的网站都会触发此事件。在图1的“处理以下事件”下拉列表框中选择“已配置网站”,点击“完成”按钮即可创建WebProvisioned事件处理器。

为了更好的了解SharePoint 2010事件处理器WebProvisioned重载方法的每个参数的作用,所以将WebProvisioned事件中的每个属性值写入Windows日志,这样可以清楚的看到当WebProvisioned事件触发后每个参数属性值,C#代码如下:

  1. using System;  
  2. using System.Security.Permissions;  
  3. using Microsoft.SharePoint;  
  4. using Microsoft.SharePoint.Security;  
  5. using Microsoft.SharePoint.Utilities;  
  6. using Microsoft.SharePoint.Workflow;  
  7. using System.Diagnostics;  
  8. using System.Text;  
  9.  
  10. namespace WebProvisionedEventReceiver.EventReceiver1  
  11. {  
  12.     /// <summary>  
  13.     /// Web 事件  
  14.     /// </summary>  
  15.     public class EventReceiver1 : SPWebEventReceiver  
  16.     {  
  17.         /// <summary>  
  18.         /// 已配置网站.  
  19.         /// </summary>  
  20.         public override void WebProvisioned(SPWebEventProperties properties)  
  21.         {  
  22.             this.LogWebEventProperties(properties);  
  23.             base.WebProvisioned(properties);  
  24.         }  
  25.         /// <summary>  
  26.         /// Web事件日志  
  27.         /// </summary>  
  28.         /// <param name="properties"></param>  
  29.         private void LogWebEventProperties(SPWebEventProperties properties)  
  30.         {  
  31.             StringBuilder sB = new StringBuilder();  
  32.             try 
  33.             {  
  34.                 sB.AppendFormat("{0} at {1}\n\n", properties.EventType, DateTime.Now);  
  35.                 sB.AppendFormat("Cancel:{0}\n", properties.Cancel);  
  36.                 sB.AppendFormat("ErrorMessage:{0}\n", properties.ErrorMessage);  
  37.                 sB.AppendFormat("EventType:{0}\n", properties.EventType);  
  38.                 sB.AppendFormat("FullUrl:{0}\n", properties.FullUrl);  
  39.                 sB.AppendFormat("NewServerRelativeUrl:{0}\n", properties.NewServerRelativeUrl);  
  40.                 sB.AppendFormat("ParentWebId:{0}\n", properties.ParentWebId);  
  41.                 sB.AppendFormat("ReceiverData:{0}\n", properties.ReceiverData);  
  42.                 sB.AppendFormat("RedirectUrl:{0}\n", properties.RedirectUrl);  
  43.                 sB.AppendFormat("ServerRelativerUrl:{0}\n", properties.ServerRelativeUrl);  
  44.                 sB.AppendFormat("SiteId:{0}\n", properties.SiteId);  
  45.                 sB.AppendFormat("Status:{0}\n", properties.Status);  
  46.                 sB.AppendFormat("UserDisplayName:{0}\n", properties.UserDisplayName);  
  47.                 sB.AppendFormat("UserLoginName:{0}\n", properties.UserLoginName);  
  48.                 sB.AppendFormat("Web:{0}\n", properties.Web.Title);  
  49.                 sB.AppendFormat("WebId:{0}\n", properties.WebId);  
  50.             }  
  51.             catch (Exception ex)  
  52.             {  
  53.                 sB.AppendFormat("Exception:{0}\n", ex.Message);  
  54.             }  
  55.             string source = "WebEventCustomLog";  
  56.             string logName = "Application";  
  57.             if (!EventLog.SourceExists(source))  
  58.             {  
  59.                 EventLog.CreateEventSource(source, logName);  
  60.             }  
  61.             try 
  62.             {  
  63.                 EventLog.WriteEntry(source, sB.ToString());  
  64.             }  
  65.             catch { }  
  66.         }  
  67.     }  
  68. }  

 

运行结果请查看原文:http://www.360sps.com/item/0d2ff85690f540a69d32072f46488299.aspx