0

I am easily able to create record in other lists using rest API.

But with this one list, api failed. Listname: Project_List Can anyone please help me.

Thanks

Code:

 function GetItemTypeForListName(name){
        return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";  
    }
function AddListItem(){
var listName = "Project_List";  
var itemType = GetItemTypeForListName(listName);
            var myID ="";   
            var name = "276 Flinders St";
              
            var item = {  
                "__metadata": { "type": itemType },  
                "Title": name
            };
            $.ajax({  
                url:  _spPageContextInfo.webAbsoluteUrl  + "/_api/web/lists/getbytitle('" + listName + "')/items(" + myID + ")",  
                type: "POST", 
                contentType: "application/json;odata=verbose",  
                data: JSON.stringify(item),  
                headers: {  
                    "Accept": "application/json;odata=verbose",  
                    "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
                    "IF-MATCH": "*",  
                    "X-HTTP-Method":"POST",  
                },  
                success: function (data) {
                    alert("success");  
                },  
                error: function (data) {  
                    alert("failed");  
                }  
            });  
        }

Edit 1: I tried with all solution which you guys gave, but still not working

Error screenshot attached: enter image description here

Edit 2:

ListItemtype: enter image description here

  • 1
    Can you please add the error you got to your question? – Ganesh Sanap Jun 16 at 15:59
  • added error in question, – Sayyed Amir Hussain Jun 17 at 5:31
  • Please add a error message. To see error message use your code like: error: function (error) { console.log(error); } and add the error message you got in console. Also, try the suggestions given below in my answer and let me know if it works for you. – Ganesh Sanap Jun 17 at 5:33
  • "A type named 'HotWorkPermit3_ImagesListItem' could not be resolved by the model. When a model is available, each type name must resolve to a valid type." – Sayyed Amir Hussain Jun 17 at 5:47
2

As per my understanding you are getting this issue because you are passing wrong type inside __metadata.

Before using the main REST call check the ListItemEntityTypeFullName of your own list and pass it in __metadata property.

You can get this property by hitting below URL in browser tab:

https://<sitecollection>/<site>/_api/web/lists/getbytitle('listName')?$select=ListItemEntityTypeFullName

Do not use the GetItemTypeForListName function in your code. This is kind of a hack which will work correctly only if there are no spaces or special characters in your list name.

Hit above URL in browser tab and pass the ListItemEntityTypeFullName you got from this endpoint in your itemType variable.

Check my answer given in below question:

how to axios post to SharePoint 2013 list

Note:

Generally, _ in list name gets converted into _x005f_ and space in list name gets converted into _x0020_ in ListItemEntityTypeFullName of SharePoint list. But, it is always recommended to use above REST endpoint and check the ListItemEntityTypeFullName of your list.

| improve this answer | |
  • thanks for replying. i tried with all these not unfortunately not working – Sayyed Amir Hussain Jun 17 at 5:32
  • Still getting same error: responseText: "{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"A type named 'HotWorkPermit3_x005f_ImagesListItem' could not be resolved by the model. When a model is available, each type name must resolve to a valid type."}}}" – Sayyed Amir Hussain Jun 17 at 6:07
  • Try running this in new browser tab: <tenant>.sharepoint.com/sites/cactus/_api/web/lists/getbytitle('HotWorkPermit3_Images')/entityTypeName and attach the screenshot of the result to your question. You need to use the result you get from this endpoint. – Ganesh Sanap Jun 17 at 6:11
  • added, see if it can help me – Sayyed Amir Hussain Jun 17 at 6:21
  • 1
    Sorry, I forgot to add "SP.Data" before entityTypeName in metadata. that is why it is causing this issue. Check updated answer and if it helped you to solve your issue please upvote(^) and accept as an Answer. – Ganesh Sanap Jun 17 at 7:14
1

Is this display name of the list?

Try with %5F var listName = "Project%5FList";

OR

/lists/getbytitle('" + encodeURIComponent(listName) + "')

https://julieturner.net/2016/09/special-characters-in-rest-listitem-metadata/

| improve this answer | |
1

While you are generating the type using : GetItemTypeForListName in you machine, You should not use like this pattern. You can get the type from the rest end point. The list name with special character like _ will not have the type with proper pattern. Check the type of this list.

| improve this answer | |
1

Thanks all guys who helped me to solve my issue.

I solved issue by removing quotes from item and previously i was using a variable to store column data and name but now i am directly writing inside ajax.

That's now working successfully, don't know why its not working when i use variable. For further clarification, i added screenshot also. enter image description here

| improve this answer | |
1

The reason for the problem is that SharePoint will escape special characters, causing '_ ' to ' x005f '.The title will not affect the excution,but the itemType get by the function GetItemTypeForListName is wrong.

My test code works well:

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
$(function (){
CreateListItemWithDetails("test")
})
function CreateListItemWithDetails(listName) {
   
    var item = {
        "__metadata": { "type": "SP.Data.Test_x005f_testListItem" },
        "Title": "test"
    };

 

    $.ajax({
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/Lists/getbytitle('test_test')/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("s");
        },
        error: function (data) {
            console.log(data);
        }
    });
}

</script>

You could update the GetItemTypeForListName,when the itemtype get by GetItemTypeForListName contains '_',we should replace it with ' x005f '.

| improve this answer | |

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.