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

Add new item to custom list using sharepoint 2013 REST API

$
0
0
/ Getting the item type for the list
        function getListItemType(name) {
            return "SP.Data." + name[0].toUpperCase() + name.substring(1) + "ListItem";
        }

        // Adding a list item with the metadata provided
        function addListItem() {
            listname = "Sample";
            url = "http://xxxxxxx:12794";
            metadata = "Title" + ':' + "New Title";
            alert(metadata);
            var test = getListItemType(listname);
            alert(test);
            // Prepping our update
            var item = $.extend({"__metadata": { "type": getListItemType(listname) },"Title":  "New Title"
            });
            //var metadata = “{ ‘__metadata’: { ‘type’: ‘SP.Folder‘ }, ‘ServerRelativeUrl’: ‘/” + serverRelativeUrl + “‘}”
               
            alert('hi');
            alert(item);

            // Executing our add
            $.ajax({
                url: url + "/_api/web/lists/getbytitle('" + listname + "')/items",
                type: "POST",
                processData: false,
                contentType: "application/json;odata=verbose",
                data: JSON.stringify(item),
                headers: {"Accept": "application/json;odata=verbose"
                },
                success: function (data) {
                    alertt("Added successfully");// Returns the newly created list item information
                },
                error: function (data) {
                    alert("Failed operation");
                }
            });

        }



Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>