function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Fabio Andree Scheurel MWFabio Andree Scheurel MW 

Files Connect and APEX: attach external (SharePoint) file to record

I have configure SharePoint Online successfully as my external document management and file storage system using the standard Files Connect adapter.

Now I want to attach a file that is stored in SharePoint to a Salesforce Record, in APEX code. However I was not able to figure out how this might be possible. Anyone succeeded in doing this? I would also go for creating a Chatter post in APEX, but the FilesCapabilities do only support linkage to existing ContentDocument record, but my SharePoint files are not (yet) listet there.
Best Answer chosen by Fabio Andree Scheurel MW
Fabio Andree Scheurel MWFabio Andree Scheurel MW
Adding a ContentVersion with Sharepoint Files Connect identifiers (ExternalDataSourceId, ExternalDocumentInfo1, ExternalDocumentInfo2) will create the new ContentDocument as well, and then a new ContentDocumentLink will attach the file to a record.

See https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentversion.htm and https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentdocument.htm
To create a document, create a new version via the ContentVersion object without setting the ContentDocumentId. This automatically creates a parent document record.

ExternalDataSourceId is the ID of the External Data Source, ExternalDocumentInfo1 is the full URL to the preview on the Sharepoint where the user will be redirected to after clicking onto "Open in Sharepoint" when looking at the file preview in Salesforce, and ExternalDocumentInfo2 is the file identifier on the SharePoint server in the form of

item:<Root Path in Base64 encoding>:<List View ID>:<List Item ID>

Hope that helps.
 

All Answers

Fabio Andree Scheurel MWFabio Andree Scheurel MW
Adding a ContentVersion with Sharepoint Files Connect identifiers (ExternalDataSourceId, ExternalDocumentInfo1, ExternalDocumentInfo2) will create the new ContentDocument as well, and then a new ContentDocumentLink will attach the file to a record.

See https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentversion.htm and https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentdocument.htm
To create a document, create a new version via the ContentVersion object without setting the ContentDocumentId. This automatically creates a parent document record.

ExternalDataSourceId is the ID of the External Data Source, ExternalDocumentInfo1 is the full URL to the preview on the Sharepoint where the user will be redirected to after clicking onto "Open in Sharepoint" when looking at the file preview in Salesforce, and ExternalDocumentInfo2 is the file identifier on the SharePoint server in the form of

item:<Root Path in Base64 encoding>:<List View ID>:<List Item ID>

Hope that helps.
 
This was selected as the best answer
Deven Dhaka 8Deven Dhaka 8
Hey Fabio,

Looks like you were able to find some workaround for linking files back to records.

For my scenario, I want to link the Sharepoint file as an attachment on the record, not only the URL. Any thoughts on this?
 
Fabio Andree Scheurel MWFabio Andree Scheurel MW
Dear Deven,

I am not sure whether attachments allow integration with SharePoint at all, I think it is limited to Salesforce Files. I would consider switching from attachments to files, where my solution mentioned above works.

Best Regards
Fabio
Deven Dhaka 8Deven Dhaka 8
Lets say if we have to link a Salesforce File to a specific record, how would you do that?

Thisi is my usecase - Currently not all users have to access to Sharepoint, therefore I want to automate file linking with records, so that users can download these files directly from Salesforce. I have enabled Files Sharing as "Copy" on File connect settings.
Fabio Andree Scheurel MWFabio Andree Scheurel MW
I would manually create a new linkage and look at the created ContentVersion document, to see how it looks like with your "Copy" sharing setting enabled. Then try to insert such a document programmatically.
Salesforce Dev 20393Salesforce Dev 20393
Fabio, Waht would the best way to move existing attachments to Sharepoint/Onedrive and link it back with the Salesforce record
Fabio Andree Scheurel MWFabio Andree Scheurel MW
That’s really difficult as there is not documentation available. Either go for trial and error and look into the data that has been created when doing everything manually, or ask Salesforce if you have a Premier Success Plan.
Bushra Salman 6Bushra Salman 6
Hi Fabio, Can you please explain how did you acieved it after File connect being done. Can you explain step by step. I have similar scenario where I want my sharepoint documents automaticaly attached to Files related list of account. How that would be achieved? Please help