4

I need to set the value of my people/group field with current user on page load. I tried many different things, but nothing worked. Let me know how to do this in SharePoint 2010.

1

Add a CQWP in the NewForm.aspx and put the below code into it.

<script type="text/javascript">

    ExecuteOrDelayUntilScriptLoaded(getWebUserData, "sp.js");
    var context = null;
    var web = null;
    var currentUser = null;

    function getWebUserData() {
        context = new SP.ClientContext.get_current();
        web = context.get_web();
        currentUser = web.get_currentUser();
        currentUser.retrieve();
        context.load(web);
        context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod), 
             Function.createDelegate(this, this.onFailureMethod));
    }

    function onSuccessMethod(sender, args) {
        var userObject = web.get_currentUser();
    SetAndResolvePeoplePicker("Members",userObject.get_loginName());

    }

    function onFailureMethod(sender, args) {
        alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
    }

    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);
    }

</script>

I haven't tested the above code because now I dont have an environment to test it, please test it and let us know.

Where, filedName -> is the control name, here in the example I have passed "Members".

Source

  • This did not work. – Firuzeh Faye Fouladi Dec 5 '13 at 1:48
  • What error message did you get.? – Karthik Jaganathan Dec 5 '13 at 5:21
  • No errors, just did not populate the field. I am wondering if anyone has got this to work? – Firuzeh Faye Fouladi Dec 6 '13 at 17:09
  • @KarthikJaganathan, the error that I'm getting is: Error: TypeError: Cannot read property 'id' of undefined when I try to set a group in the PeoplePicker control. I'll create a question for expand if is needed. I probed the sample code with a User and it worked. – Mauricio Arias Olave Mar 14 '16 at 22:55
0

This should work. Make sure you reference JQuery library and the jquery.SPServices library.

$(document).ready(function(){
var userName = $().SPServices.SPGetCurrentUser({ 
fieldName: "Name" 
});

var userNameField = $('input[title="YOUR COLUMN NAME"]').val();
if(userNameField == "")
{
$('input[title="YOUR COLUMN NAME"]').val(userName);
}
});
0

Hi use the code i am posting, it works for me. I am using SPServices to get the current user name, Please download the SPServices Library from codeplex and refer it propely in your code and then use this code. Put your code in newform.aspx

$(document).ready(function(){
var userName = $().SPServices.SPGetCurrentUser({            
         fieldName: "Title"
         });
$("div[title='PeoplePickerColumnName']").val(userName);
});

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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