Welcome to my blog, stay tunned :
Home | Blogs | Stephane Eyskens's blog

Creating a micro blog with mentions with the REST API

Hi,

Update 04/2013 download my demo App at http://sptoolbasket2013.codeplex.com/

As I've struggled a lot to get this working and since I coudln't find the information anywhere, I've decided to create this blog post that shows how you can create a microblog entry in SharePoint 2013 for the connected user (from a SharePoint-Hosted App) using mentions.

On MSDN, you can find a sample showing how to create a microblog here : http://msdn.microsoft.com/en-us/library/jj822974.aspx. While this is already a good start, there is no mention about posting more complex microblogs and for instance, embedding mentions to other people. If you try just to add @user in the text body, you won't end up with a valid solution :).

So, after a bit of digging, I eventually found out how this works. In the below example, I'm targetting users demo and eni by saying hello to both of them. This is the result when posted:

and here is the JSON data you have to send via a POST query to /_api/social.feed/my/Feed/Post to do that:

{
    'restCreationData': {
        '__metadata': {
            'type': 'SP.Social.SocialRestPostCreationData'
        },
        'ID': null,
        'creationData': {

            '__metadata': {
                'type': 'SP.Social.SocialPostCreationData'
            },
            'Attachment': null,
            'ContentItems': {
                'results': [
                    {
                        '__metadata': {
                            'type': 'SP.Social.SocialDataItem'
                        },
                        'AccountName': 'dc07\\demo',
                        'ItemType': 0,                                        
                        'Uri': null
                    },
                    {
                        '__metadata': {
                            'type': 'SP.Social.SocialDataItem'
                        },
                        'AccountName': 'dc07\\eni',
                        'ItemType': 0,
                        'Uri': null
                    }
                ]
            },
            'ContentText': 'Hello @{0} and hello @{1}',
            'UpdateStatusText': false
        }
    }
}

The magic happens with the ContentItems collection where you have to provide the information about each targetted account which you reference after in the ContentText property by their respective index.

Happy Coding!