JSAPI Tutorial: Posting an Update to a User's Network Stream

Level: Intermediate

Prerequisite: Profile Tutorial

Summary:This tutorial builds on the Profile Tutorial. In this tutorial we discuss adding "write" functionality to the page by posting updates to the user's network stream.

Note that all code samples in this document use jQuery, so that's the syntax being used.

Setup

Your application needs to be set up as described in the Profile Tutorial. The code can also be downloaded directly.

Making Raw JSAPI Calls

The JSAPI provides convenience methods for many of the most common REST API calls. It does not, however, cover the entire REST API, and does not currently support write operations. However, a Raw method is provided which can be used to access any resource method the REST API provides.

Details about Making Raw API Calls

The Raw method supports a couple of new chained methods beyond the standard ones.

When accessing a resource in this way, you need to look at the REST API documentation for that resource.


REST API "Network Update" Resource

In this case, because we are working with the "Network Update" resource, we can refer to the Post Network Update document. That document covers all of the write operations of this type.

More details

From this document you can see that adding like to a network update is a POST operation, written to

The body for this request, in XML, is listed as:

The JSAPI doesn't use XML, however. It uses JSON for all of the actions. So in order to understand what JSON to send, we can refer to the API Requests with JSON document. To perform any JSAPI writes you'll need to understand how to translate between the two.

In this case, the XML above translates to the following in JSON:


The Raw Network Update Call

Now we know what needs to happen - a POST to the like resource for the update item. The body for our update will be: So the call looks like this:

Putting it All Together

Now we can put this into the original profile application. To do that, we need to add a button for the user to click and a handler to process clicks.

First, add the button. This will be inserted in the loadData() function after the profile has loaded.

And then a handler to listen for the click events

Example Code