Deprecated: YoastSEO_Vendor\Symfony\Component\DependencyInjection\Container::__construct(): Implicitly marking parameter $parameterBag as nullable is deprecated, the explicit nullable type must be used instead in /home/nubelus/sharedove/adisjugo/wp-content/plugins/wordpress-seo/vendor_prefixed/symfony/dependency-injection/Container.php on line 60
Programmatcally retrieve user's newsfeed in SharePoint 2013 - Adis Jugo blog
Select Page

Programmatcally retrieve user’s newsfeed in SharePoint 2013

In enterprise social scenarios with SharePoint 2013, one of the basic tasks is to retrieve user’s newsfeed. Here are the code snippets for doing it with server side and client side code.

Server side:

using (SPSite site = new SPSite(theSite))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPServiceContext context = SPServiceContext.GetContext(site);

        UserProfileManager profileManager = new UserProfileManager(context);

        if (profileManager.UserExists(username))
        {
            UserProfile userProfile = profileManager.GetUserProfile(username);

            //get the Social Feed Manager of the current user
            SPSocialFeedManager feedManager = new SPSocialFeedManager(userProfile, context);

            SPSocialFeedOptions options = new SPSocialFeedOptions()
            {
                MaxThreadCount = 5
            };

            SPSocialFeed feed = feedManager.GetFeedFor(username, options); 

            foreach (SPSocialThread thread in feed.Threads)
            {
                Console.WriteLine(string.Format("Thread: {0} ID: {1} RootPostId {2}", thread.RootPost.Text, thread.Id, thread.RootPost.Id));
            }

        }
        else
        {
            throw new Exception(string.Format("User Profile for user {0} does not exist", username));
        }

    }
}

Client side:

ClientContext context = new ClientContext(siteName);

SocialFeedManager manager = new SocialFeedManager(context);

ClientResult<SocialFeed> feed = manager.GetFeed(SocialFeedType.Personal, new SocialFeedOptions() { MaxThreadCount = 20 });

context.ExecuteQuery();

Console.WriteLine("Threads");
Console.WriteLine("------------------");

foreach (SocialThread thread in feed.Value.Threads)
{
    Console.WriteLine(String.Format("{0}nn {1} - Replays: {2}nnnn", thread.RootPost.Id, thread.RootPost.Text, thread.Replies.Count().ToString()));

    foreach (SocialPost replypost in thread.Replies)
    {
        Console.WriteLine(String.Format("    ----    {0}nn {1} nnnn", replypost.Id, replypost.Text));
    }
}

Previous

Next