0

I have a SharePoint-Field of type string, that contains a value such as:

domain\usertoken

From the string I would like to retrieve the person - and from the person I would like to retrieve some user data, such as firstname and lastname.

Is there any way to ensure the user beyond the string in JSLink-context and then retrieve some user data?

My latest (failed) attempt looks like this (the relevant part is in function CustomFieldAktuellerGenehmiger(ctx)):

(function () {


    var globalOverrideCtx = {
        Templates: {
            OnPreRender:function(renderCtx){
                var visibleFields = ['Status','Aktueller_Genehmiger'];
                var fieldsCount = renderCtx.ListSchema.Field.length;
                for (var fieldIndex = fieldsCount - 1; fieldIndex >= 0; fieldIndex--) {
                    var field = renderCtx.ListSchema.Field[fieldIndex];                 
                    if(visibleFields.indexOf(field.Name)==-1){
                        renderCtx.ListSchema.Field.splice(fieldIndex, 1);
                    }
                }
            },
            Fields: {
                'Aktueller_Genehmiger':{
                    'View': CustomFieldAktuellerGenehmiger
                },  
            }
        }
    };
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(globalOverrideCtx);
})();

function CustomFieldAktuellerGenehmiger(ctx){
    var return3;

    var user = ctx.CurrentItem.Aktueller_Genehmiger;

    var context = SP.ClientContext.get_current();
    var web = context.get_web();

    // This currently returns: TypeError: Cannot read property 'get_current' of undefinedTypeError: Cannot read property 'get_current' of undefinedTypeError: Cannot read property 'get_current' of undefined
    var user = context.web.EnsureUser("cznet\zoshaeh");


    return3 = testuser;

    return return3;

}

This currently returns:

TypeError: Cannot read property 'get_current' of undefined

I don't know how to get the relevant context in JSLink.

Update: I have also tried:

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){
var context = SP.ClientContext.get_current();
var web = context.get_web();
var user = context.web.EnsureUser("domain\usertoken");
console.log(user);
})

This returns:

Uncaught TypeError: Cannot read property 'EnsureUser' of undefined

1 Answer 1

1

You could try to use this to wrap your code uesd ClientContext.

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){})

Updated:

This code I have used for your reference:

<script type="text/javascript">
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){
  var testCase=function(callback){      
        var clientContext = SP.ClientContext.get_current();
        var user = clientContext.get_web().get_currentUser();
        clientContext.load(user);
        clientContext.executeQueryAsync(function(){     
        callback(null,user.get_loginName()) ;          
        },function(sender,args){alert(args.get_message());})
    }

    }) 
</script>

similar issue: https://www.enjoysharepoint.com/uncaught-typeerror-cannot-read-property-get_current-of-undefined/

6
  • Thank you very much for your quick answer. Where would be the correct place in my code to set this line? Do I have to wrap the complete function: function CustomFieldAktuellerGenehmiger(ctx){}.
    – SteffPoint
    Jun 9, 2020 at 7:48
  • 1
    check the update
    – Amos
    Jun 9, 2020 at 7:57
  • Thanks for the update Amos. I still don't seem to get it properly - I'm sorry. I have updated my question with an attempt I have done in reference to your answer.
    – SteffPoint
    Jun 9, 2020 at 8:56
  • 1
    Try to use context.get_web().ensureUser('contoso\name') directly.
    – Amos
    Jun 15, 2020 at 7:06
  • 1
    executeQueryAsync is an asynchronous method,the variable may not be assigned value before being used.You couls try to use setTimeOut or run executeQueryAsync synchronously.Demo:sharepoint.stackexchange.com/questions/134932/…
    – Amos
    Jun 15, 2020 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.