When I try to run this script it doesn’t allow me to, any ideas?
<script src="https://code.jquery.com/jquery-1.10.2.js" type="text/javascript"></script> <form> Title :<br> <input type="text" Id="Title"><br> <button type="button" id= "button" onclick="CreateNew()">Submit</button><br> </form> <script> function CreateNew() { var listName = "samplejee"; var newItemTitle = "Title"; CreateListItemWithDetails(listName, _spPageContextInfo.webAbsoluteUrl, newItemTitle, function () { alert("New Item has been created successfully."); }, function () { alert("Ooops, an error occured. Please try again."); }); } // CREATE Operation // listName: The name of the list you want to get items from // weburl: The url of the web that the list is in. // newItemTitle: New Item title. // success: The function to execute if the call is sucesfull // failure: The function to execute if the call fails function CreateListItemWithDetails(listName, webUrl, newItemTitle, success, failure) { console.log('Title :' + newItemTitle); var itemType = GetItemTypeForListName(listName); var item = { "__metadata": { "type": itemType } }; $.each(newItemTitle, function(name, value){ item[name] = value; }); console.log('item:' + item); console.log('item with JSON.stringify:' + JSON.stringify(item)); $.ajax({ url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items", type: "POST", contentType: "application/json;odata=verbose", data: JSON.stringify(item), headers: { "Accept": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val() }, success: function (data) { success(data); }, error: function (data) { failure(data); } }); } // Get List Item Type metadata function GetItemTypeForListName(name) { return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem"; } </script>
(Visited 23 times, 1 visits today)
This is the error i got when i click on submit button..
(Visited 1 times, 1 visits today)
Beau Cameron commented on answer March 7, 2017
You cannot use “$.each(newItemTitle” because newItemTitle is a string. $.each is expecting an array.
Could you supply a few more details as follows: 1. What is the exact error (inc screenshot) 2. What steps you are performing to recreate the error? 3. Which version of SharePoint?