4

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.

3 Answers 3

2

jQuery & SPServices (client side) are my preferred tools for this type of task:

    thisUserAccount = $().SPServices.SPGetCurrentUser({fieldName: "Name"});

    // Set the [PeopleColName] to be the Logged in user
    $().SPFindPeoplePicker({  
        peoplePickerDisplayName: "PeopleColName",  
        valueToSet: thisUserAccount 
    });
3
  • I think you're referring to the other SPPeoplePicker. This one is ClientPeoplePicker. After looking through the code, none of the elements being looked for in $().SPFindPeoplePicker exist in the code above.
    – EHorodyski
    Commented Dec 12, 2013 at 15:45
  • 1
    huh? SPPeoplePicker is a method in SPServices, not an element.
    – Paul Leigh
    Commented Dec 12, 2013 at 16:21
  • Sorry, must have used the wrong terminology. There are two people picker controls in SharePoint. One is called ClientPeoplePicker and I'm not sure what the other one is called. The other one has the check mark and directory images next to it. It renders and gets populated differently. That's the one SPFindPeoplePicker is looking for, not the one that FormField resolves into...as far as I can see looking through SPServices code.
    – EHorodyski
    Commented Dec 12, 2013 at 16:54
0

I got this to work on NewForm.aspx by running the ExecuteOrDelayUntilScriptLoaded function after cui.js load, not clientpeoplepicker.js

So replacing the clientpeoplepicker.js with cui.js should help.

EDIT: we're currently using this fragment - the bigger whole, of which this is a fragment, runs in an ExecuteOrDelayUntilScriptLoaded(..., "core.js") function

    var val = "string with user names;or logins";
    var elem = editForm.find("div.sp-peoplepicker-topLevel");       

    if (elem.length == 1) {
        function isIE() {
            var myNav = navigator.userAgent.toLowerCase();
            return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false;
        }
        function ppHandler() {
            var ppdiv = elem;
            var ppinst = SPClientPeoplePicker.SPClientPeoplePickerDict[ppdiv[0].id];
            var ppinput = ppdiv.find(".sp-peoplepicker-editorInput");
            var splitUsers = val.split(";");
            for (var i=0; i < splitUsers.length; i++) {
                ppinput.val(splitUsers[i]);
                ppinst.AddUnresolvedUserFromEditor(true);
            }
            ppdiv.find('.sp-peoplepicker-delImage').css('display', 'none');

        }
        if (isIE() > 9) {
            setTimeout(ppHandler, 2000);
        } else {
            $(window).load(ppHandler);
        }
        return;
    }
4
  • I don't get it to completely resolve the user. I see part of my name in there but it isn't resolved. Could you provide a code sample of JS you got to work in the NewForm.aspx page? Maybe where on the page you're calling it as well?
    – EHorodyski
    Commented Feb 25, 2014 at 20:54
  • works for me, thx.
    – Shawn
    Commented Nov 6, 2014 at 23:15
  • dont work for me. The same as @EHorodyski say Commented Jan 18, 2015 at 15:48
  • updated with a piece of code
    – kerray
    Commented Jan 19, 2015 at 13:32
0
function SetAndResolvePeoplePicker(fieldName, userAccountName) {

var controlName = fieldName;

var peoplePickerDiv = $("[id$='ClientPeoplePicker'][title='" + controlName + "']");

var peoplePickerEditor = peoplePickerDiv.find("[title='" + controlName + "']");

var spPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[peoplePickerDiv[0].id];

peoplePickerEditor.val(userAccountName);

spPeoplePicker.AddUnresolvedUserFromEditor(true);

//disable the field

spPeoplePicker.SetEnabledState(false);

//hide the delete/remove use image from the people picker

$('.sp-peoplepicker-delImage').css('display','none');

}

Click Here More Details

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.