html code

Enable Compatibility For Special Characters When Accessing Documents From Salesforce To SharePoint Via Vhe Rest API

The situation or the context

While addressing a project requirement for a Manufacturing sector client based in Atlanta, GA, there was a need to permit special characters such as % & # _ ( ) * – . in document names when uploading from Salesforce to SharePoint and when opening the document from a custom Salesforce LWC component.

Here’s a brief overview of the scenario: The client utilizes SharePoint for file management through integration. When users upload documents in Salesforce, these files are transferred to SharePoint and displayed within a Salesforce custom component. The client requested an enhancement to enable special characters like % & # _ ( ) * – in document names.

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

The difficulty or the obstacle

The alterations made to the Custom API weren’t sufficient to fully resolve the issue. Although we could open these documents from the success dialog box, accessing them directly from the Salesforce Custom Component remained problematic. 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. This resolved the issue for most documents with special characters, except those containing spaces in the document name.

After some analysis, we discovered that ENCODINGUTIL replaces spaces with the ‘+’ sign. To tackle this, we devised a workaround by replacing the ‘+’ with ‘%20’ in the encoded document name, resolving the issue. Let me explain using an example.


  • Here are a few specific parameters utilized in the solution:

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

OOTB\Standard SharePoint document Encoded URL : 


  • For obtaining document details, we initiate a SharePoint REST API callout. Upon receiving the response, we retrieve FileRef as:
  • The response contains FileRef represented as

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

  • As evident from both URLs, the SharePoint document URL is encoded, while the response FileRef is not encoded in the same format.
  • Consequently, we must encode 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 above code, we have used EncodeUtil class and its urlEncode Method to encode the URL string and replace the method to replace ‘+’ 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.