Raghu's SharePoint Column : SharePoint - My Lifeline!!

Raghu's SharePoint Column

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();
myListItemCreationInfo.set_folderUrl("http://servername/sites/PP/Blog/Lists/Docs/Folder1");
  var newLinkItem= myList.addItem(myListItemCreationInfo);

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

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..

 
Comments

Hi,
This code is not working for me
could you please help me on this?

Hi ag,
Which part of the code is not working for you, any specific scenario details or exception details will help me to dig in.

Is it possible to copy or move listitems (not files) between folders using the client object model as well?

Leave a Reply
*

Protected by WP Anti Spam