如何:在用户的新闻流中创建和插入事件

上次修改时间: 2010年3月30日

适用范围: SharePoint Server 2010

Microsoft SharePoint Server 2010 允许您通过以下方式扩展"我的网站"宿主上的"我的新闻源"页:创建事件并将其插入到单个用户的新闻源中,或者将事件多播到几个用户的新闻源中。下面的示例遵循相同的模式。它们创建所有者和发布者是同一用户的活动,然后通过以下方式将事件插入到其他用户的新闻源中:复制事件并将原始用户的同事标识为副本的所有者。有关演示如何执行本主题中包括的任务的代码示例,请参阅代码示例:多播活动事件控制台应用程序代码示例:向您的同事活动事件发送链接。本主题假定您已将下列引用添加到 Microsoft Visual Studio 2010 项目:

重要注释重要说明

为了保护用户隐私,必须对 User Profile Service 应用程序具有管理权限才能创建活动事件并将其插入到用户的新闻源中。

  • Microsoft.SharePoint

  • Microsoft.Office.Server

  • Microsoft.Office.Server.UserProfiles

  • System.Web

创建事件并将其插入到用户新闻源中

下面的示例方法为假设的 ActivityType 创建一个 ActivityEvent 对象,该对象表示将 URL 和解释性文本发送给所有同事的用户执行的操作。该方法采用了表示事件的所有者和发布者的 UserProfile 对象,ActivityManager 可用于创建新 ActivityEvent 的 ActivityType 对象,以及表示绑定到事件的 URL 和解释性文本的字符串。

public ActivityEvent CreateActivity(UserProfile ownerProfile, UserProfile publisherProfile, ActivityType activityType, string linkText, string linkUrl)
{
  //Get the owner and publisher profile.
  Entity ownerMP = new MinimalPerson(ownerProfile).CreateEntity(activityManager);
  Entity publisherMP = new MinimalPerson(publisherProfile).CreateEntity(activityManager);

  //Create the activity and set properties.
  ActivityManager am = new ActivityManager();
  ActivityEvent activityEvent = ActivityEvent.CreateActivityEvent(am, activityType.ActivityTypeId, ownerMP, publisherMP);

  activityEvent.Name = activityType.ActivityTypeName;
  activityEvent.ItemPrivacy = (int)Privacy.Public;
  activityEvent.Owner = ownerMP;
  activityEvent.Publisher = publisherMP;

  //Create the link for the activity.
  Link link = new Link();
  link.Href = linkUrl;
  link.Name = linkText;
  activityEvent.Link = link;
  //Save your work.
  activityEvent.Commit();

  return activityEvent;
}

此 CreateActivity 方法可以创建所有者和发布者是同一用户的原始 ActivityEvent。它还可以将同一事件插入到其他用户的新闻源中。下面的示例循环访问用户同事的列表,并创建将每个同事标识为所有者的活动。

foreach (Colleague colleague in publisher.Colleagues.GetItems())
{
  //Set the owner of the activity to the colleague.
  CreateActivity(colleague.Profile, publisher, activityType, txtNote.Text, strUrl);
}

将事件多播到用户新闻源中

ActivityFeedGatherer 类允许您将事件多播给多个用户。如果您要将来自多个发布者的事件插入到多个用户的新闻源中,则 ActivityFeedGatherer 类中的方法可能特别有用。在网站性能成为关注的问题时,也应该使用此方法。

以下示例方法使用 ActivityFeedGatherer 类中的四个重要方法。

GetUsersColleaguesAndRights 方法采用 ActivityEvent 对象列表中的所有者标识符列表,并填充两个作为输出参数传递的 Dictionary 对象。第一个 Dictionary 对象将每个所有者标识符映射到表示每个所有者的 MinimalPerson 对象。第二个 Dictionary 对象将每个所有者标识符映射到表示每个所有者的同事的 MinimalPerson 对象列表。

ActivityFeedGatherer.GetUsersColleaguesAndRights(m_ActivityManager, publishers, out owners, out colleaguesOfOwners);

MulticastActivityEvents 方法采用 ActivityEvent 对象列表并将这些对象分发给在第二个 Dictionary 中返回的同事。为此,该方法会制作每个 ActivityEvent 对象的副本,并使每个同事成为新副本的所有者。MulticastActivityEvents 方法还填充另一个作为输出参数传递的 Dictionary 对象。此 Dictionary 会将每个新所有者映射到新复制的 ActivityEvent 对象的列表。

ActivityFeedGatherer.MulticastActivityEvents(m_ActivityManager, m_ActivityEvents, colleaguesOfOwners, out eventsPerOwner);

这些新事件需要批量写入数据库。CollectActivityEventsToConsolidate 方法是一个帮助程序方法,可将此 eventsPerOwner 词典转换为可以传递到 BatchWriteActivityEvents 方法的简单事件列表。

ActivityFeedGatherer.CollectActivityEventsToConsolidate(eventsPerOwner, out eventsToWrite);
ActivityFeedGatherer.BatchWriteActivityEvents(eventsToWrite, startIndex, m_ActivityManager.MaxEventsPerBatch);

接下来的示例方法合并所有这些 ActivityFeedGatherer 方法。它还循环访问需要写入到数据库中的 ActivityEvent 对象列表,以便不会超过 ActivityManager 对象的 MaxEventsPerBatch 属性的值。

public void MulticastPublishedEvents(List<ActivityEvent> m_ActivityEvents, ActivityManager m_ActivityManager)
{
   if (m_ActivityEvents.Count == 0)
   return;

   List<long> publishers = new List<long>();

//Populate the list of publishers.
   foreach (ActivityEvent activityEvent in m_ActivityEvents)
   {
      if (!publishers.Contains(activityEvent.Owner.Id))
      publishers.Add(activityEvent.Owner.Id);
   }

//Create the Dictionary objects that will contain the owners and colleagues.
   Dictionary<long, MinimalPerson> owners;
   Dictionary<long, List<MinimalPerson>> colleaguesOfOwners;
   Dictionary<long, List<ActivityEvent>> eventsPerOwner;

//Get the colleagues of the ActivityEvent publishers.
   ActivityFeedGatherer.GetUsersColleaguesAndRights(m_ActivityManager, publishers, out owners, out colleaguesOfOwners);

//Multicast the events.
   ActivityFeedGatherer.MulticastActivityEvents(m_ActivityManager, m_ActivityEvents, colleaguesOfOwners, out eventsPerOwner);

//Create a list of events that need to be written to the database.
   List<ActivityEvent> eventsToWrite;
   ActivityFeedGatherer.CollectActivityEventsToConsolidate(eventsPerOwner, out eventsToWrite);

//Iterate through the eventsToWrite list to ensure that you do not exceed
//the value of the MaxEventsPerBatch property.
   int startIndex = 0;
   while (startIndex + m_ActivityManager.MaxEventsPerBatch < eventsToWrite.Count)
   {
      ActivityFeedGatherer.BatchWriteActivityEvents(eventsToWrite, startIndex, m_ActivityManager.MaxEventsPerBatch);
      startIndex += m_ActivityManager.MaxEventsPerBatch;
   }
   ActivityFeedGatherer.BatchWriteActivityEvents(eventsToWrite, startIndex, eventsToWrite.Count - startIndex);
}

请参阅

引用

Microsoft.Office.Server.ActivityFeed

概念

如何:获取用户的事件

如何:创建新的活动类型

其他资源

Microsoft SharePoint Server 2010:活动源控制台应用程序(该链接可能指向英文页面)

创建资源文件(该链接可能指向英文页面)