0

I am trying to update a UserMulti field using CSR. However, no matter what value I send to SharePoint, the field remains empty.

This is my field definition:

<Field ID="{5EE8AF75-009D-4FBB-8AC9-D8D243170134}" Name="OnboardingStepResponsibleGroups" DisplayName="$Resources:Column_OnboardingSteps_OnboardingStepResponsibleGroups;" Type="UserMulti" UserSelectionMode="1" UserSelectionScope="0" Mult="TRUE" Group="Boardings Fields" />

I then register the field in my CSR file like so:

protected getTemplates(): SPClientTemplates.TemplateOverrides {
    var templates: SPClientTemplates.TemplateOverrides = {};

    templates.Fields = {
        "OnboardingStepResponsibleGroups": {
            NewForm: $.proxy(this.onboardingStepResponsibleGroupsNew, this)
        }
    };

    return templates;
}

private onboardingStepResponsibleGroupsNew(ctx: SPClientTemplates.RenderContext_FieldInForm): string 
{
    var formCtx = SPClientTemplates.Utility.GetFormContextForCurrentField(ctx);
    var field = new BoardingStepResponsibleGroupsNew("boardingreposiblegroup_new");

    formCtx.registerInitCallback(formCtx.fieldName, $.proxy(field.initCallback, field));
    formCtx.registerGetValueCallback(formCtx.fieldName, $.proxy(field.getValueCallback, field));

    return field.render();
}

This is my callback function:

public getValueCallback(): any {
    var values: SP.FieldUserValue[] = [];

    this.groups.forEach((group) => {
        if (group.Selected) {
            var value: SP.FieldUserValue = new SP.FieldUserValue();
            value.set_lookupId(group.ID);
            values.push(value);

        }
    });

    return values;
}

I have tried returning everything from an array of anonymous objects to an array of SP.FieldUserValue. However, no matter what I do, the field is always empty. Also, when I use

var spReponsibleGroups: string = ctx.CurrentItem["SomeMultiUserField"];

I get different values for display and edit forms. When inside display forms, the value is a string and looks like 1;#blah blah;#2;#ble ble but inside the edit form, the value looks like this:

[{
    Description: "Boarding Administratoren",
    DisplayText: "Boarding Administratoren",
    EntityGroupName: "",
    EntityType: "",
    HierarchyIdentifier: null,
    IsResolved: true,
     Key: "Boarding Administratoren",
    MultipleMatches: [],
    ProviderDisplayName: "",
    ProviderName: ""
},
{
    Description: "Boarding HR-PUL-Technik",
    DisplayText: "Boarding HR-PUL-Technik",
    EntityGroupName: "",
    EntityType: "",
    HierarchyIdentifier: null,
    IsResolved: true,
    Key: "Boarding HR-PUL-Technik",
    MultipleMatches: [],
    ProviderDisplayName: "",
    ProviderName: ""
}]

Official SharePoint documentation is lacking, as usual and I haven't had luck finding anything that's specifically about UserMulti fields targeting groups. Can anybody please help me out as I am all out of ideas?

4
  • You've tagged your question with sharepoint-online, but also with client-side-rendering. I was under the impression that CSR was unavailable in SPOnline, unless maybe it's available in classic experience? Are you using classic experience? If you go to a new/edit page that has a non-customized people picker control, is SPClientPeoplePicker available in the global namespace? Can you tell if the clientpeoplepicker.js script was loaded by SharePoint? Commented May 6, 2021 at 20:51
  • Hmmm, CSR seems to work though, I already deployed several times and other controls work just fine. Regarding clientpeoplepicker.js, do I need to load this even though I am not using a people picker? I use a checkbox group for selecting the groups I want to permit the user to choose from as the people picker control has no OOTB means of restricting the results to multiple groups and nothing else. Commented May 7, 2021 at 14:36
  • Well, if CSR is working then, great! I was just under the impression that it was not available in SPO, but if it is, it is. Regarding use of clientpeoplepicker.js, I was mentioning that because the way you worded your question, I was under the impression you were having issues with the control on the client side. I'm not sure I'm clear on what exactly you are having a problem with, then. How are you trying to set the field value? A REST call? Commented May 7, 2021 at 18:30
  • Ok, sorry if my question was a bit unclear. I don't use REST calls, I use getValueCallback to supply the value to SharePoint. SharePoint calls this function when saving the form so you basically can "overwrite" the default value. My problem is how to actually format this value so SharePoint can process it. I do not know if I should return an array of SP.FieldUserValue or SP.FieldLookupValue or a ';#'-separated string of IDs and Names. I have tried returning everything but SharePoint apparently has a problem with the type of returned value because the field always remains empty after editing. Commented May 11, 2021 at 13:08

1 Answer 1

1

After clearing things up a bit with the comments....I don't have a proper answer for you.

I've never used getValueCallback, and honestly didn't even know it was a thing, so I can't tell you how to format the values so that SharePoint will accept them. I can maybe suggest an alternate way to approach the problem, though.

See, I didn't know about getValueCallback because I would never get in the way of SharePoint doing its own thing with a form that it generated (i.e. the default New or Edit forms). If I were doing a custom form, I'd probably be going fully custom, rendering all the controls myself, and handling all CRUD operations myself via REST.

But if I needed to somehow customize the input control used for a field in one of the SP generated forms, (especially if it was something as involved as a user field, which essentially has its own API just to interact with the control), here's how I would approach it:

  • wait for $(document).ready() or something similar, or if I was using CSR possibly do something onPostRender, essentially use whatever method made sense to wait until the page is loaded and rendered,
  • hide the default SharePoint generated control,
  • render my own custom control,
  • use whatever event handling makes sense for my custom control to detect changes, and then use that to set the value of the hidden default control.

Then when the form is submitted SharePoint will collect the value from the default control. It doesn't care if it's hidden or visible. And for a user field, if you use the SPClientPeoplePicker API to set the value, it will be set in whatever way is going to satisfy SharePoint to accept the value when the form is submitted.

I know that doesn't directly answer your question, but that's how I would approach the situation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.