Monday, November 25, 2013

Create,update and delete List Item using Java Script Client Object Model in SharePoint 2010


1- Create/Add list item 

 var oListItem;

    function createListItem()
{
        var clientContext = new SP.ClientContext('/');
        var oList = clientContext.get_web().get_lists().getByTitle('My Announcements List');

        var itemCreateInfo = new SP.ListItemCreationInformation();
        oListItem = oList.addItem(itemCreateInfo);

        oListItem.set_item('Title', 'My First Item');
        oListItem.update();
        clientContext.load(oListItem);

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

    }

  function onQuerySucceeded()
 {
        alert('Item created: ' + oListItem.get_id());
  }

function onQueryFailed(sender, args)
 {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
  }


2- Update list item

 var oListItem;

function updateListItem()
{
        var clientContext = new SP.ClientContext('/');
        var oList = clientContext.get_web().get_lists().getByTitle('My Announcements List');
    
        this.oListItem = oList.getItemById(3);

        oListItem.set_item('Title', 'Updated Title by mohit');
        oListItem.update();

      clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

  }

 function onQuerySucceeded()
 {
        alert('Item updated!');
  }

function onQueryFailed(sender, args)
 {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
 }

3- Delete list item 
   
 var oListItem;

 function deleteListItem()
 {  
        var clientContext = new SP.ClientContext('/');
        var oList = clientContext.get_web().get_lists().getByTitle('My Announcements List');

        this.oListItem = oList.getItemById(3);
        oListItem.deleteObject();

        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

 }

  function onQuerySucceeded()
 {
        alert('Item deleted: ' + itemId);
 }

function onQueryFailed(sender, args)
 {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
 }

--------------------------

No comments:

Post a Comment

SharePoint Deployment Error : The specified path, file name, or both are too long.

Hi, Sometimes we get surprised with below error while deploying wsp file... Solution : To overcome with this issue, we just ne...