I had run into a requirement where user wanted to upload a document in CRM but implement a naming format for the file uploaded. Things got little tricky as one Account can have multiple files based on a it’s relationship with a particular custom entity and the documents should be uploaded and available on Account folder but the child entity should reference a particular file uploaded.
To accomplish this requirement we implemented Canvas Apps and embedded it into CRM using ModelDrivenFormIntegration. User was shown a list of records from Custom entity with an upload button that would upload the document to SharePoint based on the document location configured for that Account in CRM as well as create a reference to the file on that Custom entity record.
Showing the data in Canvas App was pretty simple but complexity arise when I identified that document upload is only available using SharePoint List but I wanted to upload the document to a Location type in SharePoint.
So, first I created a temporary SharePoint List where it would save the Account Id, Custom Entity Id and Path where the file should be uploaded. Then on the Form control in Canvas App these values were getting appropriately set. Upload button click created the List record in SharePoint.
Once the list got created a Flow would get triggered that would retrieve the attachments and upload it to appropriate Document Location.
Below is the entire Flow.
In Detail Flow Explanation
When an item is created in SharePoint list this Flow will be triggered. It takes in two required parameters; Site Address – which is the SharePoint site address configured with CRM, List Name – the temporary list created that is bound with the Canvas App.
Get attachments from List Item
This action gets all the attachment data for a particular list item. In this case it would be the created list item. Point to note here is that it does not give you the attachment contents.
Get attachment contents
The above action gives you a list of attachment data and hence to fetch the attachment contents we need to loop through each of the attachments. The loop element here would be the “body” of action prior to this.
Create file in appropriate location
Once we have the contents now we create the file in proper location. The location data is saved on the List which makes it easy.
Delete the List Item
As stated earlier the List is suppose to be a temporary holding place; so once the attachment is copied over to proper location I am deleting the List Item that was created by the Canvas App (to keep it clean)
Setting the reference on the Custom entity was handled inside Canvas App and as the reference was set the App looked like the one shown below.