Stop Following a Document Using REST

Introduction

This is developed using the NAPA development tool, you can just pass the Document URL in the Text box, on the click of the “Stop Follow” button the Result will be displayed in an alert message.

Step 1

On your Developer Site, open the "Napa" Office 365 Development Tools and then choose Add New Project.

  • Choose the App for SharePoint template, name the project and then click the Create button 
  • Replace Default.aspx with the following code
  • Replace APP.js with the following source code
  • Publish Your App


Step 2

Change the Permission: Tenant = Write, User Profiles = Read



Step 3

Update Default.aspx and App.js file.

Default ASPX



App.js
  1. 'use strict';  
  2.   
  3. var hostweburl;  
  4. var appweburl;  
  5. var documenturl;  
  6. var followDocEndpoint;  
  7.   
  8. $(document).ready(function()  
  9. {  
  10.     hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));  
  11.     appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"))  
  12.   
  13.     followDocEndpoint = decodeURIComponent(appweburl) + "/_api/social.following";  
  14.   
  15.     $("#btnclick").click(function(event)  
  16.     {  
  17.         documenturl = document.getElementById("stopFollow").value;  
  18.         stopFollow();  
  19.         event.preventDefault();  
  20.     });  
  21.     $.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js");  
  22. });  
  23.   
  24. function stopFollow()   
  25. {  
  26.     $.ajax({  
  27.         url: followDocEndpoint + "/stopfollowing",  
  28.         type: "POST",  
  29.         data: JSON.stringify({  
  30.             "actor": {  
  31.                 "__metadata":   
  32.                 {  
  33.                     "type""SP.Social.SocialActorInfo"  
  34.                 },  
  35.                 "ActorType": 1,  
  36.                 "ContentUri": documenturl,  
  37.                 "Id"null  
  38.             }  
  39.         }),  
  40.         headers:  
  41.         {  
  42.             "accept""application/json;odata=verbose",  
  43.             "content-type""application/json;odata=verbose",  
  44.             "X-RequestDigest": $("#__REQUESTDIGEST").val()  
  45.         },  
  46.         success: function()   
  47.         {  
  48.             alert('The user has stopped following the document.');  
  49.         },  
  50.         error: stopFollowErrorHandler  
  51.     });  
  52. }  
  53.   
  54. function stopFollowErrorHandler(data, errorcode, errormessage)  
  55. {  
  56.     alert("Couldn't stop following the document" + errormessage);  
  57. }  
  58.   
  59. function getQueryStringParameter(paramToRetrieve)  
  60. {  
  61.     var params = document.URL.split("?")[1].split("&");  
  62.     for (var i = 0; i < params.length; i = i + 1) {  
  63.         var singleParam = params[i].split("=");  
  64.         if (singleParam[0] == paramToRetrieve) return singleParam[1];  
  65.     }  
  66. }  
Step 4

Publish the solution and click the Trust It button.



Step 5

Enter the Document URL and click on the Stop Follow button.



Output


 

Up Next
    Ebook Download
    View all
    Learn
    View all