Without using any addons, has anyone discovered a way to set a default value for a <SharePoint:FormField>
where the field is a Person/Group
? We'd like to default a field on our newform
to the current user and are finding that it's much more difficult than anticipated.
I believe once rendered the FormField
is actually resolved into a <SharePoint:ClientPeoplePicker>
. Due to inspecting the HTML rendered for both.
At the very least I thought I had the option to use <SharePoint:ClientPeoplePicker>
but it looks like you can't assign a field to that control. Furthermore, you can't cast a FormField
and I'm really looking to avoid having to use the CSOM people picker which I know how to populate but is several lines of code and a lot of effort for something I hoped would be so simple.
I've tried populating the item's field on several stages of my control's life-cycle, starting with OnInit
. It looks like it gets set and I can write the value onto the form but it won't resolve in the FormField
. Furthermore, if I set the value on OnInit
the value won't even save to the item after pressing the Save button.
I've tried doing an system update to the field on the OnInit
but afterwards I get an error if I try to save the form saying there was an invalid save (or something of the sort).
Edit: I've resorted to client-side and am very close. I'm using the code below:
ExecuteOrDelayUntilScriptLoaded(function () {
setTimeout(function () {
var ppDiv = $("[id$='ClientPeoplePicker'][title='Owner']");
var ppEditor = ppDiv.find("[title='Owner']");
var spPP = SPClientPeoplePicker.SPClientPeoplePickerDict[ppDiv[0].id];
ppEditor.val("domain\\user");
spPP.AddUnresolvedUserFromEditor(true);
}, 2000);
}, "clientpeoplepicker.js");
The above code works, but notice how I have to set a timeout. What happens is that SPClientPeoplePicker.SPClientPeoplePickerDict
is not initialized directly after clientpeoplepicker.js
is loaded. Now the question I'm looking for is how can I get an event kicked off once this array gets loaded? I don't want to use a timeout because if the time might vary due to client resources which at that point are out of my hand. There's also a race condition to take into consideration.