2015-11-04 43 views
0

我试图在更改源项目后获取克隆上的通知。在我们的系统中,当源项目发生更改时,克隆也会自动更改。但是,我们需要自动拒绝Sitecore通知,说明“原始项目中的某个字段已更改”并提供审阅/接受/拒绝选项。问题是在克隆上使用GetNotifications()会返回0个元素 - 这意味着Sitecore没有找到任何通知。但是,当我重新加载/重新打开克隆时,我清楚地看到它们。Sitecore NotificationProvider.GetNotifications为空

我试着重装使用项目:运行GetNotifications()之前

item.Reload(); 

Context.ClientPage.SendMessage(this, "item:load(id=" + item.ID + ")"); 

,但既不发出的通知大于零的计数。

这是我使用的完整的代码(其中copyItem是我的克隆)int k是一个测试,它返回0

 using (new SecurityDisabler()) 
     { 
      if (copyItem.IsClone) 
      { 
       var notifies = Database.GetDatabase("master").NotificationProvider.GetNotifications(copyItem); 

       int k = -1; 

       if (notifies != null) k = notifies.Count(); 

       foreach (Notification n in notifies) 
       { 
        n.Reject(copyItem); 
       } 
      } 
     } 

注:我打电话OnItemSaved事件下上面的代码。

回答

0

我发现了一个helpful post,它提供了一些与我使用的代码稍有不同的东西。它使用Sitecore作业(异步/后台进程)来运行通知检查,并且也有轻微的延迟。现在这似乎对我很好。最后,这些通知不再显示!

我最后的代码是:

public void OnItemSaved(object sender, EventArgs args) 
    { 
     var item = Event.ExtractParameter(args, 0) as Item; 

     ReReferenceFieldAndRemoveNotifications(item, args); 

     ... 
    } 

    private void ReReferenceFieldAndRemoveNotifications(Item srcItem, EventArgs args) 
    { 
     if (srcItem != null && !srcItem.Paths.Path.ToLower().Contains(string.Format("{0}/{1}", "content", "canada"))) 
     { 
      var destItem = Database.GetDatabase("master").GetItem(srcItem.Paths.Path.Replace("United States", "Canada")); 

      // Update the clone 
      Rereferencer.RereferenceFields(srcItem, destItem); 

      // Now reject the notifications on the clone (accepting would push the US values which we don't want) 
      using (new SecurityDisabler()) 
      { 
       if (srcItem.HasClones) 
       { 
        var jobOptions = new JobOptions("RejectNotifications", string.Empty, Context.GetSiteName(), this, "RejectNotifications", new object[] { srcItem }); 
        var job = new Job(jobOptions); 
        jobOptions.InitialDelay = new TimeSpan(0, 0, 0, 1, 0); 
        JobManager.Start(job); 
       } 
      } 
     } 
    } 

    public void RejectNotifications(Item args) 
    { 
     // Remove and reject any notifications on the clone. 
     using (new SecurityDisabler()) 
     { 
      var item = args; 
      var clones = item.GetClones(true); 
      foreach (var clone in clones) 
      { 
       var notifications = clone.Database.NotificationProvider.GetNotifications(clone); 
       foreach (var notification in notifications) 
       { 
        clone.Database.NotificationProvider.RemoveNotification(notification.ID); 
        notification.Reject(clone); 
       } 

       clone.Editing.BeginEdit(); 
       try 
       { 
        clone.Fields["__Workflow"].Value = args.Fields["__Workflow"].Value; 
        clone.Fields["__Workflow state"].Value = args.Fields["__Workflow state"].Value; 
       } 
       finally 
       { 
        clone.Editing.EndEdit(); 
       } 

      } 
     } 
    } 

注:ReReference代码没有与此相关的解决方案,你不需要这个。