0

I am trying to insert list item's value through JavaScript object model in SP. When I call set_item('fieldname','Title'), it throw error "c.set_item is not a function". How to successfully Insert a List Item?

Here is my code:

var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo); 
oListItem.set_item('Title', 'My New Item!');
oListItem.update();
  • 1
    Why are you writing this.oListItem in one line and oListItem in the other? are they the same? What does oListItem look like, before you call set_item() ? – Nils May 28 '19 at 7:39
  • @Mavani Please provide your load and execute query code as well. Also, is there any required field other than "Title" ? – Sohail Shaikh May 28 '19 at 7:44
  • @SohailShaikh, have you tried the code snippet below ? – Jerry_MSFT May 30 '19 at 7:34
  • @Jerry_MSFT I know the below code will add the item successfully. I was just trying to check the full code OP (@Mavani) is executing. – Sohail Shaikh May 30 '19 at 11:27
1

Here is a code snippet for your reference:

<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(createListItem, 'sp.js');
function createListItem() {

    var clientContext = new SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('Test');

    var itemCreateInfo = new SP.ListItemCreationInformation();
    this.oListItem = oList.addItem(itemCreateInfo);

    oListItem.set_item('Title', 'My New Item!');

    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());
}

</script>

enter image description here

Reference:

How to: Create, Update, and Delete List Items Using JavaScript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.