none
SharePoint person field - auto fill with current user? RRS feed

  • Question

  • Hi, I use SharePoint 2010.

    CanNOT use SharePoint Designer or Visual Studio.
    JavaScript is okay.

    I have a person field in my list, in new list form how to have it auto populated with current user's name?

    Thanks so much.

    Friday, December 21, 2012 11:17 PM

Answers

  • ran009

    Wish you could use SharePoint Designer because all you would have to do to get the current logged user is to use the "Created By" field in the list. This column automatically populates the individual who created the item.

    Try this

    <script type="text/javascript"> 
    $(document).ready(function() { 
    $('div.ms-inputuserfield').text($().SPServices.SPGetCurrentUser({fieldName: "Title",debug: false}));
    </script>

    Here is another way that may help you out as well

    http://sharepointjavascript.wordpress.com/2009/12/04/link-to-fill-user-login-name-in-people-picker/

    • Proposed as answer by AliYasir Saturday, December 22, 2012 10:47 AM
    • Marked as answer by ran009 Monday, June 15, 2015 6:31 PM
    Saturday, December 22, 2012 7:44 AM
  • Hi ran009,

    To add the following script, you will need to add a Content Editor Web Part to the New Form page. To do this, add the following to the end of the new form url:

    ?ToolPaneView=2

    You will then be able to add web parts to the page. Add the Content Editor Web Part to the page, then Edit Source and add the script. Set the web part to be hidden.

    The script code:

    <script type="text/javascript">
    _spBodyOnLoadFunctionNames.push(
    "fillDefaultValues");

    function
     fillDefaultValues() 
    {

    fillPeoplePickerWithCurrentUser('Submitted_x0020_By');

    }

    function
     fillPeoplePickerWithCurrentUser(pickerName)
    {

    //get the current user from the welcome menu
    var
     currentUser = getCurrentUser();

    //check to see that we've got it

    if
    (currentUser != null)
    {

    //get the people pickers input div
    var
     pp = getPickerInputElement(pickerName);
    //set it to the current user if we've found it

    if
    (pp != null)

    pp.innerHTML = currentUser;

    }

    }

    function
     getCurrentUser()
    {

    var tags = document.getElementsByTagName('a');

    for (var i=0; i < tags.length; i++)
    {

    if(tags[i].innerText.substr(0,7) == 'Welcome')
    {

    return tags[i].innerText.substr(8,tags[i].innerText.length);

    }

    }

    }

    function
     getPickerInputElement(fieldsInternalName)
    {

    var result  = "";
    var
     divs = document.getElementsByTagName("DIV");
    for
    (var i=0; i < divs.length ; i++)
    {

    if(divs[i].id=="WebPartWPQ2")
    {

    var tds = divs[i].getElementsByTagName("TD");
    for
    (var j=0; j < tds.length; j++)
    {

    var cellHTML = tds[j].innerHTML;

    if(cellHTML.indexOf('FieldInternalName="' + fieldsInternalName + '"') >= 0)
    {

    var innerDivs = tds[j].getElementsByTagName("DIV");
    for
    (var k=0; k < innerDivs .length; k++)
    {

    if(innerDivs[k].id.indexOf("UserField_upLevelDiv") > 0)
    {

    result = innerDivs[k];
    break
    ;

    }

    }

    }

    }

    }

    }
    return
     result;

    }
    </
    script>

    For more information, see

    http://blogs.vbcity.com/skullcrusher/Default.aspx

    http://social.technet.microsoft.com/Forums/en/sharepointadminlegacy/thread/389f66a1-d1c1-4630-b5a5-e8be0b970aa7

    Best Regards.


    Kelly Chen
    TechNet Community Support

    Monday, December 24, 2012 6:43 AM
    Moderator

All replies

  • ran009

    Wish you could use SharePoint Designer because all you would have to do to get the current logged user is to use the "Created By" field in the list. This column automatically populates the individual who created the item.

    Try this

    <script type="text/javascript"> 
    $(document).ready(function() { 
    $('div.ms-inputuserfield').text($().SPServices.SPGetCurrentUser({fieldName: "Title",debug: false}));
    </script>

    Here is another way that may help you out as well

    http://sharepointjavascript.wordpress.com/2009/12/04/link-to-fill-user-login-name-in-people-picker/

    • Proposed as answer by AliYasir Saturday, December 22, 2012 10:47 AM
    • Marked as answer by ran009 Monday, June 15, 2015 6:31 PM
    Saturday, December 22, 2012 7:44 AM
  • Hi ran009,

    To add the following script, you will need to add a Content Editor Web Part to the New Form page. To do this, add the following to the end of the new form url:

    ?ToolPaneView=2

    You will then be able to add web parts to the page. Add the Content Editor Web Part to the page, then Edit Source and add the script. Set the web part to be hidden.

    The script code:

    <script type="text/javascript">
    _spBodyOnLoadFunctionNames.push(
    "fillDefaultValues");

    function
     fillDefaultValues() 
    {

    fillPeoplePickerWithCurrentUser('Submitted_x0020_By');

    }

    function
     fillPeoplePickerWithCurrentUser(pickerName)
    {

    //get the current user from the welcome menu
    var
     currentUser = getCurrentUser();

    //check to see that we've got it

    if
    (currentUser != null)
    {

    //get the people pickers input div
    var
     pp = getPickerInputElement(pickerName);
    //set it to the current user if we've found it

    if
    (pp != null)

    pp.innerHTML = currentUser;

    }

    }

    function
     getCurrentUser()
    {

    var tags = document.getElementsByTagName('a');

    for (var i=0; i < tags.length; i++)
    {

    if(tags[i].innerText.substr(0,7) == 'Welcome')
    {

    return tags[i].innerText.substr(8,tags[i].innerText.length);

    }

    }

    }

    function
     getPickerInputElement(fieldsInternalName)
    {

    var result  = "";
    var
     divs = document.getElementsByTagName("DIV");
    for
    (var i=0; i < divs.length ; i++)
    {

    if(divs[i].id=="WebPartWPQ2")
    {

    var tds = divs[i].getElementsByTagName("TD");
    for
    (var j=0; j < tds.length; j++)
    {

    var cellHTML = tds[j].innerHTML;

    if(cellHTML.indexOf('FieldInternalName="' + fieldsInternalName + '"') >= 0)
    {

    var innerDivs = tds[j].getElementsByTagName("DIV");
    for
    (var k=0; k < innerDivs .length; k++)
    {

    if(innerDivs[k].id.indexOf("UserField_upLevelDiv") > 0)
    {

    result = innerDivs[k];
    break
    ;

    }

    }

    }

    }

    }

    }
    return
     result;

    }
    </
    script>

    For more information, see

    http://blogs.vbcity.com/skullcrusher/Default.aspx

    http://social.technet.microsoft.com/Forums/en/sharepointadminlegacy/thread/389f66a1-d1c1-4630-b5a5-e8be0b970aa7

    Best Regards.


    Kelly Chen
    TechNet Community Support

    Monday, December 24, 2012 6:43 AM
    Moderator
  • Hi Ran009,

    Assuming that you want to do this for a SharePoint list column, you can accomplish this very simply by customizing your list form with InfoPath (no Designer or Visual Studio needed). Take the following steps:

    1. When you're in the list, click on the List tab in the ribbon
    2. There select "Customize Form"
    3. Your form will now open in InfoPath. In the right hand menu you will see all your data fields. Now click on "Show Advanced view" under the data fields.
    4. In the advanced view of your fields, expand the data fields, specifically the person field you want to auto fill. When you can expand no more you should see 3 fields for the Person: Display Name, AccountID and Account Type.
    5. Now right click on the AccountID field and select "Properties"
    6. In the menu that opens you have the option to set a default value. Next to that box there is a Function button, click that button.
    7. Now another window opens where you can add fields, functions etc. Click on the Functions Button
    8. One of the available catagories will be "User". Click on that and select the "username()" function.
    9. Now click OK until you're back in InfoPath.
    10. Now right click the Account Type field and again select Properties.
    11. Now type in the default value box: user
    12. Click OK again
    13. Now you can publish your form with the Quick Publish button. This is a Blue circle with a white upward arrow and a lightning bolt next to the save button in the top menu bar.

    If you now add a new item to your list, this form will open and that person field will be prepopulated


    • Edited by E. Spelier Friday, April 19, 2013 11:35 PM
    • Proposed as answer by MattB71 Friday, May 9, 2014 9:51 AM
    Friday, April 19, 2013 11:35 PM
  • View this add-on(SharePoint Default Value Add-On), which allow user set "Current User"  as default value.



    We deliver powerful and smart SharePoint Add-Ons and web parts for your SharePoint 2010
    SharePoint Employee Directory|Password Change & Expiration|Cross-Site Lookup Column|More

    Tuesday, September 24, 2013 10:20 AM
  • E. Spelier's answer works perfectly in Sharepoint 2010. Why would they not populate that with a value by default?? So stupid. Thanks so much!
    • Edited by Drew Alden Wednesday, October 30, 2013 3:56 PM gave credit
    Wednesday, October 30, 2013 3:54 PM
  • Hi,

    When I click "Properties", the tab to set default value for the person field is disabled. Any idea on how this can be enabled? Thanks.

    Sunday, November 24, 2013 11:15 PM
  • Wow! Not often that someone takes the time to really detail a solution. Thanks! Worked perfectly

    Les Zatony, HSB

    Tuesday, November 26, 2013 4:34 PM
  • That walkthrough was great, many thanks.  It works well in SharePoint Online using InfoPath 2013
    Friday, May 9, 2014 9:53 AM
  • Hi all

    Any idea as to how I could pre-populate a Person field (with current user) in SharePoint 2013 without using InfoPath? The ideas above doesn't seem to do the trick...

    Thanks!

    Thursday, March 26, 2015 4:39 PM
  • Tuesday, February 6, 2018 3:01 PM
  • This works great when adding a new item... is there a way to have that field repopulate with the current user if someone else is later editing the item? Thanks!

    Tuesday, March 20, 2018 7:29 PM
  • Hi, Did your find a workaround for this?
    Tuesday, August 7, 2018 1:41 PM
  • It worked perfectly fine for me!!!

    Just one question here... now person name is populated and i have another sharepoint where i have defined persons manager and other properties... is there a way to fectch those properties from other list and fill in infopath form?


    Azhar

    Thursday, July 4, 2019 10:15 AM