Skip to the content

Patch person/group field with multiple selection - Part 1

I have recently worked on a PowerApps customized form where I had to build a relationship between different fields and patch lookup and person/group filed with multiple selection.

As a data source I used four SharePoint lists.

  1. Docs Approval – List that contains the data entered by the user of the App
  2. Docs – List of all available documents
  3. Initiators – List of initiators and documents that a specific initiator can initiate
  4. Approvers – List of approvers and documents that a specific approver can approve

Docs Approval (SharePoint List)

Column

Type

Required

Get information from:

In this column:

Allow multiple selection

Title

Single line of text

No

 

 

No

Initiator

Person or Group

Yes

 

 

No

Document

Lookup

Yes

Docs

Title

No

Approvers

Person or Group

Yes

 

 

Yes

Docs (SharePoint List)

Column

Type

Required

Title

Single line of text

Yes

Initiators (SharePoint List)

Column

Type

Required

Get information from:

In this column:

Allow multiple selection

Initiator

Person or Group

Yes

 

 

No

Document Can Initiate

Lookup

Yes

Docs

Title

Yes

Approvers (SharePoint List)

Column

Type

Required

Get information from:

In this column:

Allow multiple selection

Initiator

Person or Group

Yes

 

 

No

Document Can Approve

Lookup

Yes

Docs

Title

Yes


Add the following Data Sources

App Onstart

Populate the collection "CollInitiators" with the data from the Initiators connection and add the columns, DisplayName, Email, DocumentCanInitiate

ClearCollect(CollInitiators,
    AddColumns( [@'Initiators'],
        "DisplayName", Initiator.DisplayName,
        "Email", Initiator.Email,
        "DocumentCanInitiate", 'Document Can Initiate'.Value
        )
);

// Populate CollApprovers with the data from the Approvers_1 datasource
ClearCollect(CollApprovers, Approvers_1);

Unlock the cards Initiator, Document and Approvers

Initiator_DataCard1

Items for DataCardValueInitiator

Set the Items/Data source to the collection Collinitiators (this collection is populated in the App OnStart) and set the Primary Text to DisplayName, Secondary text to Email and SearchField to DisplayName

OnSelect for DataCardValueInitiator

When an initiator is selected we set the variable "varInitiatorSelected" to true

Set(varInitiatorSelected, true)



OnChange for DataCardValueInitiator

If the form mode is new and if an initiator has been selected we reset the Document and the Approvers fields.

If(SharePointForm1.Mode = FormMode.New && varInitiatorSelected = true,
    Reset(DataCardValueDocument);
    Reset(DataCardValueApprovers);
)

Document_DataCard1

Items for Document_DataCard1

By using the LookUp command get all the documents that the selected initiator can initiate

LookUp(CollInitiators, Initiator.DisplayName = DataCardValueInitiator.Selected.DisplayName).DocumentCanInitiate

Ensure that you select ‘Document Can Initiate’ which returns a table with Id and Value. Id will be used for the patching when saving a record.

If you select ‘DocumentCanInitate’ it will return only the value and not the Id as you can see below

OnSelect for Document_DataCard1

Set the variable varDocumentSelected to true. This variable is used to reset the Approvers field.

Set(varDocumentSelected, true)

OnChange for Document_DataCard1

//Clear the Selected Approvers Collection
Clear(CollSelectedApprovers);

//Loop through all the Approvers. If the Document selected is in the Document Can approve 
//add the Approver's DisplayName and Email to the Approvers collection

ForAll(Approvers_1,
If(DataCardValueDocument.Selected.Value in Document_x0020_Can_x0020_Approve.Value,
Collect(CollSelectedApprovers, {DisplayName:Approver.DisplayName, Email:Approver.Email})));

//Reset Approvers Field
If(SharePointForm1.Mode = FormMode.New && varDocumentSelected = true,
Reset(DataCardValueApprovers);
)

Approvers_DataCard1

Items for DataCardValuesApprovers

Set the data source for DataCardValuesApprovers to the collection "CollSelectedApprovers" and set the Primary Text to DisplayName, Secondary text to Email and SearchField to DisplayName

CollSelectedApprovers

SharePointIntegration

OnSave for SharePointIntegration

// Add all selected approvers to the collection "CollSelectedApprovers"
// The Combobox has the following fields Primary text: DisplayName, Secondary text:Email, SearchField: DisplayName

ForAll(
DataCardValueApprovers.SelectedItems,
Collect(
CollSelectedApprovers,
{
Email: Email,
Claims: "i:0#.f|membership|" & Lower(Email),
Department: "",
JobTitle: "",
Picture:"",
DisplayName:DisplayName
}
)
);


// Loop through all the selected approvers to create the JSON that you need 
//to patch multiple people. Add the JSON to the collection “CollectedApprovers_Table” 

ForAll(
    CollSelectedApprovers,
    Collect(
        CollSelectedApprovers_Table,
        {
        '@odata.type': "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser",
        Email: Email,
        Claims: "i:0#.f|membership|" & Lower(Email),
        Department: "",
        JobTitle: "",
        Picture:"",
        DisplayName:DisplayName
        }
    )
);

//Insert the new record  into the ‘Docs Approvals’ table using the patch function, for the Approvers
// field use the collection ‘CollSelectedApprovers_Table,’ created above

 Patch('Docs Approvals', Defaults('Docs Approvals'),
                    {
                    Title: DataCardValue1.Text,
                    Initiator: {'@odata.type':
                                    "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser",
                                    Claims: "i:0#.f|membership|" & DataCardValueInitiator.Selected.Email,
                                    Department: "",
                                    DisplayName: DataCardValueInitiator.Selected.DisplayName,
                                    Email: DataCardValueInitiator.Selected.Email,
                                    JobTitle: "",
                                    Picture: ""
                                }, 
                    Approvers: CollSelectedApprovers_Table,
                    Document: DataCardValueDocument.Selected                
                    }
    );

In Part 2 I will cover:

  • Editing and updating existing items
  • Validation
  • Attachments

About the author

Giorgio Capisani

Giorgio Capisani

I am an experienced IT professional specialising in Microsoft technologies (SharePoint 2010/2013/Online, Power Platform, Office 365), with over 10 years of experience in large public and private sector organisations. 

comments powered by Disqus

Tech

Visit my blog

So you landed on my website … Why not have a look at my blog and leave a comment or ask a question.