Muokkaa

Jaa


Follow documents and sites by using the .NET client object model in SharePoint

Learn how to work with Following Content features by using the SharePoint .NET client object model.

How do I use the .NET client object model to follow content?

SharePoint users can follow documents, sites, and tags can follow documents, sites, and tags to get updates about the items in their newsfeeds and to quickly open followed documents and sites. You can use the .NET client object model in your app or solution to start following content, stop following content, and get followed content on behalf of the current user. This article shows you how to create a console application that uses the .NET client object model to work with Following Content features for documents and sites.

The following objects are the primary APIs for Following Content tasks:

Note

You also use these APIs for Following People tasks, but the GetSuggestions and GetFollowers methods available from SocialFollowingManager only support following people, not content. For more information about how you can use SocialFollowingManager , see Follow content in SharePoint and Follow people in SharePoint. For code examples that show how to follow people, see How to: Follow people by using the .NET client object model in SharePoint.

Prerequisites for setting up your development environment to work with Following Content features by using the SharePoint .NET client object model

To create a console application that uses the .NET client object model to work with Following Content features for documents and sites, you'll need the following:

  • SharePoint with My Site configured, with the My Site site created for the current user, and with a document uploaded to a SharePoint document library

  • Visual Studio 2012

  • Full Control access permissions to the User Profile service application for the logged-on user

Note

If you are not developing on the computer that is running SharePoint, get the SharePoint Client Components download that contains SharePoint client assemblies.

Create a console application to work with Following Content features by using the SharePoint .NET client object model

  1. In Visual Studio, choose File, New, Project.

  2. In the New Project dialog box, choose .NET Framework 4.5 from the drop-down list at the top of the dialog box.

  3. In the Templates list, choose Windows, and then choose the Console Application template.

  4. Name the project FollowContentCSOM, and then choose the OK button.

  5. Add references to the following assemblies:

  • Microsoft.SharePoint.Client
  • Microsoft.SharePoint.ClientRuntime
  • Microsoft.SharePoint.Client.UserProfiles
  1. Replace the contents of the Program class with the code example from one of the following scenarios:
  1. To test the console application, on the menu bar, choose Debug, Start Debugging.

Code example: Start and stop following content by using the SharePoint .NET client object model

The following code example makes the current user start following or stop following a target item. It shows how to:

  • Check whether the current user is following a particular document or site by using the IsFollowed method.

  • Start following a document or site by using the Follow method.

  • Stop following a document or site by using the StopFollowing method.

  • Get the count of documents or sites that the current user is following by using the GetFollowedCount method.

This code example uses the SocialFollowResult object that is returned by the Follow method to determine whether to start or stop following the target item.

Note

Change the placeholder values for the serverUrl and contentUrl variables before you run the code. To use a site instead of a document, use the variables that are commented out.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Social;

namespace FollowContentCSOM
{
    class Program
    {
        static ClientContext clientContext;
        static SocialFollowingManager followingManager;
        static void Main(string[] args)
        {

            // Replace the following placeholder values with the URL of the target
            // server and target document (or site).
            const string serverUrl = "http://serverName";
            const string contentUrl = @"http://serverName/libraryName/fileName";
            const SocialActorType contentType = SocialActorType.Document;
            // Do not use a trailing '/' for a subsite.
            //const string contentUrl = @"http://serverName/subsiteName"; 
            //const SocialActorType contentType = SocialActorType.Site;

            // Get the client context.
            clientContext = new ClientContext(serverUrl);

            // Get the SocialFeedManager instance.
            followingManager = new SocialFollowingManager(clientContext);

            // Create a SocialActorInfo object to represent the target item.
            SocialActorInfo actorInfo = new SocialActorInfo();
            actorInfo.ContentUri = contentUrl;
            actorInfo.ActorType = contentType;

            // Find out whether the current user is following the target item.
            ClientResult<bool> isFollowed = followingManager.IsFollowed(actorInfo);

            // Get the information from the server.
            clientContext.ExecuteQuery();

            Console.WriteLine("Was the current user following the target {0}? {1}\\n",
                actorInfo.ActorType.ToString().ToLower(), isFollowed.Value);
            Console.Write("Initial count: ");

            // Get the current count of followed items.
            WriteFollowedCount(actorInfo.ActorType);

            // Try to follow the target item. If the result is OK, then
            // the request succeeded.
            ClientResult<SocialFollowResult> result = followingManager.Follow(actorInfo);
            clientContext.ExecuteQuery();

            // If the result is AlreadyFollowing, then stop following 
            // the target item.
            if (result.Value == SocialFollowResult.AlreadyFollowing)
            {
                followingManager.StopFollowing(actorInfo);
                clientContext.ExecuteQuery();
            }

            // Handle other SocialFollowResult return values.
            else if (result.Value == SocialFollowResult.LimitReached
                || result.Value == SocialFollowResult.InternalError)
            {
                Console.WriteLine(result.Value);
            }

            // Get the updated count of followed items.
            Console.Write("Updated count: ");
            WriteFollowedCount(actorInfo.ActorType);
            Console.ReadKey();
        }

        // Get the count of the items that the current user is following.
        static void WriteFollowedCount(SocialActorType type)
        {

            // Set the parameter for the GetFollowedCount method, and
            // handle the case where the item is a site. 
            SocialActorTypes types = SocialActorTypes.Documents;
            if (type != SocialActorType.Document)
            {
                types = SocialActorTypes.Sites;
            }

            ClientResult<int> followedCount = followingManager.GetFollowedCount(types);
            clientContext.ExecuteQuery();
            Console.WriteLine("{0} followed {1}", followedCount.Value, types.ToString().ToLower());
        }
    }
}

Code example: Get followed content by using the SharePoint .NET client object model

The following code example gets the documents and sites that the current user is following and gets information about the user's Following Content status. It shows how to:

  • Check whether the current user is following the target document and site by using the IsFollowed method.

  • Get the count of documents and sites that the current user is following by using the GetFollowedCount method.

  • Get the documents and sites that the current user is following by using the GetFollowed method.

  • Iterate through the groups of content and get each item's name, content URI, and URI.

Note

Change the placeholder value for the serverUrl, docContentUrl, and siteContentUrl variables before you run the code.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Social;

namespace FollowContentCSOM
{
    class Program
    {
        static void Main(string[] args)
        {

            // Replace the following placeholder values with the URLs of
            // the target server, document, and site.
            const string serverUrl = "http://serverName";
            const string docContentUrl = @"http://serverName/libraryName/fileName";
            const string siteContentUrl = @"http://serverName/subsiteName"; // do not use a trailing '/' for a subsite

            // Get the client context.
            ClientContext clientContext = new ClientContext(serverUrl);

            // Get the SocialFollowingManager instance.
            SocialFollowingManager followingManager = new SocialFollowingManager(clientContext);

            // Create SocialActorInfo objects to represent the target 
            // document and site.
            SocialActorInfo docActorInfo = new SocialActorInfo();
            docActorInfo.ContentUri = docContentUrl;
            docActorInfo.ActorType = SocialActorType.Document;
            SocialActorInfo siteActorInfo = new SocialActorInfo();
            siteActorInfo.ContentUri = siteContentUrl;
            siteActorInfo.ActorType = SocialActorType.Site;

            // Find out whether the current user is following the target
            // document and site.
            ClientResult<bool> isDocFollowed = followingManager.IsFollowed(docActorInfo);
            ClientResult<bool> isSiteFollowed = followingManager.IsFollowed(siteActorInfo);

            // Get the count of documents and sites that the current
            // user is following.
            ClientResult<int> followedDocCount = followingManager.GetFollowedCount(SocialActorTypes.Documents);
            ClientResult<int> followedSiteCount = followingManager.GetFollowedCount(SocialActorTypes.Sites);

            // Get the documents and the sites that the current user
            // is following.
            ClientResult<SocialActor[]> followedDocResult = followingManager.GetFollowed(SocialActorTypes.Documents);
            ClientResult<SocialActor[]> followedSiteResult = followingManager.GetFollowed(SocialActorTypes.Sites);

            // Get the information from the server.
            clientContext.ExecuteQuery();

            // Write the results to the console window.
            Console.WriteLine("Is the current user following the target document? {0}", isDocFollowed.Value);
            Console.WriteLine("Is the current user following the target site? {0}", isSiteFollowed.Value);
            if (followedDocCount.Value > 0)
            {
                IterateThroughContent(followedDocCount.Value, followedDocResult.Value);
            } if (followedSiteCount.Value > 0)
            {
                IterateThroughContent(followedSiteCount.Value, followedSiteResult.Value);
            }
            Console.ReadKey();
        }

        // Iterate through the items and get each item's display
        // name, content URI, and absolute URI.
        static void IterateThroughContent(int count, SocialActor[] actors)
        {
            SocialActorType actorType = actors[0].ActorType;
            Console.WriteLine("\\nThe current user is following {0} {1}s:", count, actorType.ToString().ToLower());
            foreach (SocialActor actor in actors)
            {
                Console.WriteLine("  - {0}", actor.Name);
                Console.WriteLine("\\tContent URI: {0}", actor.ContentUri);
                Console.WriteLine("\\tURI: {0}", actor.Uri);
            }
        }
    }
}

See also