PowerApps Set SharePoint Person field to Current User

It seems like such a typical request.  And yet, so many hoops to jump through.

I’ll note at the onset that this post is based on PowerApps 2.0.650

My scenario is familiar.  I have a SharePoint record, and I am using a PowerApps screen as an Approval type form.

In my SharePoint item I have fields for Approved (Date/Time) and ApprovedBy (Person or Group).

So when I save my form, I’d like these fields to be populated with the current time and current user.

PowerApps basically provides three avenues for updating a record: SubmitForm(), Update(), and Patch()

SubmitForm is the simplest and uses databound fields.

Update() allows you to select and update a record (or records) with any values you want, either from your form or otherwise.  But, if you don’t provide a value for a column, it will be blanked out.  Not the greatest.

Patch() does a bit better than Update in that it will allow you to selectively update a single column and leaves all other values intact.  However, you have to map every column you want to update, so if you have a record with lots of fields, you would have to set values for them all in your Patch() statement.  Annoying.

So what I really wanted to accomplish was to set default values for my databound controls that may to the Approved and ApprovedBy fields, then use SubmitForm() to sweep up all the values and update the record.

Here’s the approach that worked, and properly passes the Current User through to the Person and Group field:

  1. Set the OnVisible property of my Screen to set a Context Variable with a representation of the current User.
  2. Add a DataCard for the Person field to my EditForm.
  3. Set the Update property of the DataCard to the name of the Context Variable.  This becomes the value passed by the SubmitForm() method to the column in my datasource.
  4. Set the OnSubmit() property of my Save button to SubmitForm(FormName)
  5. Set the Visible property on the DataCard to False so it is not shown on the form.

Let’s break this down first by looking at how the Person record is represented.  PowerApps uses JSON style notation for objects.  In this case, there is a special object for a SharePoint User object, as seen below:

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

A few things to note:

  1. If you’ve used SharePoint on-premise, then the formatting of the claims username may appear odd, but it works for Office365.  If you are connecting to an On-Premise SharePoint data source using a Gateway, then this format may need to change for Windows Accounts.
  2. From what I could tell, the Email property is the most important – even if the other properties (Department, DisplayName, JobTitle, Picture) are not populated, SharePoint will identify the correct user.
  3. Be sure to use the SPListExpandedUser data type.  There is also SPListExpandedReference (used for Choice fields) but it will fail when using a Person field.
  4. You’ll get an error saying ‘A value must be provided for item’ if anything is incorrect.

So by setting a ContextVariable when the screen loads, I can then pass that object as the properly formatted value during the SubmitForm() call.

Once the variable is set, we then use it on the data card as the Update value

Things like this definitely make PowerApps a tool that requires some technical skills to achieve various business requirements.  But as the product matures, I am hopeful that shortcuts will be implemented to make these common scenarios less cumbersome to address.


Posted

in

, ,

by

Tags:

Comments

3 responses to “PowerApps Set SharePoint Person field to Current User”

  1. Logan Francis Avatar
    Logan Francis

    Great response. Any suggestions on how to patch a selected user rather than the current.

  2. madi Avatar

    Hei Smith . I have tried by seeing ur post but it patched if i use User().Email etc but when i try to patch what user enters in person field it didnt update or sve . Please reply . Struggling hard with this issue . Here is my code

    {
    Title:”SinglePerson”,
    Behandler:{
    ‘@odata.type’:”#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser”,
    Claims:”i:0#.f|membership|” & Lower(DataCardValue33.Selected.Email),
    Department:””, DisplayName:DataCardValue33.Selected.DisplayName,
    Email:DataCardValue33.Selected.Email,
    JobTitle:”.”,
    Picture:”.”
    }

  3. Daniel Kaschel Avatar

    Note that this process is no longer necessary. Check out a somewhat simpler solution available now:

    https://lhdinger.wordpress.com/2018/02/22/powerapps-people-picker-default-value/#comment-1901

Leave a Reply