SharePoint Portal Server 2003 有個叫作 Alert 的功能,能讓使用者訂閱想要看的網站資訊,當有新增或更動的時候,會自動送一份通知給使用者。這個功能在 SPS2001 的時候稱之為 Subscription,當然,除了名稱不同之外,SPS2003 的 Alert 與舊版本在其它的地方也是相差很多的,使用 Object Class 的方式也有著天壤之別,這一篇在說明在客製化 SPS2003 Alert 上的一些應用技巧,及可能碰上的陷阱。

一般而言,我們在客製化 Alert 時會使用到這些 name space:

using Microsoft.SharePoint;
using System.Security.Principal;

[Init]

這邊是底下說明的幾個功能都會使用到的初始化程式碼,用於開啟 SPSite,及裡頭的某個 SPWeb,來進行後續的各項 Alert 動作。

SPSite s;
SPWeb w;
try
{
string mySiteName = args[1];
s = new SPSite(“http://localhost/sites/” + mySiteName);
w = s.OpenWeb();
}
catch
{
Console.WriteLine (“Site name must specify correctlly! Abort!!”);
return;
}

SPAlertCollection alerts = w.Alerts;

[List Alerts]

列出所有在這個 SPWeb 中的 Alerts,包含了 Alert 的 Title 及歸屬的 Username。

alerts = w.Alerts;
foreach (SPAlert alert in alerts)
{
Console.WriteLine( w.Title + ” :: ” + alert.Title + ” :: ” + alert.User.LoginName);
}

[Delete Alerts]

刪除所有在這個 SPWeb 中的 Alert,先列出所有的 Alerts,然後再一個個的刪除掉,也可以加些判斷式,用於只刪除某些 Alert。

alerts = w.Alerts;
foreach (SPAlert alert in alerts)
{
Console.WriteLine(“Delete: ” + w.Title + ” :: ” + alert.Title + ” :: ” + alert.User.LoginName);
SPAlertCollection delAlerts = alert.User.Alerts;
for (int i=delAlerts.Count-1; i>-1;i–)
{
delAlerts.Delete(i);
}
}
Console.WriteLine (“==== Delete Alerts. DONE!! ====”);

[Add Alert]

新增一筆 Alert,這邊的 sUsername 應該是 AD 上頭的 User,由於 SPS 充分與 AD 作了整合,所以 User 的資料會自動的同步。

Console.WriteLine(“Login Name : ” + sUsername);
SPUser u = w.AllUsers[sUsername];
SPAlertCollection UserAlerts = u.Alerts;
SPList myAlertList;
try
{
myAlertList = w.Lists[“Announcements”];
UserAlerts.Add (myAlertList,
Microsoft.SharePoint.SPEventType.Add,
Microsoft.SharePoint.SPAlertFrequency.Daily);
}
catch
{
Console.WriteLine(“Announcements – ERR”);
}

特別注意: 由於我們是在 Console 端的程式,所以可以直接用了 System.Security.Principal; 帶入目前 Login 的 User (應該得是 SPS2003 的 Admin) 帳號執行以上的程式,如果使用其它的 Username Login 的話,會有權限上的問題。為了使 Security.Principal 生效,我們並無法使用 SPSite s = new SPSite(“http://ServerFQDN/sites/” + mySiteName); 的方式來開啟一個 SPSite,而一定得用 localhost 才行,否則使用者的資訊也會帶不進去。因此,上述的程式碼應該應用於 Console 端的 Admin Tool,用於 Force Subscribe /Alert Manage 程式的撰寫。

你以為這樣就結束了? 其實還沒,由於我們是用 http://localhost/ 加入 Alert 的,所以會造成一個小問題,那就是所有送出的 Alert,在網址的部分會以 http://localhost/ 作為開頭,導致使用者無法由 Alert Mail 返回 SPS 讀取資訊,我們還必需額外的對資料庫動點手腳才行。SPS2003 的資料庫名稱應該是像這樣的 xxxxxxxx1_SITE,我們要對此資料庫下一個 UPDATE 的 SQL Command:

UPDATE SchedSubscriptions SET SiteUrl=’http://ServerFQDN’ WHERE siteUrl = ‘http://localhost’

這裡的 ServerFQDN 得修改為你 SPS 的 FQDN,這樣子就可以了。

那麼,在 WebParts 上頭的應用該怎麼辦呢? 有底下的方法,已經實作出來了,可以應用在 WebParts 上,可是限制上明顯比 Console Application 多了許多,應用上也有著更多的陷井存在:

[Init]

using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.WebControls;

SPSite site = SPControl.GetContextSite(Context);
SPWeb myWeb = site.OpenWeb();
myWeb.AllowUnsafeUpdates = true;
string _sUserID = Context.User.Identity.Name;
SPUser u = myWeb.AllUsers[_sUserID];
SPAlertCollection alerts = u.Alerts;

[List Alerts]

alerts = u.Alerts;
foreach (SPAlert alert in alerts)
{
string myCurAlert = alert.Title;
}

[Delete Alerts]

SPAlertCollection delAlerts = myWeb.CurrentUser.Alerts;
for (int idxAlert = delAlerts.Count – 1; idxAlert > -1; idxAlert–)
{
delAlerts.Delete(idxAlert);
}

[Add Alert]

SPList myAlertList;
myAlertList = myWeb.Lists[“Announcements”];
alerts.Add (myAlertList,
Microsoft.SharePoint.SPEventType.Add,
Microsoft.SharePoint.SPAlertFrequency.Daily);

由於在使用上限制多多,而且在對某個 User 資訊進行存取的時候常常會出現權限的問題,透過 WebPart 要使用 Admin 的權限來運行這些功能的話,又會有很多安全性的問題,所以 Alert 部分的客製化可以說不大容易,以上僅供參考,如果您有更好的方法,請大大不吝賜教。

[End]

最後修改日期: 2004-08-26

作者

留言

撰寫回覆或留言

發佈留言必須填寫的電子郵件地址不會公開。