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_text(‘My blog url’);
    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}.’);

    // 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=””

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

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

social post

 Subscribe to my blog

Leave a Reply