Import DMF Projects using flow (Power Automate)

While implementation there is a need to import DMF projects frequently, doing it manually is a huge effort and to automate this task I have created an OData action and Flow. I will explain the implementation of this in this blog.

First we need to create an excel file with following information:
1) Title – DMF Project Name (Definition Group)
2) D365 Entity Name
3) Excel File – File name to read from SharePoint

We will then create an OData Action with this code below:

    [SysODataActionAttribute('createDMFProject', false)]
    public static void createDMFProject(str _defintionGroupName, str _entityName, str _fileName, str   _fileBase64)
    {
        str fileExtWithoutDot;
        str contentType;
        str fileId;
        guid fileGuid = newGuid();
        DMFDefinitionGroup  definitionGroup;
        DMFDefinitionGroupEntity    definitionGroupEntityTable;
        
        //Creates DMF project
        ttsbegin;
        definitionGroup.initValue();
        definitionGroup.DefinitionGroupName = _defintionGroupName;
        definitionGroup.OperationType = DMFOperationType::Import;
        definitionGroup.insert();
        ttscommit;
        
        fileExtWithoutDot = DMFDataSource::getFormatFileExtension('EXCEL');
        contentType     = strFmt('application/%1', fileExtWithoutDot);

        fileId = guid2str(fileGuid);
       
        //Creates a file from Base64
        System.Byte[] reportBytes = System.Convert::FromBase64String(_fileBase64);
        System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(reportBytes);

        FileUploadTemporaryStorageStrategy fileUploadStrategy = new FileUploadTemporaryStorageStrategy();
        FileUploadTemporaryStorageResult fileUploadResult = fileUploadStrategy.uploadFile(memoryStream, _fileName, contentType , '.xlsx');

        //Adds the entity and file to the DMF Project
        ttsbegin;
        definitionGroupEntityTable.initValue();
        definitionGroupEntityTable.DefinitionGroup = _defintionGroupName;
        definitionGroupEntityTable.Entity = _entityName;
        definitionGroupEntityTable.Source = 'EXCEL';
        definitionGroupEntityTable.SampleFilePath = fileUploadResult.getFileId();
        definitionGroupEntityTable.ExcelLookUp = 
        DMFDefinitionGroupEntity::insertExcelLookUpValue(_defintionGroupName,                                                                                                  _entityName,
                                                                                                  definitionGroupEntityTable.SampleFilePath,
                                                                                                definitionGroupEntityTable.Source);
        definitionGroupEntityTable.RunInsertLogic = NoYes::Yes;
        definitionGroupEntityTable.RunValidateWrite = NoYes::Yes;

        definitionGroupEntityTable.insert();
        ttscommit;

        DMFXmlGeneration::generateMappingV2(definitionGroupEntityTable, '', true, true, false);
    }
}

Last step is to create a flow: This will read our excel file based on that it will create a project and then use the file name from our excel file find it in SharePoint folder and attach it to the project.

Cheers,
Sami Noor Khan

Leave a comment