2

I am trying to follow a document in SharePoint library using REST API here is my URL : _spPageContextInfo.webAbsoluteUrl + '/_api/social.following/follow'

But i getting this error in response text

{"error":{"code":"3, Microsoft.Office.Server.Social.SPSocialException","message":{"lang":"en-US","value":"The target of the operation was not found. Internal type name: Microsoft.Office.Server.UserProfiles.FollowedContentException. Internal error code: 12."}}} Error Code 3 : Internal Server error (Not sure exactly what it means)

Any idea on what needs to be corrected in order to follow a document ?

Edited:

@M.Qassas :- I am still getting the same error. Please find the screenshot below. We have not implemented SharePoint app. I am calling this rest endpoint from a js file which is located in Site Assets folder. enter image description here

  • instead of _spPageContextInfo.webAbsoluteUrl try with _spPageContextInfo.siteAbsoluteUrl and check ? – Gautam Sheth Oct 24 '17 at 6:20
  • No luck. I got same same error. I am able to follow sites using the below code but not documents. – Sid Oct 24 '17 at 6:26
  • can you post the code that you are using to follow the documents ? – Gautam Sheth Oct 24 '17 at 6:34
  • I am using below code posted by M.Qassas. I have modified it and defined error function requestFailed, added docx url and inside document.ready added followingManagerEndpoint = decodeURIComponent(_spPageContextInfo.siteAbsoluteUrl) + "/_api/social.following"; isFollowed(); – Sid Oct 24 '17 at 6:49
1

The Internal Server Error means The execution of the service failed in some way.

The common reasons for this error are

  • Failed to get Connection properties.
  • Failed to get Service properties.
  • The maximum number of connections allowed for the service instance cannot be determined.
  • There is an error in the endpoint URL.
  • There is an error in the Ajax call (type, data).

Anyway, to follow a document by using the SharePoint REST service. try to use the following

// Replace the documentUrl placeholder value before you run the code.
var documentUrl = _spPageContextInfo.webAbsoluteUrl + "/Doc URL";
var followingManagerEndpoint;

// Get the SPAppWebUrl parameter from the query string and build
// the Following manager endpoint.
$(document).ready(function () {
    var appweburl;
    var params = document.URL.split("?")[1].split("&");
    for (var i = 0; i < params.length; i = i + 1) {
        var param = params[i].split("=");
        if (param[0] === "SPAppWebUrl") appweburl = param[1];
    }
    followingManagerEndpoint = decodeURIComponent(appweburl) + "/_api/social.following";
    isFollowed();
});

// Check whether the current user is already following the document.

function isFollowed() {
    $.ajax( {
        url: followingManagerEndpoint + "/isfollowed",
        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 (responseData) { 
            stringData = JSON.stringify(responseData);
            jsonObject = JSON.parse(stringData);
            if (jsonObject.d.IsFollowed === true ) {
                alert('The user is currently following the document.');
                stopFollowDocument();
            }
            else {
                alert('The user is currently NOT following the document.');
                followDocument();
            }
        },
        error: requestFailed
    });
}

// Make the current user start following a document.

function followDocument() {
    $.ajax( {
        url: followingManagerEndpoint + "/follow",
        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 (responseData) { 
            stringData = JSON.stringify(responseData);
            jsonObject = JSON.parse(stringData);
            var statusMessage = {
                0 : 'The user has started following the document. ',
                1 : 'The user is already following the document. ',
                2 : 'An internal limit was reached. ',
                3 : 'An internal error occurred. '
            }
            alert(statusMessage[jsonObject.d.Follow] + 'Status code = ' + jsonObject.d.Follow);
        },
        error: requestFailed
    } );
}

Note: if you are using SharePoint APP, you should set the App permission properly as the following:

  • Set Write permissions for the Tenant scope in the Content category.
  • Set Read permissions for the User Profiles scope in the Social category.

For more details check Start following and stop following a document by using the SharePoint REST service

  • I am still facing the same issue. Please find the screenshot i have added in the question. – Sid Oct 24 '17 at 5:57
0

Check the js code below, please make sure the document url is right. And add the code into a script editor web part in a web part page.

    <script src="//code.jquery.com/jquery-1.11.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
    // begin work to call across network
    var requestUri = _spPageContextInfo.siteAbsoluteUrl+"/_api/social.following/follow";
    var documentUrl=_spPageContextInfo.siteAbsoluteUrl+"/Shared%20Documents/doc.docx";
    // execute AJAX request
    $.ajax({
        url: requestUri,
        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 (responseData) { 
            stringData = JSON.stringify(responseData);
            jsonObject = JSON.parse(stringData);
            var statusMessage = {
                0 : 'The user has started following the document. ',
                1 : 'The user is already following the document. ',
                2 : 'An internal limit was reached. ',
                3 : 'An internal error occurred. '
            }
            alert(statusMessage[jsonObject.d.Follow] + 'Status code = ' + jsonObject.d.Follow);
        },
        error: requestFailed
    });
});
function requestFailed(xhr, ajaxOptions, thrownError) {
    alert('Error:\\n' + xhr.status + '\\n' + thrownError + '\\n' + xhr.responseText);
}
</script>

enter image description here

0

Thank you all for your responses. I am not sure why i was getting that error when i try to follow a document using REST api. I scrapped that approach and to my surprise I was able to perform all request (i.e. StartFollowing/StopFollowing/AlreadyFollowed a document) using JSOM. I know this doesn't answers initial question but still other approach works.

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.