1

Trying to figure out why I am getting this error with my code, am I missing a script reference, or is perhaps something wrong with the Context data grab section?

Error:

The property or field 'Email' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

Script references in Web Part Page:

<SharePoint:ScriptLink name="clienttemplates.js" runat="server" LoadAfterUI="true" Localizable="false"/>
<SharePoint:ScriptLink name="clientforms.js" runat="server" LoadAfterUI="true" Localizable="false"/>
<SharePoint:ScriptLink name="clientpeoplepicker.js" runat="server" LoadAfterUI="true" Localizable="false"/>
<SharePoint:ScriptLink name="autofill.js" runat="server" LoadAfterUI="true" Localizable="false"/>
<SharePoint:ScriptLink name="sp.RequestExecutor.js" runat="server" LoadAfterUI="true" Localizable="false"/>
<SharePoint:ScriptLink name="sp.js" runat="server" LoadAfterUI="true" Localizable="false"/>
<SharePoint:ScriptLink name="sp.runtime.js" runat="server" LoadAfterUI="true" Localizable="false"/>
<SharePoint:ScriptLink name="sp.core.js" runat="server" LoadAfterUI="true" Localizable="false"/>
<SharePoint:ScriptLink name="SP.Debug.js" runat="server" LoadAfterUI="true" Localizable="false"/>
<SharePoint:ScriptLink name="SP.Runtime.Debug.js" runat="server" LoadAfterUI="true" Localizable="false"/>

Code:

///////////////////Enable JSOM Intellisense for MS Visual Studio////////////////////////////////////////////////////////////////////////
/// <reference path="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\clienttemplates.js" />
/// <reference path="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\clientforms.js" />
/// <reference path="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\clientpeoplepicker.js" />
/// <reference path="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\autofill.js" />
/// <reference path="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\sp.RequestExecutor.js" />
/// <reference path="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\sp.js" />
/// <reference path="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\sp.runtime.js" />
/// <reference path="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\sp.core.js" />
/// <reference path="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\sp.debug.js" />
/// <reference path="C:\Projects\jquery\VsDoc\jquery-1.4.4-vsdoc.js" />
//////////////////////////////////////////////////////////////////////////////////////////////////////////
v_item = '';

$(
function () {
    if (typeof (_spBodyOnLoadWrapper) != 'undefined') { _spBodyOnLoadWrapper(); }
ExecuteOrDelayUntilScriptLoaded(getDivision_Chief_User, "sp.js");
});

function getDivision_Chief_User() {
    var clientContext = new SP.ClientContext.get_current();

    var web = clientContext.get_web();

    var list = web.get_lists().getByTitle('myTestList'); 

    v_item = list.getItemById(1);

    clientContext.load(v_item);

    clientContext.executeQueryAsync(onSuccess, onFailure);
}
function onSuccess(sender, args) {
    var context = SP.ClientContext.get_current();
    var web = context.get_web();
    var userName = v_item.get_item('myPersonLookUpField').get_lookupValue();
    var user = web.ensureUser(userName);

    //ERROR HAPPENS HERE WHEN DEBUGING IN BROWSER

    var email = user.get_email();
    var loginName = user.get_loginName();
    var v_Id = user.get_id();
    var v_Title = user.get_title();

    alert('The Display Name of the user is ' + email);
    alert('The Account Name of the user is ' + loginName);
    alert('The email address of the user is ' + v_Id);
    alert('The User Title is ' + v_Title);

    //$("#h_ValueWritter").html(v_commentsTemp);


}
function onFailure(sender, args) {

    console.log('Failure!');
}
1

I believe it is happening because you have to load the user object before you can use it. So for instance, after your lines:

var context = SP.ClientContext.get_current();
var web = context.get_web();
var userName = v_item.get_item('myPersonLookUpField').get_lookupValue();
var user = web.ensureUser(userName);

you would then need to do:

context.load(user);
context.executeQueryAsync(onSecondSuccess, onFailure);

before you can work with the user object:

var email = user.get_email();
var loginName = user.get_loginName();
var v_Id = user.get_id();
var v_Title = user.get_title();
  • Tried that and still getting same error as the original error The property or field 'Email' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. – Shane Gib. Oct 24 '16 at 16:31
1

Try the code modified as below:

    ///////////////////Enable JSOM Intellisense for MS Visual Studio////////////////////////////////////////////////////////////////////////
    /// <reference path="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\clienttemplates.js" />
    /// <reference path="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\clientforms.js" />
    /// <reference path="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\clientpeoplepicker.js" />
    /// <reference path="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\autofill.js" />
    /// <reference path="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\sp.RequestExecutor.js" />
    /// <reference path="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\sp.js" />
    /// <reference path="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\sp.runtime.js" />
    /// <reference path="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\sp.core.js" />
    /// <reference path="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\sp.debug.js" />
    /// <reference path="C:\Projects\jquery\VsDoc\jquery-1.4.4-vsdoc.js" />
    //////////////////////////////////////////////////////////////////////////////////////////////////////////
    v_item = '';
    SP.SOD.executeFunc("sp.js", "SP.ClientContext", function(){
            SP.SOD.registerSod("sp.userprofiles.js", SP.Utilities.Utility.getLayoutsPageUrl("sp.userprofiles.js"));
            SP.SOD.executeFunc("sp.userprofiles.js", "SP.UserProfiles.PeopleManager", getDivision_Chief_User);
    });

    function getDivision_Chief_User() {
        var clientContext = new SP.ClientContext.get_current();

        var web = clientContext.get_web();

        var list = web.get_lists().getByTitle('myTestList'); 

        v_item = list.getItemById(1);

        clientContext.load(v_item,"Include(Title,myPersonLookUpField)"); //add the column internal name

        clientContext.executeQueryAsync(onSuccess, onFailure);
    }
    function onSuccess(sender, args) {
        var context = SP.ClientContext.get_current();
        var web = context.get_web();
        var userName = v_item.get_item('myPersonLookUpField').get_lookupValue();
        var user = web.ensureUser(userName);
        context.load(user);
        context.executeQueryAsync(function(){
       var email = user.get_email();
        var loginName = user.get_loginName();
        var v_Id = user.get_id();
        var v_Title = user.get_title();

        alert('The Display Name of the user is ' + email);
        alert('The Account Name of the user is ' + loginName);
        alert('The email address of the user is ' + v_Id);
        alert('The User Title is ' + v_Title);

        //$("#h_ValueWritter").html(v_commentsTemp);  

},
function(sender, args){

  alert(args.get_message());

});

    }
    function onFailure(sender, args) {

        console.log('Failure!');
    }
  • Getting the following error at below line of code: The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. var userName = v_item.get_item('myPersonLookUpField').get_lookupValue(); – Shane Gib. Oct 24 '16 at 16:14
  • have modified it now. It will include the custom column in context.load – Gautam Sheth Oct 24 '16 at 18:05

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.