Saturday, June 13, 2020

Integrate Microsoft Flows with Power Apps

In this article we would go through the steps of Creating a Flow and integrating it with a PowerApps Form.

Though Power Apps doesn't has ability to call any web services / rest apis, it provided easy way to integrate with Flows, thus if we have any complex logic to be executed in the form, we can call a Flow with configured input parameters and the Flow executes the logic and provides response output parameters. 

Creation of  PowerApps Supported Flow
  • Create a Flow of type "Instant - from Blank" and in the trigger, select Power Apps,
     
  • Now, for any input parameter that required from PowerApps, click on any action and from Add Dynamic content window, select Ask in PowerApps from Manual section.
     
    We refer to as many input parameters we want and all would be in string.
  •  Build your logic using input values 
  • Finally, to respond back with output values, use Respond to a PowerApp or Flow action. This action would provide an ability to add any number of Output values of different types. 

  • Save the flow.
Creation of PowerApp and integrating the above Flow
  • Create an App or through any Sharepoint List, click on customize forms. I used Sharepoint list here
  • Select the Screen Control and go to On Visible property, in the function pane, click on Action tab and choose Power Automate. A subscreen pops up with list of available Flows. Click on the Flow we want to call.
     
  • Once we click on it, it adds a Method to call the Flow and it highlights shows the input parameters to fill in.
     
  • Here i set the output of Flow response to a variable and referred it in couple of label texts

  • Save the PowerApp and publish it. Once we load the form, we can see the responses on screen

Few important observations are as below
  1. Boolean output is not coming up in the PowerApp. Better we use string type and compare it
  2. For date field, PowerApps is considering as a string.
  3. On a high note, though we have different data types for output, if we want to sue in powerApps, better we use string type and convert them

Thursday, June 4, 2020

Microsoft Flow - Setup Sharepoint Groups and Users

In this article we would go through Creation of a Sharepoint Group, providing access to it and then adding a set of users to it through the Microsoft Flow using Sharepoint Rest APIs.

Creation of a Group:
Rest API to create a group is 

Uri: /_api/Web/SiteGroups
Method: POST
Header: {content-type:'application/json; odata=verbose'}
Body:{
               "__metadata": {
                        "type": "SP.Group"
               },
               "Title": "Demo Group - Contribute",
               "Description": "Created through API in Flows."
           }

We refer the above API call in Flow action: Send an HTTP Request to Sharepoint as in  below


Set Permissions to Sharepoint Group:
Rest API to set Permissions for a group / user is 

Uri: /_api/web/roleassignments/addroleassignment(principalid=<<PrincipalID>>, roledefid=<<PermisisonRoleID>>)
Method: POST

Here, PrincipalID is the ID of group created in above action. we can fetch this in Flow as body('HTTP_-_Create_Group')?['d']?['Id']
And, RoleDefID is the ID of permission role we are looking to assign. In sharepoint, each permisison role has an internal ID. We can find them by accessing Rest API /_api/web/roledefinitions
Here, for Contribute, the RoleID is 1073741827


Add Users to a Sharepoint Group
Rest API to add users to a Sharepoint group is

Uri:/_api/web/sitegroups/GetById(<<GroupID>>)/users
Method: POST
Header: {content-type:'application/json; odata=verbose'}
Body: {
                  "__metadata": {
                          "type": "SP.User"
                  },
                  "LoginName": "<<LoginName of User>>"
            }

We already have GroupID from the first action and for user, we need Login Name of it. 



Once we execute it, it creates a group and adds user as in below


If we have multiple user accounts, we can add them to an array and use loop action for this API. Or, use BULK API to merge all calls except the first one as we need GroupID for other service calls. For Bulk API, I have provided demo in my previous post Set Permissions on a Sharepoint list Item

Wednesday, June 3, 2020

Microsoft Flows: Move Library documents

This article is to go through about Get Files (Properties Only), Move File and Move Folder Sharepoint actions in Microsoft Flows through below implementation.

We want to archive all the files / folders in one library to Central Archive (Separate Site Collection).

Source Library: A Document library with versioning enabled and also has some Meta data columns.

Destination Library: 
  • In Destination Library, we need to make sure the versioning is enabled (as we have it in Source) with higher or same limits as in Source Library. 
  • It should also include all the meta data columns as in Source Library as Move Actions would fail if schema doesn't match from Source
  • Few extra columns like Archived on and SourceLoc to capture archival info.

Below are the steps to achieve the functionality
  1. Use Get Files (Properties Only) action to fetch all the files and Folders in the given source library
    Note: We need to make Include Nested Items Property to "No" as we don't want the documents inside folders and when we move folders, it would go along with inside content.

  2. Apply to Each action to lop through each File / Folder fetched from above action
  3. Condition action to check if the current file is a File or Folder. We have IsFolder property to identify it.
  4. Based on above condition, 
    • If its folder, use Move Folder action to move complete folder along with content inside it to destination library
    • If Its File, use Move File action to move the corresponding file to destination.

      Note: Both above actions work same as the Move to commands we use in sharepoint
  5. Once the Move action is completed, in order to update the Archival meta data in destination, use Update File Properties  action.

    Here as we either moved File / Folder in above action, we use below if logic to fetch the correct ItemID to update.  
    If(empty(body('Move_file'))
            ,body('Move_folder')?['ItemId']
            ,body('Move_file')?['ItemId']
      )

Complete flow would appear as below,


When we run this flow, it moves all files from Main Library in Demo Site to Documents Library in ArchivalCenter.