SharePoint 2013 Hosting :: How To Retrieve Followed Sites in SharePoint 2013 Using REST API

This article explains how to retrieve the site name and URL followed by the current user in SharePoint 2013 using a client object model (REST API and JavaScript)

We can use the SharePoint 2013 Representational State Transfer (REST) service to do the same tasks you can do when you use the .NetCSOM, JSOM.
Here I explain how to retrieve the site name and URL followed by the current user in SharePoint 2013 using a client object model (REST API and JavaScript) and displaying it in the SharePoint page.

  • Create a new page and a Content Editor Webpart (CEWP)

Create a New page

  • Edit the web part that was added to the page.
  • Upload your text file script into the site assests and copy the path of the text file and paste it into the Content link in CEWP.

Code

The following example shows how to retrieve all of the following sites:

<html>    
    <head>    
       <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>    
          <script type="text/javascript">  
             var followingManagerEndpoint;  
             var followedCount; 
             var followingEndpoint;    
             var URL;    
             var website;    
             var clientContext;  
             SP.SOD.executeFunc('sp.js', 'SP.ClientContext', loadWebsite);    
             function loadWebsite() {    
                clientContext = SP.ClientContext.get_current();    
                website = clientContext.get_web();    
                clientContext.load(website);    
                clientContext.executeQueryAsync(onRequestSucceeded, onRequestFailed);    
             }    
             function onRequestSucceeded() {   
               URL = website.get_url();    
               followingManagerEndpoint = decodeURIComponent(URL) + "/_api/social.following";   
               getMyFollowedContent();  
            }    
            function onRequestFailed(sender, args) {    
              alert('Error: ' + args.get_message());    
            }   
            // Get the content that the current user is following.  
            // The "types=14" parameter specifies all content types  
            // (documents = 2 + sites = 4 + tags = 8).  
           function getMyFollowedContent() {  
             $.ajax( {  
                  url: followingManagerEndpoint + "/my/followed(types=14)",  
                  headers: {   
                      "accept": "application/json;odata=verbose"  
                  },  
                  success: followedContentRetrieved,  
                  error: requestFailed  
            });  
         }  
         // Parse the JSON data and iterate through the collection.  
         function followedContentRetrieved(data) {  
             var stringData = JSON.stringify(data);  
             var jsonObject = JSON.parse(stringData);   
             var types = {  
               1: "document",  
               2: "site",  
               3: "tag"   
            };  
            var followedActors = jsonObject.d.Followed.results;   
            var followedList = "You're following items:";  
            for (var i = 0; i < followedActors.length; i++) {  
               var actor = followedActors[i];  
               followedList += "<p>The " + types[actor.ActorType] + ": \"" +actor.Name + "\"</p>"+"<p>Site URL " + ": \"" +  
               actor.Uri+ "\"</p>";;  
            }   
            $("#Follow").html(followedList);   
         }  
         function requestFailed(xhr, ajaxOptions, thrownError) {  
           alert('Error:\n' + xhr.status + '\n' + thrownError + '\n' + xhr.responseText);  
         }  
       </script>    
    </head>    
 <body>    
 <div id="Follow"></div>    
 </body>    
 </html>

Happy Coding :)

Best Recommended SharePoint 2013 Hosting

ASPHostPortal.com

ASPHostPortal.com is Perfect, suitable hosting plan for a starter in SharePoint. ASPHostPortal  the leading provider of Windows hosting and affordable SharePoint Hosting. ASPHostPortal proudly working to help grow the backbone of the Internet, the millions of individuals, families, micro-businesses, small business, and fledgling online businesses. ASPHostPortal has ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2015, .NET 5/ASP.NET 4.5.2, ASP.NET MVC 6.0/5.2, Silverlight 6 and Visual Studio Lightswitch, ASPHostPortal guarantees the highest quality product, top security, and unshakeable reliability, carefully chose high-quality servers, networking, and infrastructure equipment to ensure the utmost reliability