Validating Document Using REST

Introduction

This is developed using the NAPA development tool. You can just pass the Document URL in the Text box. When the Validate button is clicked 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 Permissions.

Tenant = Write

User Profiles = Read



Step 3

Update Default.aspx and App.js files.

Default ASPX



App.js

  1. 'use strict';  
  2. var hostweburl;  
  3. var appweburl;  
  4. var followedDocumentURI;  
  5. var documentUrl;  
  6.   
  7. // Get the SPAppWebUrl parameter from the query string and build  
  8. // the Following manager endpoint.  
  9. $(document).ready(function() {   
  10.     hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));  
  11.     appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));  
  12.     followedDocumentURI = decodeURIComponent(appweburl) + "/_api/social.following";  
  13.   
  14.     $("#btnclick").click(function(event) {  
  15.         documentUrl = document.getElementById("isDocFollowed").value;  
  16.         isFollowed();  
  17.         event.preventDefault();  
  18.     });   
  19.     $.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js");   
  20. });   
  21. function isFollowed() {  
  22.     $.ajax({  
  23.         url: followedDocumentURI + "/isfollowed",  
  24.         type: "POST",  
  25.         data: JSON.stringify({  
  26.             "actor": {  
  27.                 "__metadata": {  
  28.                     "type""SP.Social.SocialActorInfo"  
  29.                 },  
  30.                 "ActorType": 1,  
  31.                 "ContentUri": documentUrl,  
  32.                 "Id"null  
  33.             }  
  34.         }),  
  35.         headers: {  
  36.             "accept""application/json;odata=verbose",  
  37.             "content-type""application/json;odata=verbose",  
  38.             "X-RequestDigest": $("#__REQUESTDIGEST").val()  
  39.         },  
  40.         success: isFollowedSuccessHandler,  
  41.         error: isFollowedErrorHandler  
  42.     });  
  43. }   
  44. function isFollowedSuccessHandler(data) {  
  45.     var stringData = JSON.stringify(data);  
  46.     var jsonObject = JSON.parse(stringData);  
  47.     if (jsonObject.d.IsFollowed === true) {  
  48.         alert('The user is currently following the document.');  
  49.     } else {  
  50.         alert('The user is currently NOT following the document.');  
  51.     }  
  52. }   
  53. function isFollowedErrorHandler(data, errorCode, errorMessage) {  
  54.     alert(errorMessage);  
  55. }   
  56. function getQueryStringParameter(paramToRetrieve) {  
  57.     var params = document.URL.split("?")[1].split("&");  
  58.     for (var i = 0; i < params.length; i = i + 1) {  
  59.         var singleParam = params[i].split("=");  
  60.         if (singleParam[0] == paramToRetrieve) return singleParam[1];  
  61.     }  

Step 4

Publish the solution and click the Trust It Button.



Step 5

Enter the Document Library URL and click on the Validate Button.



Output