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
_spPageContextInfo.webAbsoluteUrl
try with_spPageContextInfo.siteAbsoluteUrl
and check ?