SharePoint 2013 Retrieving Followed Documents

Retrieve the Documents which are followed by the Current user in SharePoint 2013 Using Client object model(REST API, JavaScript and HTML) and Displaying it in the simple HTML table.

Create a Content Editor web part in any of Your SharePoint page where the following documents has to be displayed. Copy and paste the code given below in that content Editor Web part. This code will just display only five followed documents.

  1. <html>  
  2. <head>  
  3. <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>  
  4. <script type="text/javascript">  
  5.    var followingEndpoint;  
  6.    var URL;  
  7.    var website;  
  8.    var clientContext;  
  9.    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', loadWebsite);  
  10.    function loadWebsite() {  
  11.    clientContext = SP.ClientContext.get_current();  
  12.    website = clientContext.get_web();  
  13.    clientContext.load(website);  
  14.    clientContext.executeQueryAsync(onRequestSucceeded, onRequestFailed);  
  15.    }  
  16.    function onRequestSucceeded() {  
  17.    URL = website.get_url();  
  18.    followingEndpoint = decodeURIComponent(URL) + "/_api/social.following";  
  19.    getFollowedDocs();  
  20. }  
  21.    function onRequestFailed(sender, args) {  
  22.    alert('Error: ' + args.get_message());  
  23. }  
  24.    function getFollowedDocs() {  
  25.    $.ajax({  
  26.    url: followingEndpoint + "/my/Followed(types=2)",  
  27.    headers: { "accept": "application/json;odata=verbose" },     
  28.    success: retrieveFollowedDocs,  
  29.    error: function (xhr, ajaxOptions, thrownError) {  
  30.    alert("GET error:\n" + xhr.status + "\n" + thrownError);  
  31.    }  
  32.    });  
  33.    }  
  34.    function retrieveFollowedDocs(data) {     
  35. var stringData = JSON.stringify(data);  
  36. var jsonObject = JSON.parse(stringData);  
  37. var followed = jsonObject.d.Followed.results;  
  38. //var threads = followed.results;  
  39. var followedContent = '';  
  40. followedContent +='<table><thead><tr><th>Title</th></tr></thead></table><ul>';  
  41. for (var i = 0; i < followed.length; i++)  
  42. {  
  43. //The Below if condition is for the Document limit. I need 5 documents to be displayed so given i=5. Based on your requirement you can the number.  
  44. if (i ==5)  
  45. {  
  46.    var qryString = window.location.href;  
  47.    var qrySplit = qryString.split('?');  
  48.    if (qrySplit.length > 1)  
  49. {  
  50.    continue;  
  51. }  
  52.    else  
  53. {  
  54.       break;  
  55. }  
  56. }  
  57. var folDocs = followed[i];  
  58. var NamewithExtn = folDocs.Name;  
  59. var docName = NamewithExtn.split('.');  
  60. var Name = docName[0];  
  61. var ContentURL = folDocs.Uri;  
  62. followedContent += '<li>';  
  63. followedContent += '<table><tbody><tr><td><a href="'+URL+'">'+Name+'</a>';  
  64. followedContent += '</td></tr></tbody></table>'  
  65. followedContent += '</li>'  
  66. }  
  67. followedContent +='</ul>';  
  68. $("#message").empty();  
  69. $("#message").append(followedContent);  
  70. innerTable = '';  
  71. }  
  72. </script>  
  73. </head>  
  74. <body>  
  75. <div id="message"></div>  
  76. </body>  
  77. </html>  
Ebook Download
View all
Learn
View all