Thursday 11 November 2021

[Resolved:] File Upload using REST API from Salesforce to SharePoint with special characters in filename.


SCENARIO

While working on one of the requirements for a packaging solutions customer based out Atlanta, Georgia there was a requirement to allow Salesforce users to upload documents from Salesforce entities.


Due to Salesforce storage limitation, uploaded documents are stored into SharePoint.


CHALLENGE

We have achieved functionality of uploading a file from Salesforce to SharePoint by creating custom lightning web component. We have used SharePoint REST APIs for direct upload from Salesforce to SharePoint.

However, we got stuck with the error while uploading a file contains special characters. 


RESOLUTION

We tried various solutions to resolve the issue but no luck. After relentless efforts, we concluded to utilize simple JavaScript function which encodes given file name and upload to SharePoint with the same name through REST APIs. Finally, it does the trick. Below is the sample for the same:


var updatedFileName = encodeURIComponent(this.files.name);
var url1 = 'https://br.sharepoint.com/sites/SFUpload' + "/_api/web/GetFolderByServerRelativePath('/Sites//SFUpload/Document Library')/Files/AddUsingPath(overwrite=true,decodedurl='" + updatedFileName + "')";


CONCLUSION

By encoding the file name, we are successfully able to upload a file with special characters from Salesforce to SharePoint.


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