0

I have created the list called "dept info" in SharePoint 2013. And I have tried the API call to insert the item into list.but the list name contains the spance.if without space the other list items are inserted .with space is not working. the API code is,

SupportInfoListName="Dept%20Info";
        var itemType = GetItemTypeForListName("Dept%20Info");
        var item = {
            "__metadata": { "type": itemType },
            "Title": "56464*/**"
            };

        $.ajax({
            url:"siteirl/_api/web/lists/getByTitle('Dept Info')/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(data);
            }
        });

        // Get List Item Type metadata
        function GetItemTypeForListName(name) {
console.log(name);
            return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
        }

Suggest an idea to insert an item using API into list.

3 Answers 3

2

Please check your metadata value in JSON object. You need to replace space in list name with special character "x0020". Please try with this for example if your ListName is "Test Data" then your metadata value "SP.Data.Test_x0020_DataListItem".

1
  • Thanks . I made a mistake in metadata value in JSON object.Got it now.
    – PGP
    Commented Jan 11, 2017 at 5:59
1

Can you try to replace this line

var itemType = GetItemTypeForListName("Dept%20Info");

with

var itemType = GetItemTypeForListName("Dept Info");
1
  • ya, I tried .but it's not working.
    – PGP
    Commented Jan 11, 2017 at 5:48
1

You need to enter the list title in the GetItemTypeForListName parameter

Try as below:

    SupportInfoListName="Dept Info";
            var itemType = GetItemTypeForListName("Dept Info");
            var item = {
                "__metadata": { "type": itemType },
                "Title": "56464*/**"
                };

            $.ajax({
                url:"siteirl/_api/web/lists/getByTitle('Dept Info')/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(data);
                }
            });

            // Get List Item Type metadata
            function GetItemTypeForListName(name) {
    console.log(name);
                return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
            }

Looks like something is wrong with listitementityname. Try below code:

var listTitle = "Dept Info"

function executeJson(options) 
{
    var headers = options.headers || {};
    headers["Accept"] = "application/json;odata=verbose";
    if(options.method == "POST") {
        headers["X-RequestDigest"] = $("#__REQUESTDIGEST").val();
    }   

    var ajaxOptions = 
    {       
       url: options.url,   
       type: options.method,  
       contentType: "application/json;odata=verbose",
       headers: headers
    };
    if(options.method == "POST") {
      ajaxOptions.data = JSON.stringify(options.payload);
    }  

    return $.ajax(ajaxOptions);
}


function getItemTypeForListName(listTitle)
{
  return executeJson({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/?$select=ListItemEntityTypeFullName",
    method: 'GET'
  }).then(function(data){
       return data.d.ListItemEntityTypeFullName;
  });  
}

getItemTypeForListName(listTitle)
.done(function(name){
   console.log(name);

   var item = {
                "__metadata": { "type": name },
                "Title": "56464*/**"
            };

    $.ajax({
            url:"siteurl/_api/web/lists/getByTitle('"+ listTitle +"')/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) {
                console.log("success");
            },
            error: function (data) {
                console.log(data);
            }
        });

})
.fail(function(error){
  console.log(JSON.stringify(error));
});
1
  • it shows the "404 (Not Found)" in URL. but copy paste the URL value in browser I can view the items.
    – PGP
    Commented Jan 11, 2017 at 5:51

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.