Click here to Skip to main content
15,894,646 members
Articles / Productivity Apps and Services / Sharepoint

SharePoint 2010 Client Object Model, Part 2

Rate me:
Please Sign up or sign in to vote.
4.55/5 (11 votes)
13 Oct 2011CPOL9 min read 95.4K   2.7K   27  
An investigation of SharePoint 2010 Client Object Model and how to use it
var LIST_NAME = 'SPUG Demo Contacts';

$(document).ready(function ()
{
});

/// Add Contact button event handler
function onAdd()
{
    var ctx = SP.ClientContext.get_current();
    var web = ctx.get_web();
    var list = web.get_lists().getByTitle(LIST_NAME);

    // can be null when adding item to root folder
    var createInfo = null;
    //var createInfo = new SP.ListItemCreationInformation();
    var item = list.addItem(createInfo);

    $(".field").each(function ()
    {
        var id = $(this).attr("ID");
        var value = $(this).val();

        item.set_item(id, value);
    });

    item.update();

    ctx.executeQueryAsync(
            Function.createDelegate(this, onAddContactSuccess),
            Function.createDelegate(this, onFail));
}

/// Cancel button event handler
function onCancel()
{
    SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.cancel, 'Cancel');
}

/// Function to be called after executeQueryAsync
/// in onAdd has been successful 
function onAddContactSuccess(sender, args)
{
    SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK, 'OK');
}

/// Function to notify of any failure for executeQueryAsync
function onFail(sender, args)
{
    alert(args.get_message());
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions