HomeDev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunityDoc feedbackLog In
GitHubNuGetDev CommunityDoc feedback

Feeds

Describes the feeds feature of the Optimizely Community API.

A feed represents a record of the activities occurring within the application. As an activity is added to the system, the platform generates feed items for users subscribed to the entity upon which the activity occurred.

In the Optimizely Community API, feed items can be managed through a service implementing the interface IFeedService. This service provides the ability to retrieve feed items generated by the Activity Streams system.

Manage feeds

In the Optimizely Community API, feed items are retrieved through a service implementing the interface IFeedService.

Access an IFeedService

When the Activity Streams feature is installed to an Optimizely Content Management System (CMS) site, with the feature's site integration package, you can get an instance of the Feed service from the inversion of control (IoC) container.

Example:

var feedService = ServiceLocator.Current.GetInstance<IFeedService>();

When the feature is installed to a non-Optimizely CMS site, you can get an instance of a service from the default factory class provided within the package.

Example:

var factory = new EPiServer.Social.ActivityStreams.DefaultActivityStreamsFactory();
 var feedService = factory.CreateFeedService();

Retrieve feed items

To retrieve a collection of feed items, regardless of the type of payload extending the activity, use the Get(Criteria<FeedItemFilter>) method. This method accepts an instance of Criteria<FeedItemFilter>, which contains the specifications necessary to retrieve the desired feed items.

The Filter property of the Criteria<FeedItemFilter> class accepts an instance of the FeedItemFilter class. This class contains the specifications that allow you to refine the result set of FeedItems that you want to retrieve.

  • Actor – Assigning a value (Reference) to this property refines the result set to FeedItems representing activities initiated by the identified user.
  • Subscriber– Assigning a value (Reference) to this property refines the result set to FeedItems representing activities on targets to which the identified user is subscribed.
  • Target– Assigning a value (Reference) to this property refines the result set to FeedItems representing activities targeting the identified entity.

You can apply the specifications of the FeedItemFilter in conjunction with one another. Each specification, which is assigned a value in the filter, further refines the result set (that is, a logical AND). In the following example, a page of FeedItems is retrieved for a particular subscriber.

IFeedService feedService;
// ...

var subscriber = Reference.Create("user://identifier/for/a/user");
var criteria = new Criteria<FeedItemFilter>
  {
    Filter = new FeedItemFilter()
      {
        Subscriber = subscriber
      }                
  };
var pageOfFeedItems = feedService.Get(criteria);

In the previous example, the request to retrieve feed items is invoked synchronously. An example of retrieving feed items asynchronously using the asynchronous overload with C#'s async and await keywords is described below.

private async Task<ResultPage<FeedItem>> GetFeedItemsAsync(IFeedService feedService)
  {
    // ...

    var subscriber = Reference.Create("user://identifier/for/a/user");
    var criteria = new Criteria<FeedItemFilter>
      {
        Filter = new FeedItemFilter()
          {
            Subscriber = subscriber
          }                
      };
    var getFeedItemsTask = feedService.GetAsync(criteria);

    //Do other application specific work in parallel while the task executes.
    //....

    //Wait until the task runs to completion.
    var pageOfFeedItems = await getFeedItemsTask;
    return pageOfFeedItems;
  }

The FeedItemSortFields class exposes a set of fields upon which a result set of feed items may be ordered. These fields may be applied in the construction of sorting rules that you associate with your criteria. These fields include

  • FeedItemSortFields.ActivityId
  • FeedItemSortFields.ActivityDate
  • FeedItemSortFields.Actor
  • FeedItemSortFields.Target

The following example demonstrates how to apply sorting rules to order your result set by Target reference, alphabetically in ascending order. A fallback rule, subsequently ordering results with the same Target reference according to when the activity occurred from newest to oldest, is also added.

var criteria = new Criteria<FeedItemFilter>
  {
    // ...

    OrderBy = new List<SortInfo>
      {
        new SortInfo(FeedItemSortFields.Target, true),
        new SortInfo(FeedItemSortFields.ActivityDate, false),
      }
  };

For details regarding the use of criteria, including information on paging and sorting, see Criteria in Discover the platform.

Retrieve composite feed items

To retrieve a collection of FeedItems with their accompanying extension data, use the Get<TExtension>(CompositeCriteria\<FeedItemFilter,TExtension>) method. This method accepts an instance of CompositeCriteria\<FeedItemFilter,TExtension>, which contains the specifications necessary to retrieve the desire FeedItems.

The Filter property of the CompositeCriteria\<FeedItemFilter,TExtension> class accepts an instance of the FeedItemFilter class. This class contains the specifications that let you refine the result set of FeedItems that you want to retrieve.

The ExtensionFilter property of the CompositeCriteria\<FeedItemFilter,TExtension> class accepts a FilterExpression, which lets you specify a Boolean expression to further refine the result set by values represented within your extension data. For more information on this type of filter, see Composite Criteria and Filtering Composistes oin Discover the platform.

In the following example, a page of FeedItems composed with the PageRated (a sample class used in earlier examples) is retrieved. The criteria combine a filter native to IFeedService with a custom one targeting a field the PageRated extension data.

More specifically, this sample demonstrates the retrieval of feed items targeting a specific resource (a page, product, etc.), upon which a PageRated activity has occurred where rating of 5 was assigned.

IFeedService feedService;
// ...

var criteria = new CompositeCriteria<FeedItemFilter, PageRated>
  {
    Filter = new FeedItemFilter
      {
        Target = Reference.Create("resource://identifier/for/a/resource")
      },
    ExtensionFilter = FilterExpressionBuilder<PageRated>.Field(pr => pr.Rating).EqualTo(5)
  };      
var feedPage = feedService.Get<PageRated>(criteria);

In the previous example, the request to retrieve feed items is invoked synchronously. An example of retrieving feed items asynchronously using the asynchronous overload with C#'s async and await keywords is described below.

private async Task<ResultPage<Composite<FeedItem, PageRated>>> GetFeedItemsAsync(IFeedService feedService)
 {
    // ...

    var criteria = new CompositeCriteria<FeedItemFilter, PageRated>
      {
        Filter = new FeedItemFilter
          {
            Target = Reference.Create("resource://identifier/for/a/resource")
          },
        ExtensionFilter = FilterExpressionBuilder<PageRated>.Field(pr => pr.Rating).EqualTo(5)
      };       
    var feedItemsTask = feedService.GetAsync(criteria);

    //Do other application specific work in parallel while the task executes.
    //....

    //Wait until the task runs to completion.
    var feedItemsPage = await feedItemsTask;
    return feedItemsPage;
  }

The fields of your activity's extension data may also be applied in the sorting of your result set. The example below demonstrates the use of an extension data field in the definition of a sorting rule.

var criteria = new CompositeCriteria<FeedItemFilter, PageRated>
  {
    // ...

    OrderBy = new List<SortInfo>
      {
        new SortInfo(new SortField(FilterExpressionBuilder<PageRated>.Field(payload => payload.Rating)), true),
        new SortInfo(FeedItemSortFields.ActivityDate, false),
      }
  };

For details regarding the use of criteria, including information on paging and sorting, see Criteria in Discover the platform.