In this blog post I am going to show you how you can assign the currently logged in user to a user and group form field control by using the ECMA Client Object Model and the latest version of the List Form Manipulation Framework (LFMF) that you can download for free on Codeplex. This code sample can also be used to understand how to retrieve the currently logged in user by using ECMA Client Object Model. First of all, upload and activate LFMF on your site collection and create a simple task list. We will populate the Task list with the currently logged in user. Go and modify the LFMF config.js in your site collection located under “/Style Library/LFMCoreModules/LFMConfig.js” and replace its contents with following code: var context = null;
var web = null;
var currentUser = null;
function fillOutUserData() {
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.onfillOutUserDataSuccess), Function.createDelegate(this, this.onfillOutUserDataFailure));
}
function onfillOutUserDataFailure(sender, args) {
alert('Not able to retrieve the current user: ' + args.get_message() + '\n' + args.get_stackTrace());
}
function onfillOutUserDataSuccess(sender, args) {
var user = web.get_currentUser();
var email = user.get_email();
var author = new qppfSPFormField("Assigned To", "SPFieldUser");
if (email != undefined && email != "") {
author.setValue(email);
}
else {
author.setValue(user.get_loginName());
}
}
$(document).ready(function () {
// put your configuration here
// this code is executed on every page load
if (window.location.href.indexOf("/Lists/Tasks/NewForm.aspx") > -1) {
ExecuteOrDelayUntilScriptLoaded(fillOutUserData, "sp.js");
}
});
Consider to clear always your browser cache to avoid having old versions of your JavaScript code when loading the page.
As you can see from the code above the relevant part of LFMF is just line 22, 24, and 27. The rest of the example is ECMA Script code to retrieve the current logged in user.
Let us analyze the code snippets step by step:
$(document).ready(function () {
// put your configuration here
// this code is executed on every page load
if (window.location.href.indexOf("/Lists/Tasks/NewForm.aspx") > -1) {
ExecuteOrDelayUntilScriptLoaded(fillOutUserData, "sp.js");
}
});
As you probably already know, the LFM Framework works with jQuery. Hence, we have to guarantee that our JavaScript code is launched when the document is ready. Since the LFMConfig.js is called on every page load, we have to distinguish our code parts by using the if statement on line 4.
function fillOutUserData() {
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.onfillOutUserDataSuccess), Function.createDelegate(this, this.onfillOutUserDataFailure));
}
This code snippet calls the ClientContext of the Client Object Model and prepares a request to retrieve the current user data. This information is retrieved from the current user property from the current web object.
The “executeQueryAsync” method starts an asynchronous call that might either succeed or fail. We are going now to see what happens if the request succeeds.
function onfillOutUserDataSuccess(sender, args) {
var user = web.get_currentUser();
var email = user.get_email();
var author = new qppfSPFormField("Assigned To", "SPFieldUser");
if (email != undefined && email != "") {
author.setValue(email);
}
else {
author.setValue(user.get_loginName());
}
}
If the request succeeds, the code listed above will be executed accordingly. Line 2 retrieves the current user data while line 3 we are retrieving the user’s e-mail address (works well in Office 365). Then LFMF comes in action and binds the “Assigned To” field of the form.
Last but not least, if the user’s e-mail was successfully retrieved, then it is going to be assigned to the “Assigned to” field. If this is not the case, then the system tries to fall back and retrieve the current login name of the user and assign it to the “Assigned to” field.
The rest is history .
Hope this helps,
Patrick