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"

use

method: "POST"

and for the actual data package to post, instead of

data: JSON.stringify(item)

use

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\"}"

16 comments:

  1. Nice tutorial. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated tutorials…
    Best Devops Training in pune
    Data science training in Bangalore

    ReplyDelete
  2. Study ExcelR PMP Certification where you get a great experience and better knowledge.

    PMP Certification


    Location 1:

    ExcelR - Data Science, Data Analytics Course Training in Bangalore 49, 1st Cross, 27th Main BTM Layout stage 1 Behind Tata Motors Bengaluru, Karnataka 560068 Phone: 096321 56744 Hours: Sunday - Saturday 7AM - 11PM.

    Comments

    ReplyDelete
  3. I have actually never perused such overwhelmingly great substance like this. I concur with your focuses and your thoughts. This information is extremely incredible. Much obliged.


    SEO services in kolkata
    Best SEO services in kolkata
    SEO company in kolkata
    Best SEO company in kolkata
    Top SEO company in kolkata
    Top SEO services in kolkata
    SEO services in India
    SEO copmany in India

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete

  5. Wow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also.
    data science course in guwahati

    ReplyDelete
  6. I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well.
    Python Training in Chennai

    Python Training in Training

    Python Training in Bangalore

    Python Hyderabad

    Python Training in Coimbatore


    ReplyDelete
  7. Thank you for posting informative insights, I think we have got some more information to share with you! Do check out Data Analytics Course

    ReplyDelete
  8. Really awesome blog. Useful information and knowledge. Thanks for posting this blog. Keep sharing more blogs again soon.
    AI Patasala Data Science Course

    ReplyDelete
  9. Data Science is a dynamic domain with a promising future, start your Data Science Course today with 360DigiTMG and become a Data Scientist without hassle.
    best data science courses in chennai

    ReplyDelete
  10. Thank you the sharing the most informative blog for us. It may helpfull for you all.

    Selenium Training in Chennai
    Best Selenium Training in Chennai

    ReplyDelete
  11. I thought I was head bound with my ideas on this, but with your excellent writing expertise however you have well managed to convert my beliefs. I have gathered some new thoughts on this topic with your new-fangled ideas. Your amazing piece of writing kept me hooked to it. According to me, you have successfully covered all the major points which this article demanded.
    SAP training in Kolkata
    SAP training in Mumbai
    SAP training institute in Kolkata
    SAP training courses in Mumbai
    Data Science training in Mumbai
    SAP training in Pune
    Data Science training in Pune

    ReplyDelete