24

I am currently using Office 365 and lots of REST queries to display data. However I am now creating my own forms using Bootstrap and jQuery.

However, how do I use REST to add the data to the list?

Can I have some code example with the columns, so I can try, please?

3 Answers 3

25

This blog has a code snippet that is useful:

// 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) {
    var itemType = GetItemTypeForListName(listName);
    var item = {
        "__metadata": { "type": itemType },
        "Title": newItemTitle
    };

    $.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";
}
2
  • function GetItemTypeForListName(name) { return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join('x0020').slice(1) + "ListItem"; } this works for me if the list name has space in it Commented Nov 25, 2019 at 6:51
  • How to deal with authentication? I use this code from the same browser where there I have an open session for that same SPO site and I get 403
    – golimar
    Commented Dec 1, 2020 at 14:02
8

I would recommend the article Manipulating list items in SharePoint Hosted Apps using the REST API, it contains a thorough description with examples how to perform CRUD operations using REST API in SharePoint 2013.

Below is provided a slightly modified version of function for creating list item from the specified article:

function createListItem(siteUrl,listName, itemProperties, success, failure) {

    var itemType = getItemTypeForListName(listName);
    itemProperties["__metadata"] = { "type": itemType };

    $.ajax({
        url: siteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items",
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(itemProperties),
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function (data) {
            success(data.d);
        },
        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";
}

Usage

//specify item properties
var itemProperties = {'Title':'Order task','Description': 'New task'};
//create item
createListItem(_spPageContextInfo.webAbsoluteUrl,'Tasks',itemProperties,
   function(entity){
      console.log('New task ' + entity.Title + ' has been created');
   },
   function(error){
      console.log(JSON.stringify(error));
   }
);
4

You can use this code for updating Text,Number,Lookup,Content Type,Single Select managed Metadata columns fields using REST API:

item = {
"__metadata": {
    "type": "SP.Data.ABCListItem"
},
"Title": "Name",
"Name": "Name field",
"PId": 736,
"short": "short desc",
"long": "long desc",
"category": "High",
"status": "New",
"ContentTypeId":"0x0100624BD12SDDS5E1C4F54CBF434A4C07E90D630015827ECAAC097748868AAB8A57615B52",
"MetadataSingleSelect": {
    "TermGuid": "4cbeecf8-8833-4328-1234-5679b0a42f46",
    "WssId": -1
}
};
$.ajax({
url: fullurl +"/_api/web/lists/getbytitle('Listnme')/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) {
    alert('Success');
},
error: function(data) {
   alert(Error);
}
});

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.