0

I'm trying to add a button on a document list which should cause a user stop following the document.

a REST Call from the URL

...teams/corporate_communications/_api/social.following/stopfollowing(ActorType=1,ContentUri=@v,Id=null)?@v=%27https://sp.egger.com/teams/corporate_communications/eggernet/Shared%20Documents/Gruppennews/2017_07_announcement_MAZ/MAZ_Zwischenablage/Employee_magazine_DE_2017_06.pdf%27

gave me this error XML

<m:error xmlns:m="..">
    <m:code>
     -1, Microsoft.SharePoint.Client.ClientServiceException
    </m:code>
    <m:message xml:lang="en-US">
      The HTTP method 'GET' cannot be used to access the resource 
     'StopFollowing'. The operation type of the resource is specified as 
     'Default'. Please use correct HTTP method to invoke the resource.
    </m:message>
</m:error>

Debugger Info

And when I run my script

I get following "403 FORBIDDEN" in Debugger

And the Error Message is

Error:\n403\nFORBIDDEN\n{"error":{"code":"-2130575251, 
Microsoft.SharePoint.SPException","message":{"lang":"en-US","value":"The 
security validation for this page is invalid and might be corrupted. Please 
use your web browser's Back button to try your operation again."}}}

I tried to execute as admin in and as User but both results are same so I'm wondering is there a way to implement REST on SP site because it is the recomended way from the dev.office. Or would be the Java object model more apropriate

0

To implement stop following a document using REST you will have to issue a POST request. A sample code is shared below :

use strict';  

var hostweburl;  
var appweburl;  
var documenturl;  
var followDocEndpoint;  

$(document).ready(function()  
{  
hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));  
appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"))  

followDocEndpoint = decodeURIComponent(appweburl) + "/_api/social.following";  

$("#btnclick").click(function(event)  
{  
    documenturl = document.getElementById("stopFollow").value;  
    stopFollow();  
    event.preventDefault();  
});  
$.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js");  
});  

function stopFollow()   
{  
$.ajax({  
    url: followDocEndpoint + "/stopfollowing",  
    type: "POST",  
    data: JSON.stringify({  
        "actor": {  
            "__metadata":   
            {  
                "type": "SP.Social.SocialActorInfo"  
            },  
            "ActorType": 1,  
            "ContentUri": documenturl,  
            "Id": null  
        }  
    }),  
    headers:  
    {  
        "accept": "application/json;odata=verbose",  
        "content-type": "application/json;odata=verbose",  
        "X-RequestDigest": $("#__REQUESTDIGEST").val()  
    },  
    success: function()   
    {  
        alert('The user has stopped following the document.');  
    },  
    error: stopFollowErrorHandler  
});  
}  

function stopFollowErrorHandler(data, errorcode, errormessage)  
{  
 alert("Couldn't stop following the document" + errormessage);  
}  

function getQueryStringParameter(paramToRetrieve)  
{  
var params = document.URL.split("?")[1].split("&");  
for (var i = 0; i < params.length; i = i + 1) {  
    var singleParam = params[i].split("=");  
    if (singleParam[0] == paramToRetrieve) return singleParam[1];  
}  
}  

Output : enter image description here

Stop Following Document using REST

  • My first atempts weren't succesful. Im trying to understand the getQueryStringParameter Function, because I get an Error message on the second split() Method regarding to my URL I do not have the char "&" in it what is problably causing my error. My URL looks like this: sp.egger.com/teams/corporate_communications/eggernet/… – Andreas Vitzthum Aug 3 '17 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.