Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Add new List item via Javascript and HTML input forms

$
0
0

Hi All,

I'm not a JavaScript expert so hoping someone can look at this code and let me know what I'm missing.  I'm attempting to create an HTML input form and have the results submitted to a SharePoint 2013 Custom List.  I've been able to get this code to work by hard coding the title field value but that defeats the purpose of an input form. Can someone review the code to see what I'm missing.  Please no links to other articles, and actual vetted code sample would be most helpful.  Thank you,

<input type="text" placeholder="Email Address" name="Email"><br><br><input type="button" value="Submit" onclick="createListItem()"><script>
var siteUrl = 'http://Mysiteurl.com';
var emailValue = $(‘[name=”Email”]’).val();

function createListItem() {

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

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

    oListItem.set_item('Title', 'emailValue');


    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>



Viewing all articles
Browse latest Browse all 7589

Trending Articles