0

Is it possible to return JSON when using the following code. Trying to integrate DynaTable into app to handle all the sorting and filtering and I think JSON might get it to work.

// This function shows Country Limits table
function readCountries() {

$('#countries').show();
// Let dynatable do its magic.
$('#tblCountries').dynatable();
var rptCountries = document.getElementById("countryListRow");
rptCountryList = web.get_lists().getByTitle('CountriesLimits');
var camlQuery = SP.CamlQuery.createAllItemsQuery();
var rptCountryListItems = rptCountryList.getItems(camlQuery);
context.load(rptCountryListItems);
context.executeQueryAsync(

    function () {
        // Success returned from executeQueryAsync

        if (rptCountryListItems.get_count() > 0) {
            var rptCountryListItemEnumerator1 = rptCountryListItems.getEnumerator();

            // Lets get the select items
            while (rptCountryListItemEnumerator1.moveNext()) {
                var rptCountryListItem1 = rptCountryListItemEnumerator1.get_current();
                rptCountries.innerHTML += "<tr><td>" + rptCountryListItem1.get_id() + "</td>" +
            "<td>" + rptCountryListItem1.get_item("Title") + "</td>" +
            "<td>" + rptCountryListItem1.get_item("GiftLimit") + "</td>" +
            "<td>" + rptCountryListItem1.get_item("HospitalityLimit") + "</td></tr>";

            }
        }
        else { alert("There are no Countries setup. Add some now"); }
    },
    function (sender, args) {
        // Failure returned from executeQueryAsync
        alert("Error in Populating Business Units: " + args.get_message());
    }
    );


}
5

If you want JSON you should use the REST API instead of the Client Object Model.

var call = jQuery.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getByTitle('CountriesLimits')",
    type: "GET",
    dataType: "json",
    headers: {
        Accept: "application/json;odata=verbose"
    }
});
call.done(function (data, textStatus, jqXHR) {
    // List data contained in data.d.results
    // This is an array of JSON objects
});
call.fail(function (jqXHR, textStatus, errorThrown) {
    alert("Call failed. Error: " + errorThrown);
});
  • Can i use this in a JSOM app? – Stephen Dec 17 '13 at 12:46
  • I'm not sure what you mean by a JSOM app. How did you create your Visual Studio project? The selections you made there will indicate what kind of App (or Solution) you are building. – Rob Windsor Dec 17 '13 at 13:30
  • Sorry I realise reading back that made no sense...What i meant is I am building an APP for office 365 (or App for Sharepoint 2013 as stated in VS2012), i thought the only option available to me was JSOM... – Stephen Dec 17 '13 at 14:37
  • There are three types of Apps: SharePoint-hosted, provider-hosted, and auto-hosted. You choose the type of App when you create the project. I'm going to assume you chose SharePoint-hosted. If that is the case then the code above will work if the CountriesLimits list is contained in SharePoint site used by your App (also known as the App Web). If the list is contained in the site where you installed the App (also known as the Host Web), then some changes will need to be made. – Rob Windsor Dec 17 '13 at 14:41
  • Yes I opted for Sharepoint hosted. The countries limits list get deployed with the app and lives in the site where the app gets deployed/installed. Thanks for all your help. – Stephen Dec 17 '13 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.