0
votes

I'm trying to create a SharePoint list item when user clicks a button. I can't figure out how to fix the error I'm getting. The code still seems to work as intended however.

HTML source:

<button type="button" class="btn btn-info" id="getPollval">Submit</button>'

Script - to get value from radio buttons:

var pName = '[name="Poll' + pollId + '"]:checked';
var pId = 'Poll' + pollId;
document.getElementById("getPollval").onclick = function() {
    var pollValue = document.querySelector(pName);
    createListItem(pollValue.value, pId , pollTitle);
}

Script - to create list item on SharePoint:

var siteUrl = '/sites/MyList';

function createListItem(pollValue, pId, pollTitle) {

    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('PollCustomResponses');

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

oListItem.set_item('Title', pollTitle);
oListItem.set_item('Response', pollValue);
oListItem.set_item('PollID', pId);

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

When I click the submit button, it would create a new list item as intended. However, I'm getting below error message in DevTools:

Uncaught TypeError: Cannot read property 'apply' of undefined

The error is highlighting the clientContext.executeQueryAsync line when inspected.

1

1 Answers

0
votes

I use SP.ClientContext.get_current to get context for current site usually, my sample tested script.

 <button type="button" class="btn btn-info" id="getPollval">Submit</button>
    <script type="text/javascript">
        document.getElementById("getPollval").onclick = function () {            
            createListItem("Test");
        }
        //var siteUrl = '/';
        function createListItem(pollValue, pId, pollTitle) {

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

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

            oListItem.set_item('Title', pollValue);
            //oListItem.set_item('Response', pollValue);
            //oListItem.set_item('PollID', pId);

            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>