6

I always get undefined on my SP.ClientContext.get_current()

I have tried with this code:

SP.SOD.executeFunc('sp.js','SP.ClientContext',runthiscode);

function runthiscode(){

SP.SOD.executeFunc('SP.UserProfiles.js','SP.UserProfiles',initialize);

}

And this code:

ExecuteOrDelayUntilScriptLoaded(initialize, "SP.UserProfiles.js");

I use this code for all scripts

<script type="text/javascript">
 if (window.jQuery == undefined) 
 document.write(unescape('%3Cscript src="../style library/scripts/jquery-2.0.3.js"    type="text/javascript"%3E%3C/script%3E') );
 document.write(unescape('%3Cscript src="/_layouts/15/SP.js" type="text/javascript"%3E%3C/script%3E') );
 document.write(unescape('%3Cscript src="/_layouts/15/SP.UserProfiles.js" type="text/javascript"%3E%3C/script%3E') );
</script>

The script is working if the newform.aspx is in edit page, then it autofill my columns from mysite information.

Update!

I got SP.js working, but not for userprofiles.js this is the code that is working for now:

var ctx = new SP.ClientContext.get_current();
var peopleManager = new SP.UserProfiles.PeopleManager(ctx);

ctx.add_requestFailed(function (sender, args) {
alert('Request failed: ' + args.get_message());
});

function example () {
}
window.onload = example;

So what can the problem be with the userprofiles ?

Update,

With this code above i cant push the button save

3

You can load your JS files directly, you don't need to do document.write

<script type="text/javascript" src="/_layouts/15/SP.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.userprofiles.js"></script>

For your, js code, try loading those within dom ready.

$(document).ready(function () {
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
        SP.SOD.executeFunc('SP.UserProfiles.js', 'SP.UserProfiles', initialize);
    });
});
  • it still not working, it only works when i am in edit mode for newform.aspx. Its just this newform.aspx that this script is not working.. – Anders Jul 3 '14 at 14:26
  • If you can edit NewForm.aspx using designer, add those script reference within ContentPlaceHolderId="PlaceHolderAdditionalPageHead". See if that helps. – Aveenav Jul 8 '14 at 13:49
3

Maybe it's because all the SharePoint JavaScript-Files are not ready when you try to access your context. Instead of document.ready or similar, try this:

_spBodyOnLoadFunctionNames.push("myCustomFunctionName"); //waits for all SP-Scripts

function myCustomFunctionName() {
     // My custom functionality
}
  • I tried that also – Anders Jul 4 '14 at 7:15
  • Ok thank you all for the help. i recogniced a fault collumn in my newform that gave me a unload of the page. thanks anyway. – Anders Jul 9 '14 at 1:20
1

Based on Khristopher's example for workflows, I was able to get this working for user profiles in display and edit modes:

<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(function () {
    ExecuteOrDelayUntilScriptLoaded(function () {
        SP.SOD.registerSod('SP.ClientContext', SP.Utilities.Utility.getLayoutsPageUrl('sp.js'));
        SP.SOD.registerSod('SP.UserProfiles', SP.Utilities.Utility.getLayoutsPageUrl('SP.UserProfiles.js'));
        SP.SOD.loadMultiple(['SP.ClientContext', 'SP.UserProfiles'], function () {
            var clientContext = SP.ClientContext.get_current();
            var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
        userProfileProperties = peopleManager.getMyProperties();
        clientContext.load(userProfileProperties);
        clientContext.executeQueryAsync(onSuccess, onFail);
        });
    }, "sp.js");
}, "sp.runtime.js");

function onSuccess() {      
  alert(userProfileProperties.get_displayName());
}

function onFail(sender, args) {
  alert("Error: " + args.get_message());
}
</script>
0

This works for me when I am working with WorkflowServices. I imagine that it would be very similar for your UserProfiles example:

ExecuteOrDelayUntilScriptLoaded(function () {
    ExecuteOrDelayUntilScriptLoaded(function () {
        SP.SOD.registerSod('SP.ClientContext', SP.Utilities.Utility.getLayoutsPageUrl('sp.js'));
        SP.SOD.registerSod('SP.WorkflowServices.WorkflowServicesManager', SP.Utilities.Utility.getLayoutsPageUrl('SP.WorkflowServices.js'));
        SP.SOD.loadMultiple(['SP.ClientContext', 'SP.WorkflowServices.WorkflowServicesManager'], function () {
            var context = SP.ClientContext.get_current();
            var web = context.get_web();
            var servicesManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, web);
            var instanceService = servicesManager.getWorkflowInstanceService();
        });
    }, "sp.js");
}, "sp.runtime.js");

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.