Raghu's SharePoint Corner: Create List item in Folders using Client Object Model

My blog has moved!

You will be automatically redirected to the new address. If that does not occur, visit
and update your bookmarks.

October 26, 2011

Create List item in Folders using Client Object Model

There are scenarios where you would like to add list item to a folder/sub folder in a custom SharePoint list and many of you would like to achieve this using ECMA script/ JavaScript Client Object Model. Before we start with the code, i am assuming that you have enables Folder Content Types in a custom list and have also created a Folder in a list before the below snippet is executed.

function CreateListItemInFolder()

// Create client context
 var myContext = new SP.ClientContext.get_current();
 // Get current web (comparable to SPWeb)
 var myWeb = myContext.get_web();
 // Get list by title (comparable to SPList)
   var myList = myWeb.get_lists().getByTitle('Docs');
   // Create new object for list item creation

 //Create List Item Infor and set the Folder Path for the item to be created.      
      var myListItemCreationInfo = new SP.ListItemCreationInformation();
 var newLinkItem= myList.addItem(myListItemCreationInfo);

     // Set properties for new list item
 newLinkItem.set_item('Title', "Title");
 // Update the new item

myContext.executeQueryAsync(Function.createDelegate(this, onSuccessAddQL),Function.createDelegate(this, onFailureAddQL));

function onSuccessAddQL(sender, args)
alert("Item Added Successfully In the Folder");
SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.cancel,'Closing Quick Links Dialog');
function onFailureAddQL(sender, args)
alert("Item Adding Failed to the Folder");

happy coding..

No comments: