Wednesday, July 17, 2013

Fun with SharePoint REST and Javascript

Quick Update -- We've released our IntelliGantt for SharePoint 2013 pure HTML5+Javascript gantt chart. Install it and let us know what you think!

Exciting news as we are prepping the new web-based successor to IntelliGantt specifically for SharePoint 2013. We just passed a hurdle that other SharePoint 2013 developers using rest with javascript and the SP.RequestExecutor for cross domain calls should now. It's quite a mouthful, but if you are doing a SharePoint 2013 Provider Hosted App, then you'll know exactly what I'm talking about.

The crux of the issue seems to be that while you use one parameter set for straight ajax calls, when you use the SP.RequestExecutor, that object expects slightly different parameters. To wit:
instead of

type: "POST"


method: "POST"

and for the actual data package to post, instead of

data: JSON.stringify(item)


body: JSON.stringify(item)

For completeness, here is the block that I used to update via rest using javascript across a domain with the SP.RequestExecutor object:

  new SP.RequestExecutor(closureThis.appweburl).executeAsync(
     url: closureThis.appweburl + "/_api/SP.AppContextSite(@target)/web/lists(guid\'" + listIdGuid + "\')/items(" + itemId + ")?@target='" + closureThis.hostweburl + "'",
     method: "POST",
     headers: {
       "accept": "application/json;odata=verbose",
       "content-type": "application/json;odata=verbose",
       "X-RequestDigest": closureThis.getDigestValue(),
       "X-HTTP-Method": "MERGE",
       "If-Match": "*"
     body: updateBlock,
     success: function (data) { closureThis.updateDataObjectCallback(data); },
     error: spRequestorErrorHandler

Where closureThis is an object I use to store SharePoint variables and handle callbacks
Where updateBlock is the JSON.stringify(data) value

My updateBlock value looks like this:

"{\"__metadata\":{\"type\":\"SP.Data.TasksListItem\"},\"Title\":\"First Task\"}"

1 comment:

Smith said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Javascript Online Training from India . or learn thru JavaScript Online Training from India. Nowadays JavaScript has tons of job opportunities on various vertical industry. ES6 Online Training