All about lookup fields when using SharePoint lists - Part 3, Patch People fields with multiple users allowed

In Part 2, I went through some of the process of using the Patch function to write complex SharePoint columns type such as Lookup, Choice and People.  In this final part I'll build additional functionality to my demo app to allow me to add the current user into a People column that allows multiple people to be added.  The important requirement is to maintain any existing users in that field.

The Patch function has one major drawback in this scenario, which is that it has no concept of 'Append' when patching a field with multiple entries.  It will always erase the field entirely and then add the new item(s).  Let's go back to me Project app and add a new button, we'll label it 'I'm on the Team' and position in under the form to the right of the display, as shown here


The Team field is currently empty so I could Patch the current user into that field in the same way I did with the Project Manager field in my previous post.  However, as stated above, if there were already members of the team in that field then they would be removed.  I had wondered if the Collect function might be suitable and so tried an OnSelect function of

Collect (
    glrProjects.Selected.Team,
    {
        Claims     : "i:0#.f|membership|" & Lower(User().Email),
        Department : "",
        DisplayName: User().FullName,
        Email      : User().Email,
        JobTitle   : "",
        Picture    : ""
    }
)

Unfortunately this simple option doesn't work, even though there are no errors shown either in syntax or execution.  The only option we have left is to build a collection locally from the existing user list, add the current user and then Patch that back to the SharePoint list.  The OnSelect function to achieve this is

ClearCollect(varUserList,glrProjects.Selected.Team);
Collect (
    varUserList,
    {
        Claims     : "i:0#.f|membership|" & Lower(User().Email),
        Department : "",
        DisplayName: User().FullName,
        Email      : User().Email,
        JobTitle   : "",
        Picture    : ""
    }
);
Patch (
    PA_Projects,
    glrProjects.Selected,
    {
        Team : varUserList
    }
)

There are 3 components to this.  The first ClearCollect creates an empty collection called varUserList, and then adds the user items from the Team field of the currently selected project.  If we ran this line only, we could see the collection in the editor and would be able to see the columns that are produced; namely Claims, Department, DisplayName, Email, JobTitle and Picture.  When we add the current user, we will have to match these same columns, so the Collect statement that follows does exactly that by passing in the relevant output items from the User() function.  The Claims item is simply a string containing i:0#.f|membership|followed by the user email in lower case.  At this point we now have a list of suitably structured user items in our collection so we simply Patch that list back into the Team field of the selected project.

I have used similar code in creating company event apps, where we have an 'I'm Attending' button attached to the event display.

That's it for this post, please comment below if there are any additional issues with complex data fields in SharePoint lists or if there is anything else you would like me to write about.

Comments