共用方式為


How to: Create and Insert Events on a User's Newsfeed

Applies to: SharePoint Server 2010

Microsoft SharePoint Server 2010 enables you to extend the My Newsfeed page on the My Site host by creating and inserting events into a single user’s newsfeed, or by multicasting events into several users’ newsfeeds. The samples below follow the same pattern. They create an activity in which the owner and the publisher are the same user. They then insert the event into the newsfeeds of other users by copying the event and identifying the original user’s colleagues as the owners of the copies. See Code Sample: Multicasting Activity Events Console Application and Code Sample: Send a Link to Your Colleagues Activity Event for code samples that demonstrate how to perform tasks that are included in this topic. This topic assumes that you have added the following references to your Microsoft Visual Studio 2010 project:

Important

To protect user privacy, you must have administrative permissions for the User Profile Service Application to create activity events and insert them into users’ newsfeeds.

  • Microsoft.SharePoint

  • Microsoft.Office.Server

  • Microsoft.Office.Server.UserProfiles

  • System.Web

Creating and Inserting an Event into User Newsfeeds

The sample method below creates an ActivityEvent object for a hypothetical ActivityType that represents the action of a user who sends a URL and explanatory text to all of the user’s colleagues. The method takes UserProfile objects that represent the owner and publisher of the event, an ActivityType object that the ActivityManager can use to create a new ActivityEvent, and strings that represent the URL and the explanatory text tied to the event.

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;
}

This CreateActivity method can create the original ActivityEvent in which the owner and publisher are the same user. It also can insert the same event into the newsfeeds of other users. The sample below loops through the list of user colleagues and creates an activity with each colleague identified as the owner.

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

Multicasting Events into User Newsfeeds

The ActivityFeedGatherer class enables you to multicast events to multiple users. The methods in the ActivityFeedGatherer class can be especially useful in cases where you want to insert events from multiple publishers into the newsfeeds of multiple users. You should also use this approach whenever site performance is a concern.

The following sample method uses four important methods in the ActivityFeedGatherer class.

The GetUsersColleaguesAndRights method takes a list of owner identifiers from a list of ActivityEvent objects and populates two Dictionary objects that are passed as output parameters. The first Dictionary object maps each owner identifier to a MinimalPerson object that represents each owner. The second Dictionary object maps each owner identifier to a list of MinimalPerson objects that represent the colleagues of each owner.

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

The MulticastActivityEvents method takes a list of ActivityEvent objects and distributes them to the colleagues that are returned in the second Dictionary. It does this by making copies of each ActivityEvent object and making each colleague the owner of a new copy. The MulticastActivityEvents method also populates another Dictionary object that is passed as an output parameter. This Dictionary maps each new owner to the list of newly copied ActivityEvent objects.

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

These new events need to be batch written to the database. The CollectActivityEventsToConsolidate method is a helper method that turns this eventsPerOwner dictionary into a flat list of events that can then be passed to the BatchWriteActivityEvents method.

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

The sample method that follows combines all of these ActivityFeedGatherer methods. It also iterates through the list of ActivityEvent objects that need to be written to the database so that it does not exceed the value of the MaxEventsPerBatch property of the ActivityManager object.

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);
}

See Also

Reference

Microsoft.Office.Server.ActivityFeed

Concepts

How to: Get Events for Users

How to: Create a New Activity Type

Other Resources

Microsoft SharePoint Server 2010: Activity Feeds Console Application

Creating Resource Files