Current User in newform

A new cool feature in SP2013 is JSLink. My first use was to insert the current user in a SPUser field.

Create a javascript file with the content below and deploy it to SharePoint. To link the javscript file to the right field on the list, take a look here

// load jquery as it is not in the system master
loadScript('**link to jQuery**', function () {
    _spBodyOnLoadFunctionNames.push("waitForSP");
});

function waitForSP() {
if (location.href.toLowerCase().indexOf('newform.aspx')>-1) {
ExecuteOrDelayUntilScriptLoaded(loadCurrentUser, "sp.js");
}
}

var errorCount = 0;
var context = null;
var web = null;
var currentUser = null;
var listItems = null;
var fieldName = 'Kontaktperson';

function loadCurrentUser() {
    context = new SP.ClientContext.get_current();
    web = context.get_web();
    context.load(web);
    context.executeQueryAsync(loadUser, onFailureMethod);
}

function loadUser(sender, args) {
    try {
        currentUser = web.get_currentUser();
    }
    catch (ex) {
        onFailureLoadUser(sender, args);
    }
    context.load(currentUser);
    context.executeQueryAsync(loadProfile, onFailureLoadUser);
}

function loadProfile(sender, args) {
    userInfoList = web.get_siteUserInfoList();
    camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'ID\'/><Value Type=\'Number\'>' + currentUser.get_id() + '</Value></Eq></Where></Query><RowLimit>1</RowLimit></View>');
    listItems = userInfoList.getItems(camlQuery);
    context.load(listItems);
    context.executeQueryAsync(onProfileSuccessMethod, onFailureMethod);
}

function onProfileSuccessMethod(sender, args) {
    var item = listItems.itemAt(0);
    var loginname = item.get_item('Name');
    if (typeof (loginname) != 'undefined') {
        SetAndResolvePeoplePicker(loginname);
    }
}
function onFailureMethod(sender, args) {
    alert('Error: ' + args.get_message() + '\n' + args.get_stackTrace());
}

function onFailureLoadUser(sender, args) {
    if (errorCount < 5) {
        errorCount++;
        setTimeout(function () { loadCurrentUser(); }, 1000);
    } else {
        onFailureMethod(sender, args);
    }
}

function SetAndResolvePeoplePicker(userAccountName) {
    var peoplePickerDiv = $("[id$='ClientPeoplePicker'][title='" + fieldName + "']");
    var peoplePickerEditor = peoplePickerDiv.find("[title='" + fieldName + "']");
    var spPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[peoplePickerDiv[0].id];
    peoplePickerEditor.val(userAccountName);
    spPeoplePicker.AddUnresolvedUserFromEditor(true);
}

function loadScript(url, callback) {
    var head = document.getElementsByTagName('head')[0];
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = url;
    script.onreadystatechange = callback;
    script.onload = callback;
    head.appendChild(script);
}


Thanks to stackexchange for giving me the solution to resolve a user

Sidst opdateret: 27. jan.  2015

Submenu