Requirement
Implement a custom social newsfeed, user can retrieve/new/reply/like/unlike a post, and ability to auto retrieve hastags when input "#". you would be notice that the UI is same as the default Newsfeed in SharePoint 2013. Correct! I re-built the feature follow up the default newsfeed.
Introduce:
1. Retrieve all post posted myself, the data is from the default newsfeed
and the below is the default newsfeed:
2. New a post and retrieve the hastag
3. Reply a post
4. Like and Unlike the post
3. Implement in detail
Preparation: Make sure built my site, you can check the url: http://siteurl/my/default.aspx.
Post/Reply a post by Social feed REST API in SharePoint 2013 in detali
Retrieve all newsfeed posted by myself
var appweburl = <a target=_blank href="http://weburl">http://weburl</a>;
feedManagerEndpoint = decodeURIComponent(appweburl) + "/_api/social.feed";
function getMyFeed() {
$.ajax({
url: feedManagerEndpoint + "/my/feed",
headers: {
"accept": "application/json;odata=verbose"
},
success: feedRetrieved,
error: function (xhr, ajaxOptions, thrownError) {
alert("GET error:\n" + xhr.status + "\n" + thrownError);
}
});
}
New a post
// Publish a post to the current user's feed
function postToMyFeed(posttext) {
$.ajax({
url: feedManagerEndpoint + "/my/Feed/Post",
type: "POST",
data: JSON.stringify({
'restCreationData': {
'__metadata': {
'type': 'SP.Social.SocialRestPostCreationData'
},
'ID': null,
'creationData': {
'__metadata': {
'type': 'SP.Social.SocialPostCreationData'
},
'ContentText': posttext,
'UpdateStatusText': false
}
}
}),
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) {
//
},
error: function (xhr, ajaxOptions, thrownError) {
alert("POST error:\n" + xhr.status + "\n" + thrownError);
}
});
}
Reply a post
//reply post
function Reply($object, postid, contenttext) {
$.ajax({
url: feedManagerEndpoint + "/post/reply",
type: "POST",
data: JSON.stringify({
'restCreationData': {
'__metadata': {
'type': 'SP.Social.SocialRestPostCreationData'
},
'ID': postid,
'creationData': {
'__metadata': {
'type': 'SP.Social.SocialPostCreationData'
},
'ContentText': contenttext,
'UpdateStatusText': false
}
}
}),
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) {
//
},
error: function (xhr, ajaxOptions, thrownError) {
alert("POST error:\n" + xhr.status + "\n" + thrownError);
}
});
}
Like/Unlike apost
// like a post via id
function LikePostByID($object, postid) {
$.ajax({
url: feedManagerEndpoint + "/post/like",
type: "POST",
data: JSON.stringify({
'ID': postid
}),
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) {
console.log("success to like");
},
error: function (xhr, ajaxOptions, thrownError) {
alert("POST error:\n" + xhr.status + "\n" + thrownError);
}
});
}
// unlike a post via id
function UnLikePostByID($object, postid) {
$.ajax({
url: feedManagerEndpoint + "/post/unlike",
type: "POST",
data: JSON.stringify({
'ID': postid
}),
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) {
console.log("success to unlike");
},
error: function (xhr, ajaxOptions, thrownError) {
alert("POST error:\n" + xhr.status + "\n" + thrownError);
}
});
}
Retirve hastags auto in UI
please refer to Autocomplete(Enables users to quickly find and select from a pre-populated list of values as they type, leveraging searching and filtering.)
and Sharepoint 2013 Retrieve Taxonomy Term Store via Javascript
More:
Social feed REST API reference for SharePoint 2013 in detail, and if you want the project code, please email to me.