0

I have a small application that when initialized gets some information from a Sharepoint List via REST call.

Those information eventually needs to be modified from inside the application, again via REST API, and saved in the same List. That is why, when I originally get those information, I also store the ListItemEntityTypeFullName value of the List in a global scope variable, that will be later passed in the POST function.

My code to get the list items is as follows:

    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items",
        type: "GET",
        headers: {"Accept": "application/json;odata=verbose"}
    }).done(function(data) {
        listType = data.d.results[0].__metadata.type
    });

If no element is found, the only way I know to store the ListItemEntityTypeFullName value would be to make a second ajax call as it follows:

    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/?$select=ListItemEntityTypeFullName",
        type: "GET",
        headers: {"Accept": "application/json;odata=verbose"}
    }).done(function(data) {
        listType = data.d.ListItemEntityTypeFullName
    });

Now, my question is: is there any way to make both requests in a single call, so that if no elements have been found in the List, I can still get somewhere the ListItemEntityTypeFullName value? I am trying to optimize the number of calls I have to make to keep the application fast and smooth.

Thanks!

1

Still need to create another request if there is no items in list. As when list have no items, data.d.results array will be empty.

<script src="https://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
<script type='text/javascript'>
var listType;
$( document ).ready(function() {
      $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Tasks')/items",
        type: "GET",
        headers: {"Accept": "application/json;odata=verbose"}
    }).done(function(data) {
        if(data.d.results.length > 0)
        {
          listType = data.d.results[0].__metadata.type;
          console.log(listType);
        }
        else
        {
          GetListItemEntityTypeFullName();
        }
    });
});

function GetListItemEntityTypeFullName()
{
   $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Tasks11')/?$select=ListItemEntityTypeFullName",
        type: "GET",
        headers: {"Accept": "application/json;odata=verbose"}
    }).done(function(data) {
        listType = data.d.ListItemEntityTypeFullName
        console.log(listType);
    });
}
</script>

Another option to get ListItemEntityTypeFullName is to generate directly with this format:

SP.Data.ListNameListItem

2
  • Yes, this was my approach so far, I guess there is no way to merge the two calls together. Thank you anyway – supertrip86 Apr 7 '20 at 8:43
  • Yes. Or you could directly use SP.Data.ListNameListItem directly without doing a request . – Jerry_MSFT Apr 7 '20 at 9:03

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.