2017-01-23 69 views
1

任何人都可以在Sharepoint 2013上使用SPAlert過濾器嗎?SPAlert.Filter無法正常工作

如果我設置SPAlert實例Filter屬性警報尚未發送

SPAlert newAlert = user.Alerts.Add(); 

SPAlertTemplateCollection alertTemplates = new SPAlertTemplateCollection(
(SPWebService)(SPContext.Current.Site.WebApplication.Parent)); 

newAlert.AlertType = SPAlertType.List; 
newAlert.List = list; 
newAlert.Title = alertTitle; 
newAlert.DeliveryChannels = SPAlertDeliveryChannels.Email; 
newAlert.EventType = eventType; 
newAlert.AlertFrequency = SPAlertFrequency.Immediate; 
newAlert.AlertTemplate = alertTemplates[Constants.AlertTemplates.GenericListCustom];        

var wsm = new WorkflowServicesManager(web); 
var wss = wsm.GetWorkflowSubscriptionService(); 
var subscriptions = wss.EnumerateSubscriptionsByList(list.ID); 
bool assotiationExist = false; 
var guid = Constants.Workflows.ApprovalWF.Guid; 
foreach (var subs in subscriptions) 
{ 
    assotiationExist = subs.DefinitionId == guid; 
     if (assotiationExist) 
     { 
       newAlert.Filter = "<Query><Eq><FieldRef Name=\"ApprovalStatus\"/><Value type=\"string\">Approved</Value></Eq></Query>"; 
     } 
}        

newAlert.Update(false); 

回答

0

問題出現在newAlert.EventType = eventType行中。 eventType是SPEventType.Add。這是工作流程將ApprovalStatus字段設置爲«Approved»後不發送警報的原因。

我修改了algourithm。現在eventType是SPEventType.Modify,我添加了新的字段「IsNewAlertSent」列出。當事件第一次觸發時,我發送電子郵件並設置「IsNewAlertSent」字段

最終代碼如下所示。

類UserAlertManager:

.. 
newAlert.EventType = (eventType == SPEventType.Add? SPEventType.Modify: eventType); 
newAlert.AlertFrequency = SPAlertFrequency.Immediate; 
newAlert.AlertTemplate = alertTemplates[Constants.AlertTemplates.GenericListCustom]; 
.. 
      if (assotiationExist) 
      { 
       newAlert.Filter = "<Query><Eq><FieldRef name=\"ApprovalStatus\"/><Value type=\"Text\">Approved</Value></Eq></Query>"; 
       newAlert.Properties.Add("grcustomalert", "1"); 

      } 
.. 
newAlert.Update(false); 

類GRCustomAlertHandler:

... 
string subject = string.Empty; 
string body = string.Empty; 
bool grCustomAlert = Utils.IsSPAlertCustom(ahp.a); 
if (ahp.eventData[0].eventType == (int)SPEventType.Modify && grCustomAlert) 
{ 
      SPListItem item = list.GetItemById(ahp.eventData[0].itemId); 
      var isNewAlertSentField = item.Fields.GetFieldByInternalName(Constants.Fields.IsNewAlertSent); 
      if (isNewAlertSentField != null && (item[Constants.Fields.IsNewAlertSent] == null || !(bool)item[Constants.Fields.IsNewAlertSent])) 
      { 
         ...       
         Utils.SendMail(web, new List<string> { ahp.headers["to"].ToString() }, subject, body); 
           item[Constants.Fields.IsNewAlertSent] = true; 
           using (new DisabledItemEventScope()) 
           { 
            item.SystemUpdate(false); 
           } 
       } 
} 

... 
0

如果我設置Filter屬性上SPAlert實例的警報尚未發送

你這是什麼需要準確嗎?

如果你只是想改變濾波器(警戒狀態),你只需嘗試:

newAlert.AlertType = SPAlertType.List; 
newAlert.List = list; 
newAlert.Title = alertTitle; 
newAlert.DeliveryChannels = SPAlertDeliveryChannels.Email; 
newAlert.EventType = eventType; 
newAlert.AlertFrequency = SPAlertFrequency.Immediate; 
newAlert.AlertTemplate = alertTemplates[Constants.AlertTemplates.GenericListCustom]; 
newAlert.Filter = "<Query><Eq><FieldRef Name=\"ApprovalStatus/New\"/><Value type=\"string\">Approved</Value></Eq></Query>"; 
newAlert.Update(false); 

我剛纔在您的篩選查詢中添加一個/新。警報中的查詢過濾器需要在您的字段中獲取/ New或/ Old。

如果您的提醒仍然不起作用,那麼它可能不是過濾器。