Archive for January 29, 2013

SharePoint Apps and Relative DateTime

When working with Apps, JSOM and the new SharePoint REST API there’s always a challenge when it comes to displaying moments in time or dates. New in SharePoint 2013 is the display of Time Delta’s like “A moment ago” or “2 days ago”. The Newsfeed is a good example of this.

ISO8601 standard

SharePoint 2013’s new cool REST API returns DateTime field values in ISO8601 format. This can be a value in the likes of “2013-01-29T13:37:00Z”. Unfortunately there is no helper function (or I haven’t found it yet) in the SharePoint JS Object Model to parse this format into a Time Delta.


This is where a tiny, free JavaScript library called moment.js comes in handy. With these very simple lines of code this library gives you the right output.

var spDate = “2013-01-29T13:37:00Z”;
var deltaToNow = moment(spDate, “YYYY-MM-DDTHH:mm:ss Z”).fromNow();

Displaying deltaToNow will output the relative value from the given date till the moment of execution.


SharePoint 2013: Post a Status Update to a Site Newsfeed (JSOM)

This MSDN Article explains how you’re able to post Newsfeed Items to your Timeline. The Posts however always end up on your Personal Social Feed. How can we post a message to a specific location? For instance a SharePoint Site I follow.

Social Actors

In SharePoint 2013, a SPWeb can also be a Social Actor. This means SharePoint provides some Social Metadata with the Web Object so it’s easy to find and use in a Social Context (even across SiteCollections and/or WebApplications). The Social Actor ID is a unique ID we need.


SocialActor Example:
<d:element m:type=”SP.Social.SocialActor”>
  <d:AccountName m:null=”true” /> 
  <d:ActorType m:type=”Edm.Int32″>2</d:ActorType> 
  <d:CanFollow m:type=”Edm.Boolean”>true</d:CanFollow> 
  <d:EmailAddress m:null=”true” /> 
  <d:FollowedContentUri m:null=”true” /> 
 <d:Id>8.a8c7898c48604f3c9dccdc67dbf0805e.014890b080f549e4899619003332b129. a8c7898c48604f3c9dccdc67dbf0805e.0c37852b34d0418e91c62ac25af4be5b</d:Id> 
  <d:ImageUri m:null=”true” /> 
  <d:IsFollowed m:type=”Edm.Boolean”>true</d:IsFollowed> 
  <d:LibraryUri m:null=”true” /> 
  <d:Name>Go Team</d:Name> 
  <d:PersonalSiteUri m:null=”true” /> 
  <d:Status m:type=”Edm.Int32″>0</d:Status> 
  <d:StatusText m:null=”true” /> 
  <d:TagGuid m:type=”Edm.Guid”>00000000-0000-0000-0000-000000000000</d:TagGuid> 
  <d:Title m:null=”true” /> 
 *The ID is without spaces but since it’s really long I broke it up.


On the Newsfeed page in SharePoint 2013 (Personal Site) we are able to create Updates on the Newsfeed. There is a Dropdown that provides us with the choice Share with Everyone (Post to Social Feed) or with a Site I follow (Post to Site Feed). We can simply use the awesome REST API to get an overview of the Sites I Follow.
(types=4 means Sites, types=2 means Documents).
Now in order to Post to a Site Feed of one the Sites you follow you just have to pass the SocialActor ID as targetId parameter of the SP.Social.SocialFeedManager.createPost() Method.


JSOM Example:
//Check if there is any text
var socialText = “I’d like to share this with you”;
var teamSiteId = “8.a8c7898c48604f3c9dccdc67dbf0805e.014890b080f549e4899619003332b129. a8c7898c48604f3c9dccdc67dbf0805e.0c37852b34d0418e91c62ac25af4be5b“;
//Post the URL to the Personal Newsfeed TimeLine
var clientContext = SP.ClientContext.get_current();
var feedManager = new SP.Social.SocialFeedManager(clientContext);
 // Create the post content.
var postCreationData = new SP.Social.SocialPostCreationData();
// Publish the post.
var resultThread = feedManager.createPost(teamSiteId, postCreationData);
clientContext.executeQueryAsync(function () { //success }, function() { //error });  
Easy as that!


Important Sidenotes

1) Some Sites you follow may not have a newsfeed enabled. In this case you will not be able to Post to that Site. You can use this REST Endpoint to check if the Feature for Site Feed is enable on a SPWeb.
If the Feature is activated this call will return some values. If not the Feature is not activated and it’s not possible to post to this Web.
2) The “Sites I Follow” also can contain Sites on which your App is not installed. That means the App is not trusted and doesn’t have permissions to read from that Site. So the Feature check can only be done on Sites that trust your App.