SharePoint 2013 list item update with spaces in list name

7.2k Views Asked by At

I'm trying to update a SharePoint List with the name "Director's Task List (DTL)" for single items in it. I've got the following code:

var listName = 'Director''s Task List (DTL)';

var requestURI = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items(" + ID + ")";

$.ajax({
    uri: requestURI,
    type: "POST",
    contentType: "application/json; odata=verbose",
    data: JSON.stringify(data),
    headers: {
        "Accept": "application/json; odata=verbose",
        "X-HTTP-Method": "MERGE",
        "IF-MATCH": "*",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    }
});

where data is:

var data = {
    '__metadata': { 'type': getItemTypeForListName(listName)},
    'Suspense_x0020_Status' : 
        {
            '__metadata': { 'type': 'SP.FieldUrlValue' },
            'Description': statusObject[ID]["color"],
            'Url': statusObject[ID]["html"]
        }
};

and getItemTypeForListName(listName) is:

function getItemTypeForListName(listName) {
    var itemType = "SP.Data." + listName.charAt(0).toUpperCase() + listName.slice(1) + "ListName";
    var encItemType = itemType.replace(/\s/g,'_x0020_');
    return(encItemType);
}

I am unable to get the code to complete correctly. I get an error:

A type named 'SP.Data.Director''s_x0020_Task_x0020_List_x0020_(DTL)ListName' could not be resolved by the model. When a model is available, each type must resolve to a valid type.

I know it has something to so with spaces and the single apostrophe, but I have tried several solutions, without luck. I have seen that the double single apostrophe, "''" , is a working method that has helped for me before in getting this list name type to work. So don't instantly point to that.

Any thoughts?

2

There are 2 best solutions below

3
Vadim Gremyachev On

It seems getItemTypeForListName function returns wrong list item entity type name.

The following endpoint returns the actual list item entity type name: /_api/web/lists/GetByTitle('<list title>')?$select=ListItemEntityTypeFullName

For the list title: Director's Task List (DTL):

  • getItemTypeForListName function returns SP.Data.Director''s_x0020_Task_x0020_List_x0020_(DTL)ListName
  • while the specified endpoint returns: SP.Data.Directors_x0020_Task_x0020_List_x0020_DTLListItem

Example

The example shows how to return list item type name:

var listTitle = "Director''s Task List (DTL)"

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);
})
.fail(function(error){
  console.log(JSON.stringify(error));
});

where

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);
}
0
Matthew Dewell On

Vadim, Thank You for your help. I think I finally got the request to work by manually entering it like you requested. I had to use this though:

http://[server url]/_api/web/lists/GetByTitle('Director''s%20Task%20List%20(DTL)')listItemEntityTypeFullName

This returned a value as SP.Data.TasksListItem. I then changed my data entry as follows:

var data = {
    '__metadata': { 'type': SP.Data.TasksListItem},
    'Suspense_x0020_Status' : 
        {
            '__metadata': { 'type': 'SP.FieldUrlValue' },
            'Description': statusObject[ID]["color"],
            'Url': statusObject[ID]["html"]
        }
};

This then appeared to work in getting the value to update in the List.

Thank You for your help, Vadim.