Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Current »

Example

//create an object to act as a namespace
var sharePoint = {};
//Function that makes the client side call to the server
sharePoint.getItems = function (listName, userId, onSuccess, onFailure) {
    sharePoint.clientContext = SP.ClientContext.get_current();
    sharePoint.list = sharePoint.clientContext.get_web().get_lists().getByTitle(listName);
    var camlQuery = new SP.CamlQuery();
    var caml =
        '<View>' +
            '<Query>' +
            '<Where>' +
            '<And>' +
            '<Eq>' +
            '<FieldRef Name=\'Author\' LookupId=\'TRUE\' />' +
            '<Value Type=\'Integer\'>' + userId + '</Value>' +
            '</Eq>' +
            '<Eq>' +
            '<FieldRef Name=\'Submitted\' />' +
            '<Value Type=\'Boolean\'>False</Value>' +
            '</Eq>' +
            '</And>' +
            '</Where>' +
            '<OrderBy><FieldRef Name=\'PeriodFrom\' Ascending=\'True\' /></OrderBy>' +
            '</Query>' +
            '<RowLimit>1</RowLimit>' +
            '</View>';
    camlQuery.set_viewXml(caml);
    sharePoint.listItems = sharePoint.list.getItems(camlQuery);
    sharePoint.clientContext.load(sharePoint.listItems);
    //context.load(<objectCollection>,'Include(<Field1>,<Field2>,<Field3>');
    sharePoint.clientContext.executeQueryAsync(Function.createDelegate(this, onSuccess), Function.createDelegate(this, onFailure));
};
//If successful, this method is called
sharePoint.onSuccess = function (sender, args) {
    console.log(sharePoint.listItems.get_count());
    for (var count = 0; count < sharePoint.listItems.get_count(); count++) {
        var item = sharePoint.listItems.get_item(count);
        console.log("Title: " + item.get_item('Title'));
    }
};
//If an error occurs this method is called
sharePoint.onFailure = function (sender, args) {
    if (args instanceof SP.ClientRequestFailedEventArgs) {
        var message = args.get_message();
        var code = args.get_errorCode();
        var details = args.get_errorDetails();
        var value = args.get_errorValue();
        var typeName = args.get_errorTypeName();
        var stack = args.get_stackTrace();
        var correlationId = args.get_errorTraceCorrelationId();
        alert('Error: ' + message + ".");
    } else {
        alert('Unexpected Error');
    }
};
//This method initates the process
function Test() {
    sharePoint.getItems("Timesheets", 2,sharePoint.onSuccess, sharePoint.onFailure);
}
$(document).ready(function () {
    ExecuteOrDelayUntilScriptLoaded(Test, "SP.js");
});
  • No labels