Things hard and not so hard.... RSS 2.0
# Monday, February 11, 2013

After wrestling with this tonight for sometime I’ve finally cracked it. SP2013 RTMed and alot of the sample code fails due to the fact that you need to now add ‘..;odata=verbose’ onto pretty much every call to SharePoint.

Basically you get a series of errors such as:

MIME type could not be found that matches the content type of the response. None of the supported type(s) 'application/atom+xml;type=entry, application/atom+xml, application/json;odata=verbose

 

Previously alot of the sample code has

$.getJSON(….) as part of the call to the server – as mentioned we now need to add some custom header values of ‘odata=verbose’, so to save you hours of slogging on this, the getJSON call doesn’t allow custom header values. You need to use the $.ajax(…) for these calls.

READING FROM A LIST

function getCustomers() {
 

  // begin work to call across network
  var requestUri = _spPageContextInfo.webAbsoluteUrl +
                "/_api/Web/Lists/getByTitle('CustomersREST')/items/" +
                "?$select=Id,FirstName,Title,WorkPhone" +
                "&$orderby=Title,FirstName";
   
  var requestHeaders = {
      "accept": "application/json;odata=verbose"
  }
    // execute AJAX request
  $.ajax({
      url: requestUri,
      type: 'GET',
      dataType: 'json',
      headers: requestHeaders,
      success: onDataReturned,
      error: onError
  });
}

 

UPDATING A LIST ITEM

//Sample code to update a Customer List Item in a Customer List called ‘CustomersREST’

function updateCustomer(dialogResult, returnValue) {

  if (dialogResult == SP.UI.DialogResult.OK) {
    var Id = returnValue.Id;
    var FirstName = returnValue.FirstName;
    var LastName = returnValue.LastName;
    var WorkPhone = returnValue.WorkPhone;
    var etag = returnValue.etag;

    var requestUri = _spPageContextInfo.webAbsoluteUrl +
              "/_api/Web/Lists/getByTitle('CustomersREST')/items(" + Id + ")";

    var customerData = {
      __metadata: { "type": "SP.Data.CustomersRESTListItem" },
      Title: LastName,
      FirstName: FirstName,
      WorkPhone: WorkPhone
    };

    requestBody = JSON.stringify(customerData);

    var requestHeaders = {
        "accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
        "X-HTTP-Method": "MERGE",
        "content-length": requestBody.length,
        "content-type" : "application/json;odata=verbose",
        "If-Match": etag
    }

    $.ajax({
      url: requestUri,
      type: "POST",
      contentType: "application/json;odata=verbose",
      headers: requestHeaders,
      data: requestBody,
      success: onSuccess,
      error: onError
    });

  }

}

Monday, February 11, 2013 10:47:06 PM (AUS Eastern Daylight Time, UTC+11:00)  #    Comments [0] -
.NET Developer | SharePoint | 2013 | Tips
Archive
<February 2013>
SunMonTueWedThuFriSat
272829303112
3456789
10111213141516
17181920212223
242526272812
3456789
Blogroll
 AppFabric CAT
AppFabric Windows Server Customer Advisory Team - New Blog.
[Feed] BizTalk 2006 - Windows SharePoint Services adapter
BizTalk 2006 Sharepoint adapter!!
 Breeze SharePoint 2010 Bootcamp
Breeze SharePoint 2010 Bootcamp
[Feed] BTS 2006 R2/EDI
[Feed] Chris Vidotto (MS BTS Legend)
Needs no intro....
 Mark Daunt
BTS/SPS/.NET GURU!!!
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2014
Breeze
Sign In
Statistics
Total Posts: 606
This Year: 10
This Month: 3
This Week: 0
Comments: 270
All Content © 2014, Breeze