Free computer code on screen

Enable Compatibility For Special Sharacters While Opening Documents From Salesforce To SharePoint Via The Rest API

The situation or the context.

While addressing a project requirement for a manufacturing sector client based in Atlanta, GA, we encountered a need to permit special characters such as % & # _ ( ) * – . in document names during uploads from Salesforce to SharePoint. The objective was to open these documents from a custom Lightning Web Component (LWC) within Salesforce.

Here’s an overview of the scenario: The client utilizes SharePoint for file storage and management, integrated with Salesforce. When users upload documents in Salesforce, these files are sent to SharePoint and displayed in a custom Salesforce component. The client requested an enhancement to allow special characters such as % & # _ ( ) * – in document names.

Initially, document uploads with special characters functioned correctly using Salesforce Content Document. Subsequently, modifications were made to our custom API to enable responses for document names containing special characters after uploading files to SharePoint.

The difficulty or the obstacle

The alterations made to the Custom API weren’t adequate for the complete functionality. Although we could open these documents from the success dialog box, accessing them directly from the Salesforce Custom Component posed a challenge. Our approach involved utilizing SharePoint’s OOTB REST API to generate links for SharePoint document access.

The method or the strategy

We employed the Salesforce APEX ENCODINGUTIL class to encode file names in URLs. While this resolved the problem for most documents with special characters, it didn’t work for those containing spaces in the document name.

Upon analysis, we discovered that ENCODINGUTIL replaces spaces with the ‘+’ sign. To address this, we devised a workaround by replacing the ‘+’ with ‘%20’ in the encoded document name. Allow me to illustrate this with an example.


  • Here are some specific parameters utilized in the solution:

File Name: Test % & # _ ( ) * – .docx

Default SharePoint Encoded URL:


  • For document details retrieval, we initiate a REST API call to SharePoint.
  • The response includes FileRef as

SiteURL/Shared Documents/General/Test % & # _ ( ) * – .docx

  • As evident from both URLs, the SharePoint document URL is encoded, while the FileRef in the response is not in an encoded format.
  • Consequently, we must encode the FileRef and replace ‘+’ with ‘%20’ in the Apex code as shown below.
String DocumentencodedUrl = EncodingUtil.urlEncode(FileRef, 'UTF-8');
//By using EncodingUtil class and its methods we can encode and decode URL strings.

System.Debug('Encoded Document URL--> '+DocumentencodedUrl);
// Encoded Document URL--> SiteURL%2FShared+Documents%2FGenetal%2FTest+%25+%26+%23+_+%28+%29+*+-+.docx

String DocumentUrl = DocumentencodedUrl.replace('+','%20');
//By replace string function we are replacing '+' with '%20'

System.Debug('Document URL--> '+DocumentUrl);
//Document URL--> SiteURL%2FShared%20Documents%2FGenetal%2FTest%20%25%20%26%20%23%20_%20%28%20%29%20*%20-%20.docx

In the provided code snippet, we utilized the EncodeUtil class along with its urlEncode method to encode the URL string. Additionally, we employed the replace method to substitute the ‘+’ with ‘%20’.

  • With this adjustment, our DocumentencodedUrl will function, enabling the document to be accessed from the Salesforce custom component.

Summary or Closing Remarks

Encoding the URL and substituting ‘+’ with ‘%20’ effectively resolves issues with document URLs containing special characters and spaces.