Thursday, 25 October 2012

Javascript to Get Current user name in People Picker of sharepoint list form

If you want to current user name in the people picker of sharepoint list form

Here is a simple javascipt to get your current user in your sharepoint people picker control


1. Just add content editor webpart.Refer How to add CEWP
2. Copy below script and paste in the HTML of your CEWP
3. Change picker number based on your list form.Picker Number is the number of your People Picker control from the top.If only one PP is there than Picker Number is always 1

<script type="text/javascript">
    ExecuteOrDelayUntilScriptLoaded(GetUserLoginName, "sp.js");

    function GetUserLoginName() {
        context = new SP.ClientContext.get_current();
        web = context.get_web();
        this._currentUser = web.get_currentUser();
        context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod),
            Function.createDelegate(this, this.onFailureMethod));

    function onSuccessMethod(sender, args) {
        alert('Name:' + this._currentUser.get_title() + '\n Login:' + this._currentUser.get_loginName());

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

    function AddCurrentUserToPP(LoginName) {
        var pickerNo = 1;
        // Change pickerNo based on your list form if there are 3 PP in your form and you want the current user in 3rd one than pickerNo value will be 3

        var pp = getPickerImputElement(pickerNo);
        if (pp != null) {
            pp.innerHTML = LoginName;

    function getPickerImputElement(pickerNo) {
        var result = '';
        var divs = document.getElementsByTagName('DIV');
        var j = 0;
        for (var i = 0; i < divs.length; i++) {
            if (divs[i].id.indexOf('UserField_upLevelDiv') > 0) {
                if (j == pickerNo) {
                    result = divs[i];
        return result;

Note: This is only usefull if you want Current user in one PP only...:(

No comments:

Post a Comment