本文將告訴您,如何在 Microsoft Office SharePoint Server 2007 中建立自訂的電子郵件警示處理常式。本文所述的方法會使用
IAlertNotificationHandler 介面攔截電子郵件警示,並加以修改。
若要在 Microsoft Office SharePoint Server 2007 任何下列案例中建立自訂的電子郵件警示處理常式:
- 欄位,如 ItemName] 欄位會被截斷的電子郵件警示 70 個字元。如果要解決 70 個字元的限制,使用 [其他相關資訊 > 一節中的 [所述方法]。
- 您想要在電子郵件警示中嵌入額外的內容。
- 您想要變更版面配置] 或 [電子郵件警示的外觀。
這個方法會建立的類別的繼承自
IAlertNotificationHandler 介面,然後,使用
OnNotification 方法。這個方法可讓您攔截外寄的電子郵件警示,並修改它們。您可以存取大部分的警示屬性。藉由使用 XML 剖析和 SharePoint 物件模型程式碼,您可以擷取您必須要有修改電子郵件警示的所有資訊。然後,您可以建置 HTML Stub 顯示根據您的需求的電子郵件提醒。而且,您也可以使用 SharePoint ’s SendMail 功能來傳送電子郵件提醒。
這些步驟包括格式化成密切類似預設通知範本電子郵件訊息輸出的範例程式碼。您可以修改這個範例程式碼中 HTML,以自訂產生的電子郵件警示。
- 建立繼承自 IAlertNotificationHandler 介面的類別專案。在專案中包含 Microsoft.SharePoint 命名空間和 Microsoft.SharePoint.Utilities 命名空間。
Use the following code://===================code start=====================
public class Class1:IAlertNotifyHandler
{
#region IAlertNotifyHandler Members
public bool OnNotification(SPAlertHandlerParams ahp)
{
SPSite site = null;
SPWeb web = null;
try
{
site = new SPSite(ahp.siteUrl+ahp.webUrl);
web = site.OpenWeb();
SPList list=web.Lists[ahp.a.ListID];
SPListItem item = list.GetItemById(ahp.eventData[0].itemId) ;
string FullPath=HttpUtility.UrlPathEncode(ahp.siteUrl+"/"+ahp.webUrl+"/"+list.Title+"/"+item.Name);
string ListPath = HttpUtility.UrlPathEncode(ahp.siteUrl + "/" + ahp.webUrl + "/" + list.Title);
string webPath=HttpUtility.UrlPathEncode(ahp.siteUrl+"/"+ahp.webUrl);
string build = "";
if (ahp.eventData[0].eventType==1)
eventType="Added";
else if(ahp.eventData[0].eventType==2)
eventType="Changed";
else if(ahp.eventData[0].eventType==3)
eventType="Deleted";
build = "<style type=\"text/css\">.style1 { font-size: small; border: 1px solid #000000;"+
"background-color: #DEE7FE;}.style2 { border: 1px solid #000000;}</style></head>"+
"<p><strong>"+ item.Name.ToString() +"</strong> has been "+eventType +"</p>"+
"<table style=\"width: 100%\" class=\"style2\"><tr><td style=\"width: 25%\" class=\"style1\">"+
"<a href="+ webPath +"/_layouts/mysubs.aspx>Modify my Settings</a></td>"+
"<td style=\"width: 25%\" class=\"style1\"> <a href="+ FullPath +">View "+item.Name+"</a></td>"+
"<td style=\"width: 25%\" class=\"style1\"><a href=" + ListPath + ">View " + list.Title + "</a></td>" +
" </tr></table>";
string subject=list.Title.ToString() ;
SPUtility.SendEmail(web,true , false, ahp.headers["to"].ToString(), subject,build);
return false;
}
catch (System.Exception ex)
{
return false;
}
finally
{
if (web != null)
web.Dispose();
if (site != null)
site.Dispose();
}
}
#endregion
}
//===================code end=====================
- 將強帶正負號的.dll 檔加入至全域組件快取 (GAC)。您可以將組件拖曳到組件] 資料夾。或要註冊的.dll 檔使用 GACUtil 工具。
如需詳細資訊請造訪下列 MSDN 網站]。如需詳細資訊,請按一下下列的文件編號,檢視 「 Microsoft 知識庫 」 中的文件: 315682?
(http://support.microsoft.com/kb/315682/
)
如何將組件安裝在全域組件快取在 Visual Basic.NET 或 Visual Basic 2005 中
- 製作 alertTemplates.xml 檔案在下列資料夾中的複本:
C:\Program Files\Common Files\Microsoft Shared\Web 伺服器 Extensions\12\Template\Xml
CustomAlertTemplates.xml,這個新檔案名稱,然後儲存檔案。
附註請勿直接修改 alertTemplates.xml 檔案。直接修改這個檔案不受支援。 - 編輯檔案,然後搜尋關鍵字屬性。 屬性區塊中加入下列行:
<NotificationHandlerAssembly>AlertHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d59ecf2a3bd66904</NotificationHandlerAssembly>
<NotificationHandlerClassName>AlertHandler.Class1</NotificationHandlerClassName>
<NotificationHandlerProperties></NotificationHandlerProperties> 虛設常式應該現在類似這樣:
<Properties>
<ImmediateNotificationExcludedFields>ID;Author;Editor;Modified_x0020_By;Created_x0020_By;_UIVersionString;ContentType;TaskGroup;IsCurrent;Attachments;NumComments;</ImmediateNotificationExcludedFields>
<DigestNotificationExcludedFields>ID;Author;Editor;Modified_x0020_By;Created_x0020_By;_UIVersionString;ContentType;TaskGroup;IsCurrent;Attachments;NumComments;</DigestNotificationExcludedFields>
<NotificationHandlerAssembly>AlertHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d59ecf2a3bd66904</NotificationHandlerAssembly>
<NotificationHandlerClassName>AlertHandler.Class1</NotificationHandlerClassName>
<NotificationHandlerProperties></NotificationHandlerProperties>
</Properties>
在警示的範本檔案中您想要的每個警示的範本] 區段中包含這個 XML 虛設常式。 - 在命令提示字元改 C:\Program Files\Common Files\Microsoft Shared\web 伺服器 extensions\12\BIN 目錄。然後,執行下列命令:
stsadm-o updatealerttemplates-檔案名稱"C:\Program Files\Common Files\Microsoft Shared\Web 伺服器 Extensions\12\TEMPLATE\XML\customalerttemplates.xml"-url your_sharepoint_site url
- 執行下列命令:
stsadm-o setproperty-pn 工作-立即-警示-pv 」 每隔 1 分鐘"
這個命令指定頻率 SharePoint 伺服器會檢查立即傳送的警示。此值會指定以分鐘為單位。此測試頻率設定為 1 分鐘。
如需詳細資訊請造訪下列 Microsoft 網站]: - 請確定您有 SharePoint 針對外寄電子郵件設定。 如需有關如何設定外寄電子郵件訊息的詳細資訊,請造訪下列 Microsoft 網站]:
- 如果使用測試文件庫,請確定您有開啟的文件庫的提醒。
- 請執行下列命令:
- 在 [服務 MMC] 嵌入式管理單元,重新啟動 Windows SharePoint 服務計時器服務]。
完成這些步驟之後,自訂電子郵件警示處理常式應該加以設定。建立新的警示後,您應該會收到更新自訂電子郵件警示。