Wednesday, March 9, 2016

SharePoint ListItem CRUD operation using Java Script

SharePoint ListItem CRUD operation using Java Script

//class
<style type="text/css">
     .myBlocks{
         display:inline-block;
         border1px solid blue;
         width:245px;
     }
     .myControls{
         displayinline-grid;
         border-bottom-color:red;        
         width:229px;
     }
</style>


<script type="text/javascript"  src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script type="text/javascript"  src="/_layouts/15/sp.runtime.js"> </script>
<script type="text/javascript"  src="/_layouts/15/sp.js"> </script>


//Create List Item:

function createListItem() {

    var clientContext = new SP.ClientContext.get_current();
      // Note: If your not in Current Context then use the Constructor-->ClientContext("siteUrl");
            //clientContext.Credentials =new NetworkCredential(SPUserName, SPPassWordSPDomainName);

    var oList = clientContext.get_web().get_lists().getByTitle('TestLst');
       
    var itemCreateInfo = new SP.ListItemCreationInformation();
    this.oListItem = oList.addItem(itemCreateInfo);
       
    oListItem.set_item('Title', 'My New Item!');
    oListItem.set_item('Name', 'Hello World!');
       
    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());
}

//Update List Item
function updateListItem() {
 
    var clientContext = new new SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('TestLst');
 
    this.oListItem = oList.getItemById(3);
 
    oListItem.set_item('Title', 'My Updated Title');
 
    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());
}

//Update List Item
function updateListItem() {
 
    var clientContext = new SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('TestLst');
 
    this.oListItem = oList.getItemById(3);
 
    oListItem.set_item('Title', 'My Updated Title');
 
    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());
}


//Delete List Item
function deleteListItem() {
 
    this.itemId = 2;
 
    var clientContext = new SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('TestLst');
 
    this.oListItem = oList.getItemById(itemId);
 
    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