Retrieve followed sites in SharePoint 2013 using REST API
Today i have faced one scenario that how to get all sites that i am following
Use the below code to get the sites that current user following
Steps to achieve this
1. Create a text file and paste the below code then upload that text file into any document library in site
2. Create a page and insert content editor WebPart
3. Link the text file in the content editor WebPart and save the page
Code
<html>
<head>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
var followingManagerEndpoint;
var followedCount;
var followingEndpoint;
var URL;
var website;
var clientContext;
SP.SOD.executeFunc('sp.js', 'SP.ClientContext',
loadWebsite);
function loadWebsite() {
clientContext =
SP.ClientContext.get_current();
website =
clientContext.get_web();
clientContext.load(website);
clientContext.executeQueryAsync(onRequestSucceeded, onRequestFailed);
}
function onRequestSucceeded() {
URL = website.get_url();
followingManagerEndpoint =
decodeURIComponent(URL) + "/_api/social.following";
getMyFollowedContent();
}
function onRequestFailed(sender, args) {
alert('Error: ' +
args.get_message());
}
//
Get the content that the current user is following.
//
The "types=14" parameter specifies all content types
//
(documents = 2 + sites = 4 + tags = 8).
function getMyFollowedContent() {
$.ajax({
url:
followingManagerEndpoint + "/my/followed(types=14)",
headers: {
"accept": "application/json;odata=verbose"
},
success:
followedContentRetrieved,
error: requestFailed
});
}
//
Parse the JSON data and iterate through the collection.
function followedContentRetrieved(data) {
var stringData = JSON.stringify(data);
var jsonObject = JSON.parse(stringData);
var types = {
1: "document",
2: "site",
3: "tag"
};
var followedActors = jsonObject.d.Followed.results;
var followedList = "You're
following items:";
for (var i = 0; i < followedActors.length; i++) {
var actor = followedActors[i];
followedList += "<p>The " +
types[actor.ActorType] + ": \"" + actor.Name + "\"</p>" + "<p>Site URL
" + ":
\"" +
actor.Uri + "\"</p>";;
}
$("#MyFollow").html(followedList);
}
function requestFailed(xhr, ajaxOptions, thrownError) {
alert('Error:\n' + xhr.status + '\n' + thrownError + '\n' + xhr.responseText);
}
</script>
</head>
<body>
<div id="MyFollow"></div>
</body>
</html>
Comments
Post a Comment