2

Is there a way to fetch Newsfeed with Link to Hashtag and all rich content using JSOM?

We are able to fetch Newsfeed but all text is coming in plain text whithout any link to Hashtags.

Is there a way to do this using JSOM?

  • You mean an RSS feed? – Malin De Silva Aug 20 '14 at 7:00
  • 1
    No, I am taking about SharePoint Social Newsfeed. – Amit Aug 20 '14 at 7:01
3

In order to retrieve Post content, SP.Social.SocialPost exposes the following properties:

  • Attachment - Gets an image, video, or document attachment associated with the post
  • Text - Gets the display text of the post
  • Overlays - Gets an array of objects that associates placeholder substrings in the post text with URIs and other data.

The following example demonstrates how print social feed content (posts that contains attachments and external links)

How to retrieve and print the social feed by using the JSOM in SharePoint 2013

function loadFeed(feedType,feedOptions,success,error) {
    var context = SP.ClientContext.get_current();
    var feedManager = new SP.Social.SocialFeedManager(context);
    var feed = feedManager.getFeed(feedType, feedOptions);
    context.load(feedManager);
    context.executeQueryAsync(
       function(){
          success(feed);
       }, 
       error
    );
}

function renderRootBody(feed) {
    var postHtml = '<div class="ms-microfeed-rootBody">';
    postHtml+='<div class="ms-microfeed-text ms-microfeed-rootText">';  
    var threads = feed.get_threads();
    for (var i = 0; i < threads.length ; i++) {
        var thread = threads[i];
        var post = thread.get_rootPost();
        postHtml+= renderPostBody(post);
    }
    postHtml+='</div>';
    postHtml+='</div>';
    return postHtml;
}




function renderPostBody(post) {
   var postHtml = '';      
   //get post Text
   var postText = post.get_text(); 
   //get Post Overlays
   var overlays = post.get_overlays();
   postHtml+='<span class="ms-microfeed-postBody ms-textSmall">';
   for(var i = 0; i < overlays.length;i++) {
     var overlayLinkUrl = overlays[i].get_linkUri();
     var overlayIndex = overlays[i].get_index();
     var overlayLength = overlays[i].get_length();  
     var overlayText = postText.substr(overlayIndex,overlayLength);
     var overlayLinkHtml = String.format('<a id="ms-externalLink" class="" href="{0}" target="_blank">{1}</a>',overlayLinkUrl,overlayText);
     postHtml+=overlayLinkHtml;
   }
   postHtml+='</span>';
   //get post Attachment
   var attachment = post.get_attachment();
   var attachmentUrl = attachment.get_uri();
   var attachmentName = attachment.get_name();
   postHtml+= String.format('<div id="" class="ms-microfeed-attachmentDiv"><img class="ms-microfeed-attachmentImage" src="{0}" alt="{1}"/></div>',attachmentUrl,attachmentName);
   return postHtml;
}

Example

Assume the page that contains the following placeholder:

<div class='ms-microfeed-thread'/>

Then the following script demonstrates how to retrieve and print the news feed:

SP.SOD.executeFunc('userprofile', 'SP.Social.SocialFeedManager', function() {
    var feedOptions = new SP.Social.SocialFeedOptions();
    loadFeed(SP.Social.SocialFeedType.news,feedOptions, 
      function(feed)
      {
          var feedContent = renderRootBody(feed);
          $('div.ms-microfeed-thread').append(feedContent);
      },
      function(sender, args) {
          console.log(args.get_message());
      }
   );
});

Result

enter image description here

References

0

Yes please find the Social feed REST API here.

Then consume the REST web service using jQuery as it tells here.

  • 2
    This answer needs more details to be really helpful. Do you think you can outline the steps described in your links? – Robert Lindgren Aug 22 '14 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.