If you are facing errors using special characters like '&' or '#' while working with SharePoint REST API, we have a quick fix for you. This issue is not related to SharePoint, but can occur with any REST API, say Jira or Graph API.
Special characters must be encoded before using in the URL.
Consider an example, where you are fetching a SharePoint List Item with title = "Sales & Marketing". You will receive a Microsoft.SharePoint.Client.InvalidClientQueryException error - The expression "Title eq 'sales " is not valid."
https://code2care.sharepoint.com/sites/JiraProjects/_api/web/lists/getByTitle('ActionTracker')/items?$filter=Title eq 'Sales & Marketing'&$select=Title
This is because & is a special and reserved character, you need to encode it and pass the encoded value instead.
FIX ==> In the REST Url, replace & (ampersand) with %26.
_api/web/lists/getByTitle('ActionTracker')/items?$filter=Title eq 'Sales %26 Marketing'&$select=Title
⭐ Different scripting languages treat special characters differently, accordingly you may also need to declare the ampersand using one of these - %26, & amp (without space), & amp; (without space), \&, &&, %26amp;.
⭐ In your code, try using encodeURI('Sales & Marketing') or encodeURIComponent('Sales & Marketing') with variables for escaping.
⭐ Another simple solution would be educate users to use 'and' in place of &, like 'Sales and Marketing'.
⭐ There are plenty of character encoder-decoder available online, convert the special character to encoded value and use it in the REST call.
⭐ Tip - # (hash) is another commonly used character, replace it with %23.
Facing issues? Have Questions? Post them here! I am happy to answer!
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
- SharePoint error - An exception occurred when trying to issue security token: This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms..
- PowerShell iterate and get all SharePoint sites and sub sites
- SharePoint Online: Editing session has ended Message
- How to Share Microsoft SharePoint Site with Users or Groups
- How to delete SharePoint Online List Item using REST API
- SharePoint List excel import - This table exceeds the maximum number of supported rows
- How to disable SharePoint subsite creation option for owners
- SharePoint 2010 August 2015 Update KB3055049 - Duplicate Document ID issue bug fixed
- SharePoint Server 2016 Preview installation error - This Product Key isn't a valid Microsoft Office 2016 Product Key. Check that you've entered it correctly.
- How to upload file programmatically to SharePoint Document Library using Server Object Model C# .Net
- Access URL for SharePoint Tenant Admin Center (Online Office 365)
- Error when deleting SharePoint Online folder or file
- How to extend retiring SharePoint 2010 Workflows and continue with Office 365
- How to generate client id and secret to register SharePoint App with OAuth
- How to delete SharePoint List Item programmatically using C#.Net
- How to retrieve all SharePoint Online List Items using Rest API
- [Solved] SharePoint Search Internal server error exception
- Create SharePoint Site Collection using PowerShell New-SPSite
- Fix SharePoint PowerShell error - The term Get-SPweb is not recognized as the name of a cmdlet function script file or operable program
- How to enable anonymous public access for SharePoint Online site collection, file, folder without login ?
- SharePoint Server 2016 installation System Hardware requirements
- How to create SharePoint Online List Item using REST API
- Changed AD user display name showing old name in SharePoint
- Recommended size and resolution for SharePoint Online Site logo
- Microsoft 365: How to Turn Off Delve in SharePoint Online for All Users
- Detect swipes on Android Activity - Android
- How to create a New Project in Visual Studio Code (VS Code) - HowTos
- Change the default diff or commit editor for git - Git
- How to Get the Relative Path of a file in Python Program - Python
- Python: Convert Set to a List - Python
- How to stop or quit cat command? - HowTos
- Aw Snap! Something went wrong while displaying this webpage Error code: STATUS_INVALID_IMAGE_HASH - Chrome
- Change the default Line Encoding Notepad++ (CR LF) - NotepadPlusPlus