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!
- That did'nt work, Issue type User not in directory - SharePoint external access error
- How to get SharePoint Online user details from person or group column using REST API
- Create SharePoint Site Collection with new Content database in existing web application
- Fix Power BI 404 not found error when connecting SharePoint Online List as Data Source
- How to get the SharePoint Tenant Login URL
- How to wrap column text in SharePoint Online Modern List Grid View using JSON formatting
- SharePoint Server 2016 installation System Hardware requirements
- Cannot load PowerApps form in SharePoint Online due to repeated authentication
- Change SharePoint search results FullTextSqlQuery RowLimit 10000
- How to create SharePoint Document Library
- How to exclude results from SharePoint Search
- SharePoint 2010 DataForm Unable to display this Web Part System.StackOverflowException
- Fix Error 2711 SQL RBS client - The installer has encountered an unexpected error. The specified Feature name ('Docs') not found in Feature table
- How to upload file programmatically to SharePoint Document Library using Server Object Model C# .Net
- SharePoint error cannot connect to the configuration database
- Drag drop files here option missing for SharePoint document library
- How to activate and create SharePoint Spaces feature for mixed reality experience - 2D/3D images and web parts
- [Fix] Office 365 Sharepoint One Drive Error: Something went wrong. This is a temporary issue
- SharePoint 2016 error - Could not find file ManageUserProfileServiceApplicationTenantSimplified.xml
- SharePoint List excel import - This table exceeds the maximum number of supported rows
- How to customize SharePoint Modern list form using JSON formatting
- New-SPLogFile PowerShell - create new SharePoint log file
- SharePoint Online REST API not returning all list items and limit to only 100 rows
- How to Get or Set SharePoint Document ID _dlc_DocId using PowerShell
- Get-ADUser PowerShell - Get AD user details using email address
- Brew Error - This command updates brew itself and does not take formula names - HowTos
- Code2care.org: A Decade of Serving the Tech Community - News
- AutoSave button not working on Office on Mac (Word, Excel or Powerpoint) - MacOS
- JSP Hello World Program Tutorial with Eclipse and Tomcat Server - Java
- Take Screenshots on Mac OS without Keyboard - Mac-OS-X
- Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver - Java
- List of 60 useful FTP Client Commands to access server - FTP
- The default interactive shell is now zsh. [macOS] - MacOS