JSAPI Tutorial: Sending a Message to Yourself

Level: Intermediate

Prerequisite: Profile Tutorial

Example: Message Sending Example

Summary:This tutorial builds on the Profile Tutorial. In this tutorial we discuss adding "write" functionality to the page in order to send mailbox messages to the logged in user.

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 "Send Message" Resource

In this case, because we are working with the "Send Message" resource, we can refer to the documentation on Messaging Between Connections.

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, we'll create a similar message, but format it in JSON. This example will send a message to the logged in user because it uses the path of /people/~ for the recipient.


The Message Posting 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 a form to the original HTML.

Then create the subroutine SendMessage()

Example Code