SharePoint 2013: How to Get Following and Followers of User by using JSOM or JavaScript

SharePoint 2013: How to Get Following and Followers of User by using JSOM or JavaScript

Using Social Information in SharePoint 2013

 Getting Following and followers of a specific or current user in SharePoint 2013 is very easy. SharePoint 2013 has provided client side object model library for the social activities, which enables deep interaction with social services. For JavaScript side object model you need to load the following .js file in order to start coding against User profile: "SP.UserProfiles.js". 

In this article we will see how to fetch the following and followers of a specific or current user inside JavaScript client side.
 

First step

First of all you need to load the “SP.UserProfiles.js, SP.Runtime.js and SP.js” js files on your SharePoint page, use following code snippet to load these files, 

    $(document).ready(function () {
        var scriptbase = _spPageContextInfo.webAbsoluteUrl + "/_layouts/15/";
        $.getScript(scriptbase + "SP.Runtime.js",
            function () {
                $.getScript(scriptbase + "SP.js",
                   function () {
                       $.getScript(scriptbase + "SP.UserProfiles.js", GetUserInformation_FollwersAndFollowing);
                   });
            });
        });

 Here I am using “$(document).ready”, so you need to refer “jquery-1.6.2.min.js” (or later version of jquery) also. Now once the script files loaded successfully you code is ready to go. So to fetch all followers and following users first of all get the current client context. After that you need to initialize the following manager object. This is the class which provides all the information regarding following/followers in social. The below code snippet gives you the followers and following users. Also you can get the documents to which the user is following.


Second Step

function GetUserInformation_FollwersAndFollowing() {

        // Get the current client context. 
        var clientContext = SP.ClientContext.get_current();

        // Get the following Manager. 
        followingManager = new SP.Social.SocialFollowingManager(clientContext);
        //get the Social feed info by using SocialActor info object 
        var actorInfo = new SP.Social.SocialActorInfo();

        actorInfo.AccountName = “domain\\userID”; 
         
        // Get all followers which followers 
        followers = followingManager.getFollowers();
        // Get all users to which following
        
following = followingManager.getFollowed(1); 

        clientContext.executeQueryAsync(onSuccess, onFail);

    }

 

    function onSuccess() {
        // Display followers and Following users on page. 
    }

 

    function onFail(sender, args) {
        alert("Error: " + args.get_message());
    }

 

So once “followers” and “following” gets initialized in “onSuccessForDocuments()” method you can display this data on the page as per the requirement. You can get full information of all followers and following users.

In this way you get started with social programming for SharePoint 2013.



See Also


Other languages

Leave a Comment
  • Please add 6 and 2 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Richard Mueller edited Revision 5. Comment: Fixed zero character in <a name> tag in heading in HTML so TOC works properly

  • Naomi  N edited Revision 2. Comment: Minor edit, added toc

Page 1 of 1 (2 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Very userful

  • Naomi  N edited Revision 2. Comment: Minor edit, added toc

  • Naomi  N edited Revision 4. Comment: Formatting

  • Richard Mueller edited Revision 5. Comment: Fixed zero character in <a name> tag in heading in HTML so TOC works properly

Page 1 of 1 (4 items)