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.
- <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 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();
- followingEndpoint = decodeURIComponent(URL) + "/_api/social.following";
- getFollowedDocs();
- }
- function onRequestFailed(sender, args) {
- alert('Error: ' + args.get_message());
- }
- function getFollowedDocs() {
- $.ajax({
- url: followingEndpoint + "/my/Followed(types=2)",
- headers: { "accept": "application/json;odata=verbose" },
- success: retrieveFollowedDocs,
- error: function (xhr, ajaxOptions, thrownError) {
- alert("GET error:\n" + xhr.status + "\n" + thrownError);
- }
- });
- }
- function retrieveFollowedDocs(data) {
- var stringData = JSON.stringify(data);
- var jsonObject = JSON.parse(stringData);
- var followed = jsonObject.d.Followed.results;
- //var threads = followed.results;
- var followedContent = '';
- followedContent +='<table><thead><tr><th>Title</th></tr></thead></table><ul>';
- for (var i = 0; i < followed.length; i++)
- {
- //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.
- if (i ==5)
- {
- var qryString = window.location.href;
- var qrySplit = qryString.split('?');
- if (qrySplit.length > 1)
- {
- continue;
- }
- else
- {
- break;
- }
- }
- var folDocs = followed[i];
- var NamewithExtn = folDocs.Name;
- var docName = NamewithExtn.split('.');
- var Name = docName[0];
- var ContentURL = folDocs.Uri;
- followedContent += '<li>';
- followedContent += '<table><tbody><tr><td><a href="'+URL+'">'+Name+'</a>';
- followedContent += '</td></tr></tbody></table>'
- followedContent += '</li>'
- }
- followedContent +='</ul>';
- $("#message").empty();
- $("#message").append(followedContent);
- innerTable = '';
- }
- </script>
- </head>
- <body>
- <div id="message"></div>
- </body>
- </html>