Thursday, 11 August 2022

Support special characters when opening document from Salesforce to SharePoint using Rest API

SCENARIO

While working on one of the requirements for a Manufacture sector project for a client based out of Atlanta, GA, there was a requirement to allow special characters like % & # _ ( ) * - . in the document name while uploading from salesforce to SharePoint and open the document from salesforce custom LWC component.

Let's brief on the scenario: To store\manage files client is using SharePoint through integration. While users upload documents in the salesforce, files are uploaded to SharePoint and shown to salesforce custom component. Now the client has requested new enhancement to allow special characters as well like % & # _ ( ) * - in document name.

Document upload with special characters works fine as we were using salesforce content document to upload files. After that our custom API is responsible to upload same file and give response back to open uploaded files on success dialog box.

However, file was being uploaded in SharePoint and few changes were done in Custom API to provide response back for the document name containing special character.

CHALLENGE

Custom API changes were not enough to make solution fully work as we were able to open such documents from success dialog box but not from salesforce Custom Component. We were using SharePoint OOTB REST API to build link for SharePoint document link.

APPROACH

We have used salesforce APEX ENCODINGUTIL class to encode file name in URL. This solved the issue for most of the documents having special characters but not for which contains space in Document name.

After few analysis, we found ENCODINGUTIL replace space with '+' sign. With the tricky solution we resolved the issue by replacing '+'  with '%20' in encoded Document name. Let's understand with the example.

EXAMPLE:

  • Find below few specific parameter being used in solution.
    • Document Name : Test % & # _ ( ) * - .docx
    • OOTB\Standard SharePoint document Encoded URL : 
      SiteURL/Shared%20Documents/General/Test%20%25%20&%20%23%20_%20(%20)%20%20-%20.docx
  • To get document details, we make SharePoint  REST API callout.
  • In the response, we get FileRef As 
    SiteURL/Shared Documents/Genetal/Test % & # _ ( ) * - .docx
  • As we can see from both the URL SharePoint document URL is Encoded and in the response FileRef is not Encoded in format.
  • So we need to Encode FileRef  and replace '+' with '%20' in the apex as 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'.

  • Now our DocumentencodedUrl will work and document can be opened from salesforce custom component.


CONCLUSION

By encoding URL and replacing '+' with '%20' we can resolve the document URL issue having special character and space.

 
If you have any questions you can reach out our
Salesforce Consulting team here.

No comments:

Post a Comment