0

Having some trouble with a very basic use of the JSOM controls for adding list items.

My code:

    function submitClick(who, where, when, description, name, number) {

    //alert(who + where + when + description + name + number);
    var context = SP.ClientContext.get_current();
    var list = context.get_web().get_lists().getByTitle('*my list name*');

    var itemCreateInfo = new SP.ListItemCreationInformation();

    var oListItem = list.addItem(itemCreateInfo);
    oListItem.set_item('Who', who);
    oListItem.set_item('Where', where);
    oListItem.set_item('When', when);
    oListItem.set_item('Description', description);
    oListItem.set_item('Name', name);
    oListItem.set_item('Contact', number);
    oListItem.set_item('CreateDate', Date.now());
    oListItem.set_item('Title', Date.now().toString()); //column is hidden but required
    oListItem.update();

    context.load(oListItem);
    context.executeQueryAsync(onCreateListItemSuccess, onCreateListItemFailed);

    function onCreateListItemSuccess() {
        alert('yay');
    }
    function onCreateListItemFailed(sender, args) {
        alert('failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
}

As you can see, it's just about as basic as it gets- Problem is, the ProcessQuery call always returns this:

{
"SchemaVersion":"15.0.0.0","LibraryVersion":"16.0.21010.12006","ErrorInfo":{
"ErrorMessage":"Unknown Error","ErrorValue":null,"TraceCorrelationId":"71d0ad9f-b05d-b000-95bc-b4c0e452fdc8","ErrorCode":-1,"ErrorTypeName":"Microsoft.SharePoint.Client.UnknownError"
}

That's right- "Unknown Error" and no ErrorValue. What's going on here?

2
  • What is the data type of Who & When columns? what values you are passing to these columns? Commented Feb 22, 2021 at 18:31
  • They are all Single Line of Text except for the CreateDate. Currently just passing dummy data to get it working, so something like 'a','b','c','d','e' Commented Feb 22, 2021 at 19:29

1 Answer 1

0

CreateDate should be a Date column, then should set with a date object in JavaScript, please modify as below:

    <script type="text/javascript">
        function CreateListItem() {

            var context = SP.ClientContext.get_current();
            var list = context.get_web().get_lists().getByTitle('CustomList');

            var itemCreateInfo = new SP.ListItemCreationInformation();

            var oListItem = list.addItem(itemCreateInfo);
            const timeElapsed = Date.now();
            const today = new Date(timeElapsed);
            oListItem.set_item('CreateDate', today);
            oListItem.set_item('Title', Date.now().toString()); //column is hidden but required
            oListItem.update();

            context.load(oListItem);
            context.executeQueryAsync(onCreateListItemSuccess, onCreateListItemFailed);
        }

        function onCreateListItemSuccess() {
            alert('yay');
        }
        function onCreateListItemFailed(sender, args) {
            alert('failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }
    </script>
3
  • That... somehow worked! So Date.now() doesn't actually create a date object, then? Weird. Commented Feb 23, 2021 at 16:33
  • Glad that it's working now :). Yes, Date.now() not creating the date object, you can test in Browser Developer Tool (F12), it's int value. And as it's working, I suggest you could accept as answer so that it could also help others who have the similiar question in the forum. Thanks
    – Jerry
    Commented Feb 24, 2021 at 0:41
  • Yep, was waiting for the minimum time delay, accepted now. Commented Feb 25, 2021 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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