SharePoint REST API: Not able to get list by getByTitle when list name contains spaces

Jeries Haddad 21 Reputation points
2022-11-14T07:25:10.09+00:00

I am trying to fetch SharePoint list using the SharePoint REST API from my Java application.

When the list's name does not contain a space, it works. If it contains a space it does NOT work.
For example when I do the following: http:(site URL)/_api/web/lists/getbytitle('ONEWORD')/items --> it works

But when I try on another list like this: http:(site URL)/_api/web/lists/getbytitle('TWO WORDS')/items --> it DOESN'T work

I tried encoding the list name with the following in Java: String encoded= URLEncoder.encode("TWO WORDS", "UTF-8"); But it didn't work.

I know there are many questions about this same issue, however everyone is suggesting to get list items by List GUID but I can't use this solution as I'm developing a dynamic tool for several lists with the same name. (Not the same GUID).

Any suggestions? Thank you

Microsoft 365 and Office SharePoint Development
Microsoft 365 and Office SharePoint Server Development
{count} votes

Accepted answer
  1. Tong Zhang_MSFT 9,251 Reputation points
    2022-11-15T05:54:26.8+00:00

    Hi @Jeries Haddad ,

    According to my research and testing, please try to use %20 instead of spaces, for example:

    http:(site URL)/_api/web/lists/getbytitle('TWO%20WORDS')/items  
    

    In addition, you also can try to use filter operation: http:(site URL)/_api/web/lists?$filter=Title eq 'your list title'.

    Note: If you want to get the items from the list using the Title. You have to make 2 calls:

    1. Get List GUID ,for example https://tenant.sharepoint.com/\_api/web/lists?$filter=Title eq 'test/List'&$select=Id
    2. Use the Id from the above response to get the items: https://tenant.sharepoint.com/_api/web/Lists(guid'guid from above request')/Items
      Hope it can help you. Thanks for your understanding.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.