Synchronise SharePoint folder name with row name in Dataverse or Dynamics 365
In a previous post I showed how you can use a custom page to improve the user experience significantly when using the dataverse model-driven or customer engagement (Dynamics 365) app to SharePoint ‘integration’. This is something which I truly expected the integration to do out of the box, but it doesn’t. It doesn’t cause a problem at the time but later down the track it has bitten me and many other too I’m sure! SO here goes another ‘Make the SharePoint to Dynamics 365 integration a bit more bearable’ post.
An Account record is created with the name Amey Holden Pty Ltd - the SharePoint Folder and Document Location record are created also called ‘Amey Holden Pty Ltd’ - all good (for now)
Three years later the company changes it’s name to Spadey Mc Spade Face Pty Ltd - the user updates the Account name in Dataverse, but the SharePoint foler and Document Location are still called ‘Amey Holden Pty Ltd’. Dataverse is confused, the user is confused, and I am severely triggered by this data disarray. With a little bit of Power Automate magic we can automatically synchronise any record name changes with the SharePoint folder and Document Location, because that’s what it SHOULD do!
Synchronise row name updates to SharePoint folder names
How to make the magic happen? First I need you to pretend that every time you see the word ‘Vendor’ it means ‘Account’.
Note: When it comes to anything SharePoint related with Power Automate I use an environment variable for the SharePoint Site address so its a clean move to another environment and therefore a different SharePoint site.
Did you find the Document Location?
We need to check if the action above (GetDocLocForVendor) has found anything. If the Document Location was not found - you have got bigger problems, do not pass go, go create it - this blog might help you.
empty(body('GetDocLocForVendor')?['value'])
replace(replace(replace(replace(replace(concat(triggerOutputs()?['body/name'],' - ',triggerOutputs()?['body/tc_id']),':',''),'''',''),'/',''),'&',''),'.','')
Get the SharePoint folder
So we are going to use the ‘Send a HTTP Request to SharePoint’ action which scares me silly, but trust me it doesn’t bite.
Site Address: e.g. https://amey.sharepoint.com/sites/D365
Method: GET
URI: _api/web/GetFolderByServerRelativeUrl('/sites/D365/account/Amey Holden Pty Ltd')/ListItemAllFields
Headers:
Accept - application/json;odata=verbose
Content-Type - application/json;odata=verbose
In the URI we need to dynamically set Amey Holden Pty Ltd and D365 as these will change per record/environment
D365 - this is the text after the last ‘/’ of your SharePoint URL
last(split(parameters('SharepointSiteAddress (tc_SharepointSiteAddress)'), '/'))
Amey Holden Pty Ltd - this is the relative URL which you can get from the GetDocLocForVendor action earlier (but using first() so you don’t get stuck in a loopy loop)
first(outputs('GetDocLocForVendor')?['body/value'])?['relativeurl']
Prepare some handy items for later
You could set these up directly in the next HTTP request action but it can be a bit temperamental and helps with tracing any failures in the flows later. Parallel action here is optional, it probably saves 0.00004 milliseconds but it looks pretty smart too.
ETag: outputs('GetSharePointFolder')?['body/d/__metadata/etag']
Type: outputs('GetSharePointFolder')?['body/d/__metadata/type']
Update the SharePoint Folder Name
Another HTTP Request, the areas highlighted below are reused from above (Site Address, URI and the First two Headers)
Site Address: same as previous HTTP Request action
Method: POST
URI: same as previous HTTP Request action
Headers: same as previous HTTP Request action +
If-Match - ETag
X-HTTP-Method - MERGE
Body:{"__metadata": {"type": "TYPE"},"FileLeafRef": "NEWFOLDERNAME"}
NEWFOLDERNAME in this case would be the accounts new name - ‘Spadey Mc Spade Face Pty Ltd’
And the data dilemma crisis was averted for another day
Credits to Pieter Veenstra for this blog on how to rename SharePoint folders, it helped me a lot to make this magic happen! Comments, questions or anything else always welcome, more rants about tidying up the SharePoint to Dynamics 365 integration coming soon…