Post links to social newsfeed via REST in SharePoint 2013
Recently, I had to write a SharePoint App that allows users to "social share" documents, videos, pictures from the item's content menu (ECB) to their social newsfeed.
Here is a snippet of JavaScript code to post the link of the item to the user's social newfeed via REST.
$.ajax({
url: myurl,
type:
"POST",
data: JSON.stringify({
'restCreationData':{
'__metadata':{
'type':'SP.Social.SocialRestPostCreationData'
},
'ID': null,
'creationData': {
'__metadata': {
'type': 'SP.Social.SocialPostCreationData'
},
'ContentItems': {
'results': [
{
'__metadata': {
'type': 'SP.Social.SocialDataItem'
},
'Text': statusLink,
'Uri':statusLink,
'ItemType':1//0 - document; 1 - link; 3 - tag
}
]
},
'ContentText': 'Check out this: {0}',
'UpdateStatusText': false
}
}
}),
headers: {
"accept":"application/json;odata=verbose",
"content-type":"application/json;odata=verbose",
"X-RequestDigest":$("#__REQUESTDIGEST").val()
},
success:
function () {
//alert("posted!");
window.parent.postMessage(
"CloseCustomActionDialogRefresh", "*");
},
error:
function(xhr, ajaxOptions, thrownError){
alert(
"post error: " + xhr.status + ";" + thrownError);
window.parent.postMessage(
"CloseCustomActionDialogRefresh", "*");
}
});