Description

When a new customer is created in CRM this flow will automatically create the document folder structure on SharePoint AND create a relationship to the document location.

Sub folders in CRM was tricky to understand but I finally got the hang of it. In this example I want a structure created like this for each customer:

/accounts/Country/State/City

Example

  • Account Name: Batcave Skatepark
  • Country: United States
  • State: MO
  • City: Joplin

The above customer would create the following structure in a SharePoint document library:

/accounts/United States/MO/Joplin/Batcave Skatepark

Open CRM and go to Settings>Document Management. It is missing in my picture because I already set it up but you should have an option that says enable SharePoint Integration (going off memory) For the website I will be using this in my example (The web address does NOT contain the document library name):

https://companyname.sharepoint.com/sales

When you finish the wizard several document libraries will be created like so:

In my example I will be using customer (aka: account)

From the Document Management Setting page click SharePoint Document Locations

Open another tab and go to your target Document Library (https://companyname.sharepoint.com/sales/account)

Notice in the address bar the first “subfolder” we need to mention in CRM is account and then I need to create a document link for United States that has a parent of account

From SharePoint Document Locations click New and fill in the following:

  • Name: Account
  • Parent Site or Location: Customers (May be called Default Site 1 for you)
  • Relative URL: account

Once created I clicked on the Parent Site or Location to rename that parent link to something meaning full to me like customers

So next I need a United States link in CRM but the parent to that is account because in the link below it is above it:

/accounts/United States/MO/Joplin/Batcave Skatepark

Then I create a MO Document location with United States as the parent

/accounts/United States/MO/Joplin/Batcave Skatepark

Next I create Joplin Document location with MO as the parent

/accounts/United States/MO/Joplin/Batcave Skatepark

The flow will create the rest for us and I will explain how the flow will auto create/link Country, State, City, and Account Name folders

I will chunk this into 3 sections:

  • Condition to Check Region
  • Condition to Check State
  • Condition to Check City

 

Condition Check Region

  • Trigger: When row is added, modified or deleted
    • Change Type: Added
    • Table name: Customers (Account)
    • Scope: Business Unit
  • Initialize Variable
    • Name: Region Location
    • Type: String
  • Initialize Variable
    • Name: ParentofState
    • Type: String
  • Initialize Variable
    • Name: StateDocumentLocation
    • Type: String
  • Condition – if Address1:country contains USA (Address1:country is the field on the CRM customer form)
    • True (yes)
      • Set Variable
        • Name: RegionLocation
        • Value: United States
      • Set Variable
        • Name: ParentofState
        • Value: GUID of United States Document Location
    • False (no)
      • Set Variable
        • Name: RegionLocation
        • Value: International
      • Set Variable
        • Name: ParentofState
        • Value: GUID of International Location

The GUID for your parent link can be found in the address bar after the id= at the end of the address:

https://companyname.crm.dynamics.com/main.aspx?appid=178997ec4a-4e18-ec11-73DF-0022473728dd&forceUCI=1&newWindow=true&pagetype=entityrecord&etn=sharepointdocumentlocation&id=e5660696-602a-ec11-b6e6-000000000000

 

Condition Check State

  • Initialize variable
    • Name: FolderLocation
    • Type: String
    • Value: [RegionLocation]/[Address 1:State/Address 1: City/AccountName (Dynamic variables of the field location is CRM)
  • Create new folder
    • Site Address: Sales – https://companyname.sharepoint.com/sales
    • List or Library: Customer (account)
    • Folder Path: FolderLocation (Variable from above)
  • List rows
    • Table name: Document Locations
    • Filter rows: relativeurl eq ‘Address1:State’ (Address1: State is a field on the CRM customer)
  • Condition – If length(outputs(‘List_State_rows’)?[‘body/value’]) is greater than 0
    • True (yes)
      • Set variable
        • Name: StateDocumentLocation
        • Value: first(outputs(‘List_State_rows’)?[‘body/value’])?[‘sharepointdocumentlocationid’]
    • False (no)
      • Create row
        • Table name: DocumentLocations
        • Name: Address 1: State
        • Service Type: SharePoint
        • Parent Site or Location (Document Locations): sharepointdocumentlocations(ParentofState)
        • Relative URL: Address 1: State
      • Set variable
        • Name: SateDocumentLocation
        • Value: SharePoint Document Location ID (Select the dynamic content for create state row above for this ID)

Condition Check City

  • List rows
    • Table name: Document Locations
    • Filter rows: relativeurl eq ‘Address 1: City’
  • Initialize variable
    • Name: CityDocumentLocation
    • Type: String
  • Condition – If length(outputs(‘List_City_rows’)?[‘body/value’]) is greater than 0
    • True (yes)
      • Set variable
        • Name: CityDocumentLocation
        • Value: first(outputs(‘List_City_rows’)?[‘body/value’])?[‘sharepointdocumentlocationid’]
    • False (no)
      • Create row
        • Table name: Document Locations
        • Name: Address 1: City
        • Service Type: SharePoint
        • Parent Site or Location (Document Locations): sharepointdocumentlocations(StateDocumentLocation)
        • Relative URL: Address 1: City
      • Set variable
        • Name: CityDocumentLocation
        • Value: SharePoint Document Location ID (Select the dynamic content for create city row above for this ID)
  • Add row
    • Table name: Document Locations
    • Name: Document for [Account Name] (Select dynamic content for Account Name)
    • Service Type: SharePoint
    • Parent Site or Location (Document Locations): sharepointdocumentlocations(CityDocumentLocation)
    • Regarding (Customers): /accounts[AccountID]
    • Relative URL: [Account Name](Select dynamic content for Account Name)

Now when a customer is created the folder structure will be created in SharePoint and the CRM customer record will auto link to that location in SharePoint for related documents. Hope it works for you and let me know if you have any questions.

References:

https://powerobjects.com/search/CRM+integration+/
https://linnzawwin.blogspot.com/2020/04/upload-sharepoint-documents-for.html
https://linnzawwin.blogspot.com/2021/08/automatically-create-sharepoint.html?showComment=1633711732195#c2732768867264008825
https://community.dynamics.com/crm/f/microsoft-dynamics-crm-forum/395069/how-to-find-the-parent-site-or-location-id-for-the-document-location-for-dynamic-365-and-sharepoint-integration
https://powerusers.microsoft.com/t5/Building-Flows/Document-location-with-sub-folders-in-CRM/td-p/1300189
https://docs.microsoft.com/en-us/power-platform/admin/create-edit-document-location-records
https://www.aerieconsulting.com/blog/how-to-automatically-create-document-location-for-records-in-dataverse
https://www.itaintboring.com/dynamics-crm/creating-custom-folder-structure-for-sharepoint-integration-using-flows/
https://berniethibeault.com/2021/02/02/how-to-automatically-create-document-location-for-records-in-dataverse/
https://ryanmaclean365.com/2019/11/06/display-sharepoint-documents-related-to-parent-on-child-record-in-dynamics-365/