4

Code:

  function createListItem() { 
            var item =  {
                "__metadata": { "type": "SP.Data.listnameListItem" },   
                "Title": "testsetsetset",
                "Column1": "Test", 
                "Column2": "Test" , 
            } ;

        $.ajax({
              url: apphostweb + "/_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) {
                alert("Successful");            
            },
            error: function (data) {
                alert("Failure");
            }
        });

}

I can insert the title to the list, but after I added the column1 and column2 field in the request. There is a error:

{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"A type named 'SP.Data.listnameListItem' could not be resolved by the model. When a model is available, each type name must resolve to a valid type."}}}

Can we create whole item using this method? OR another method should be used?

8

This issue is related to ListItemEntityTypeFullName. ListItemEntityTypeFullName must be specified in __metadata. Your error message seems SP.Data.listnameListItem is not the valid value for ListItemEntityTypeFullName. This value can be found in the following end-point.

/_api/web/lists/GetByTitle('listName')/ListItemEntityTypeFullName

Make a GET request to the above URL or hit directly to the browser and find the value of ListItemEntityTypeFullName. Finally add this value in _metadata.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.