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