How to publish a post to SharePoint Social Feed using SharePoint 2013 JSOM

Home | SharePoint 2013 | How to publish a post to SharePoint Social Feed using SharePoint 2013 JSOM

In this article, I’ll explain how to publish a Post to the SharePoint Social Feed using SharePoint 2013 JavaScript Object Model (JSOM).Open Visual Studio 2013, Create a new SharePoint Hosted App and name it as PublishPost.

Go to default.aspx, add a reference to SP.UserProfiles.js in the PlaceHolderAdditionalPageHead

<script type=”text/javascript” src=”/_layouts/15/sp.userprofiles.js”></script>

Add the following Javascript function in the App.JS file (comment out the existing code already in the file)

SP.SOD.executeOrDelayUntilScriptLoaded(WritePost, ‘SP.UserProfiles.js’);

function WritePost() {
    var oclientContext;
    var ofeedManager;
    var oresultThread;

    // Initialize the current client context and the SocialFeedManager instance.
    oclientContext = SP.ClientContext.get_current();
    ofeedManager = new SP.Social.SocialFeedManager(oclientContext);

    // Add a link to be included in the post.
    var olinkDataItem = new SP.Social.SocialDataItem();
    olinkDataItem.set_itemType(SP.Social.SocialDataItemType.link);
    olinkDataItem.set_text(‘My blog url’);
    olinkDataItem.set_uri(‘
http://sundarnarasiman.net’);
    var osocialDataItems = [ olinkDataItem ];

    // Set up the post content
    var opostCreationData = new SP.Social.SocialPostCreationData();
    opostCreationData.set_contentText(‘The text for the post, which contains a {0}.’);
    opostCreationData.set_contentItems(osocialDataItems);

    // Write the post
    oresultThread = ofeedManager.createPost(null, opostCreationData);
    oclientContext.executeQueryAsync(WriteSucceeded, WriteFailed);
}

function WriteSucceeded(sender, args) {
    $get(“ResultMessage”).innerText = ‘Successfully posted the message to Posts’;
}
function WriteFailed(sender, args) {
    $get(“ResultMessage”).innerText = ‘Failure in writing message’ + args.get_message();
}

Open the default.aspx and add the following snippet

<Div id=”ResultMessage” style=”color: blue”></Div>

Open the AppManifest.XML and provide FullControl permission to UserProfiles (Social) and Tenant. Otherwise, you’ll get a permission error.

<App xmlns=”http://schemas.microsoft.com/sharepoint/2012/app/manifest”
     Name=”PublishPost”
     ProductID=”{72faf665-bf93-4a8e-88a7-2d2ad0be0f9d}”
     Version=”1.0.0.0″
     SharePointMinVersion=”15.0.0.0″
>
  <Properties>
    <Title>PublishPost</Title>
    <StartPage>~appWebUrl/Pages/Default.aspx?{StandardTokens}</StartPage>
  </Properties>

  <AppPrincipal>
    <Internal />
  </AppPrincipal>
  <AppPermissionRequests>
    <AppPermissionRequest Scope=”
http://sharepoint/social/tenant” Right=”FullControl” />
    <AppPermissionRequest Scope=”
http://sharepoint/content/tenant” Right=”FullControl” />
  </AppPermissionRequests>
</App>

Now, you’ll see that the post being published to MySite.

social post

 Subscribe to my blog

Leave a Reply