次の方法で共有


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", "*");
 
 }
 
 });
 

Comments

  • Anonymous
    September 24, 2013
    Can you show us how you construct or an example of your "myurl"?

  • Anonymous
    October 08, 2013
    Hi Todd, Sorry for the late reply. Here is an example of how to construct your "myurl":

In your element.xml that defines your ECB's custom action, pass the {AppWebUrl} handle to the target page (in my case, ~appWebUrl/Pages/Default.aspx) via a parameter. <CustomAction Id="e6837758-0af2-413f-8934-b62bb89def3f.Social Sharing"                RegistrationType="ContentType"                RegistrationId="0x0101"                Location="EditControlBlock"                Sequence="101"                              Title="Post to Newsfeed"                HostWebDialog="true"                HostWebDialogHeight="400"                HostWebDialogWidth="250"                >    <!--    Update the Url below to the page you want the custom action to use.    Start the URL with the token ~remoteAppUrl if the page is in the    associated web project, use ~appWebUrl if page is in the app project.    -->    <UrlAction Url="~appWebUrl/Pages/Default.aspx?item={ItemUrl}&site={SiteUrl}&spapp={AppWebUrl}" />  </CustomAction> 2. In your .js file, first get the appweburl from the target page's parameter. Then construct the rest of myurl. var appweburl = getParameterByName("spapp"); var myurl = appweburl + "/_api/social.feed/my/Feed/Post"; Hope this helps.

  • Anonymous
    November 19, 2014
    Hi Rita, Any pointers on how this javascript could be adapted to create a button in Publishing pages to 'share to newsfeed' - rather than an ECB custom action? Looks awesome, btw.