Add list item to the list Using ECMA Script in sharePoint 2013

Hi,

i have to add listitem to the list using JavaScript. But i am getting error in context Url.

var context = SP.ClientContext.get_current(); //gets the current context
    alert(context);-->alert showing objectobject not the site url
    var web = context.get_web(); //gets the web object
    alert(web);alert showing objectobject not the Weburl
    var list = web.get_lists(); //gets the collection of lists
    alert(list);
    function createItem() {
        // Retrieve the list and add an item to it.
        var targetList = list.getByTitle("VolunteerDocument");
        alert(targetList);
        //create a new item object
        var listItemCreation = new SP.ListItemCreationInformation();
        //add the item to the list using addItem method
        var newItem = targetList.addItem(listItemCreation);
        var listItemTitle = document.getElementById("txtCaption").value;
        alert(listItemTitle);
        var listItemCustom = document.getElementById("txtTitle").value;
        alert(listItemCustom);
        //using set_item method u can set the values to the item fields
        newItem.set_item('Description', listItemTitle);
        newItem.set_item('Title', listItemCustom);
        newItem.update();
        context.load(newItem);
        context.executeQueryAsync(ItemCreationSuccess, ItemCreationFail);
    }

Can any one suggest me how to do this one..

Thanks...

August 20th, 2015 3:37am

Hi

It seems your first statement is not correct. It should be- 

var context = new SP.ClientContext.get_current(); //gets the current context
Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 4:08am

hi,

I tried below like even getting same error.

 var context = new SP.ClientContext.get_current(); //gets the current context
        alert(context);
        var web = context.get_web(); //gets the web object
        var list = web.get_lists();
        var targetList = list.getByTitle("VolunteerDocument");

please attached screen shot.

August 20th, 2015 4:20am

Hi,

I don't think there is anything wrong with alert. It will show [object Object] only. Please check your "createItem()" function calls and also check your list is getting updated or not.

Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 5:57am

Hi,

The list item is not updating...

Below is the my code....

function createItem() {
        var context = new SP.ClientContext.get_current(); //gets the current context
        alert(context);
        var web = context.get_web(); //gets the web object
        var list = web.get_lists();
        var list = web.getByTitle("ListName");
        alert(list);
        //create a new item object
        var listItemCreation = new SP.ListItemCreationInformation();
        //add the item to the list using addItem method
        var newItem = targetList.addItem(listItemCreation);
        var listItemTitle = document.getElementById("txtCaption").value;
        alert(listItemTitle);
        var listItemCustom = document.getElementById("txtTitle").value;
        alert(listItemCustom);
        //using set_item method u can set the values to the item fields
        newItem.set_item('Description', listItemTitle);
        newItem.set_item('Title', listItemCustom);
        newItem.update();
        context.load(newItem);
        context.executeQueryAsync(ItemCreationSuccess, ItemCreationFail);
    }

August 21st, 2015 2:12am

Hi,

Here is a code snippet which works fine in my environment for your reference:

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">

// When the body is loaded, the onload event handler executes each function whose name is contained in this array.
_spBodyOnLoadFunctionNames.push("callCSOM");

function callCSOM()
{
    $("#Button1").click(function()
    {
        // Make sure the SharePoint script file 'sp.js' is loaded before your code runs.
        ExecuteOrDelayUntilScriptLoaded(sharePointReady, "sp.js");
    }); 
}

function sharePointReady() 
{
    addListItems();
}

var clientContext;
var currentUser;
var targetWeb;
var message = "Web retrieved:";

function addListItems()
{
    // Create an instance of the current context to return context information
    clientContext = SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('customlist3');

    //Initializes a new instance of the SP.ListItemCreationInformation Class
    var itemCreateInfo = new SP.ListItemCreationInformation();

    //Creates a new list item in the list
    this.newListItem = oList.addItem(itemCreateInfo);

    //Sets the specified field value
    newListItem.set_item('Title', 'new item here');

    //Commits changed properties of the list item
    newListItem.update();
    clientContext.load(newListItem);

    //Executes the current pending request asynchronously on the server
    clientContext.executeQueryAsync(onRequestSucceeded, onRequestFailed);
}

//the delegate of the method that is called when the request is executed successfully
function onRequestSucceeded() 
{
  alert("Add Item Successfully");
}

//the delegate of the method that is called when the request is executed unsuccessfully
function onRequestFailed(sender, args) 
{
    alert('Error: ' + args.get_message());
}
</script>

You can replace the listname in the code above with your own list name.

Thanks

Best Regards

Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 3:48am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics