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.
Have Questions? Post them here!
- Move Copy Migrate SharePoint OneDrive files folders to different site collection location
- How to generate client id and secret to register SharePoint App with OAuth
- How to share SharePoint site or document with all users in organization
- Get-ADUser PowerShell - Get AD user details using email address
- error CAML Query containing special characters
- Access URL for SharePoint Tenant Admin Center (Online Office 365)
- Fix SharePoint Error - The Managed Metadata Service or Connection is currently not available. The Application Pool or Managed Metadata Web Service may not have been started
- SharePoint - The URL is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web.
- How to delete SharePoint List Item programmatically using C#.Net
- How to Share Microsoft SharePoint Site with Users or Groups
- JSON column formatting to preview SharePoint Online file on mouse hover
- SharePoint List redirect user after submitting form NewForm.aspx
- See actual SharePoint error exception modify web.config
- SharePoint Server 2016 IT Preview Deprecated Removed features
- How to create SharePoint Document Library
- How to Get or Set SharePoint Document ID _dlc_DocId using PowerShell
- How to disable SharePoint subsite creation option for owners
- PowerShell - How to use Try Catch Finally blocks for error exception handling (Windows/SharePoint)
- SharePoint error - Your organization doesn't allow sharing with users from this domain. Please contact your IT department for help. (OSE403)
- [Solved] SharePoint Search Internal server error exception
- How to wrap column text in SharePoint Online Modern List Grid View using JSON formatting
- How to extend retiring SharePoint 2010 Workflows and continue with Office 365
- Changed AD user display name showing old name in SharePoint
- How to hide or remove quick launch left navigation from SharePoint Online Modern site page
- How to enable anonymous public access for SharePoint Online site collection, file, folder without login ?
- [fix] Loading class com.mysql.jdbc.Driver is deprecated - MySQL
- How to enable Do Not Disturb mode for Notification Center in Mac OS X 10.10 Yosemite - Mac-OS-X
- Steps to Delete or Deactivate Instagram Account - HowTos
- Notepad++ do not show CRLF characters - NotepadPlusPlus
- YAML Parser using Java Jackson Library Example - Java
- [Fix] Spring Tool Suite STS Code Autocomplete not working with Eclipse - Eclipse
- Test internet speed using macOS Terminal command - MacOS
- SharePoint error 2130575251 - The security validation for this page is invalid and might be corrupted - SharePoint