Can anyone share the code and help me on how to follow and test if you are already following a site?
3 Answers
// Replace the siteUrl placeholder value before you run the code.
var siteUrl = "https://domain.sharepoint.com";
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 site.
// The request body includes a SocialActorInfo object that represents
// the specified item.
// The success function reads the response from the REST service and then
// toggles the user's following status by calling the appropriate method.
function isFollowed() {
$.ajax( {
url: followingManagerEndpoint + "/isfollowed",
type: "POST",
data: JSON.stringify( {
"actor": {
"__metadata": {
"type":"SP.Social.SocialActorInfo"
},
"ActorType":2,
"ContentUri":siteUrl,
"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 site.');
stopFollowSite();
}
else {
alert('The user is currently NOT following the site.');
followSite();
}
},
error: requestFailed
});
}
// Make the current user start following a site.
// The request body includes a SocialActorInfo object that represents
// the site to follow.
// The success function reads the response from the REST service.
function followSite() {
$.ajax( {
url: followingManagerEndpoint + "/follow",
type: "POST",
data: JSON.stringify( {
"actor": {
"__metadata": {
"type":"SP.Social.SocialActorInfo"
},
"ActorType":2,
"ContentUri":siteUrl,
"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 site. ',
1 : 'The user is already following the site. ',
2 : 'An internal limit was reached. ',
3 : 'An internal error occurred. '
}
alert(statusMessage[jsonObject.d.Follow] + 'Status code = ' + jsonObject.d.Follow);
},
error: requestFailed
} );
}
// Make the current user stop following a site.
// The request body includes a SocialActorInfo object that represents
// the site to stop following.
function stopFollowSite() {
$.ajax( {
url: followingManagerEndpoint + "/stopfollowing",
type: "POST",
data: JSON.stringify( {
"actor": {
"__metadata": {
"type":"SP.Social.SocialActorInfo"
},
"ActorType":2,
"ContentUri":siteUrl,
"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 site.');
},
error: requestFailed
} );
}
function requestFailed(xhr, ajaxOptions, thrownError) {
alert('Error:\n' + xhr.status + '\n' + thrownError + '\n' + xhr.responseText);
}
-
Thaks it's almost complete.. but i'm having trouble with this error 'Uncaught TypeError: Cannot read property 'IsFollowed' of undefined' Commented Sep 9, 2015 at 15:19
-
-
i tried to view the jsonObject. but it showing like this :<!DOCTYPE html> <html lang="en" xmlns="w3.org/1999/xhtml" data-ng-app="alphaKanban"> <head> <title>app</title> <script type="text/javascript" src="../Scripts/jquery-1.9.1.min.js"></script> <script type="text/javascript" src="../Scripts/bootstrap.min.js"></script> <script type="text/javascript" src="../Scripts/angular.min.js"></sc </html> what do you think the problem? Commented Sep 9, 2015 at 15:42
-
but the post process was successfully executed XHR finished loading: POST "uling-e71b8136182c04.sharepoint.com/sharepointapp". Commented Sep 9, 2015 at 15:43
-
@Looytawon The above answer satisfies the need your question, though I'll really suggest you to ask a different question on how to call rest api in an app. It will help you better :) Commented Sep 14, 2015 at 4:52
You can use the endpoint http://<siteCollection>/<site>/_api/social.following/isfollowed
You should take a look at this very detailed MSDN article: Following people and content REST API reference for SharePoint 2013.
we have ready made examples on MSDN to follow , sites, documents, tags and people REST Social API.
-
i already tried the isfollowed REst api code .. the post was a success but when i tried to view the json form of the response i was confused when it returns hmtl tags?? wwooaahh?? what the problem? Commented Sep 9, 2015 at 15:51
-
are you using a SharePoint app or you executing the code in sharepoint site itself? Commented Sep 9, 2015 at 15:54
-
Im building a sharepiont app specifically a sharepoint hosted app ! Commented Sep 9, 2015 at 15:56
-
Ok, you said you are getting the HTML response. That means something wrong with your endpoint url formation. you might be getting "pagenotfound.aspx page markup in response. Do one thing, open the page in Chrome, copy the response you are getting in REST call. Open browser dev tools(F12). goto elements section, right click on html tag->edit as html. replace the whole markup with your response text in the editable box, click somewhere else on the browser document area. You will see the page that you are getting in the response. reply here what the page has. Commented Sep 9, 2015 at 16:10
-
i exactly recieving response like this i tried to view the jsonObject. but it showing like this :<!DOCTYPE html><html lang="en"xmlns="w3.org/1999/xhtml";data-ng-app="alphaKanban"><head><title>app</title><script type="text/javascript" src="../Scripts/jquery-1.9.1.min.js"></script><script type="text/javascript" src="../Scripts/bootstrap.min.js"></script> <script type="text/javascript" src="../Scripts/angular.min.js"></sc </html> in the jsonObject but i think the post request was successful coz it shows 'XHR finished loading: POST "uling-e71b8136182c05.sharepoint.com/sharepointkanbo. Commented Sep 9, 2015 at 16:48