Sharepoint - ExecuteOrDelayUntillScriptLoaded(codetorun,SP.js) for entire script function
Use SP.SOD.executeFunc('sp.js','SP.ClientContext',runthiscode);
instead of SP.SOD.executeOrDelayUntilScriptLoaded. because SP.SOD.executefunc makes sure the file is loaded and then executes the method and SP.SOD.executeOrDelayUntilScriptLoaded will only put the method on queue untill the file is loaded(so if file is not loaded then never executes).
SP.SOD.executeFunc loads on demand scripts, so script link should be added for that js file.
<SharePoint:ScriptLink ID="ScriptLink2" name="SP.UserProfiles.js" runat="server"
ondemand="false" localizable="false" loadafterui="true" />
<script type="text/javascript">
SP.SOD.executeFunc('sp.js','SP.ClientContext',runthiscode);
function runthiscode()
{
// Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
SP.SOD.executeFunc('SP.UserProfiles.js','SP.UserProfiles',getUserProperties);
}
var personProperties;
function getUserProperties() {
// Get the current client context and PeopleManager instance.
var clientContext = new SP.ClientContext.get_current();
var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
// Get user properties for the target user.
// To get the PersonProperties object for the current user, use the
// getMyProperties method.
personProperties = peopleManager.getMyProperties();
// Load the PersonProperties object and send the request.
clientContext.load(personProperties);
clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);
}
// This function runs if the executeQueryAsync call succeeds.
function onRequestSuccess() {
if (personProperties.get_userProfileProperties().CustomPropertyValue == "False") {
window.location(SP.Utilities.Utility.getLayoutsPageUrl('someurl.aspx');
}
}
// This function runs if the executeQueryAsync call fails.
function onRequestFail(sender, args) {
var r = confirm("There was an error, the page will reload");
window.location(SP.Utilities.Utility.getLayoutsPageUrl('someurl.aspx');
}
</script>
Prefer SP.SOD.executeFunc over SP.SOD.executeOrDelayUntilScriptLoaded
Since SP.SOD.executeFunc supports load on demand scripts, there is no need to reference explicitly SP JavaScript files using SharePoint:ScriptLink
The following example demonstrates how to initialize UserProfiles
CSOM in application page:
SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function() {
// Make sure PeopleManager is available
SP.SOD.executeFunc('userprofile', 'SP.UserProfiles.PeopleManager', function() {
var clientContext = new SP.ClientContext.get_current();
var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
//...
});
});
Usage of SP.Utilities.Utility.getLayoutsPageUrl
SP.Utilities.Utility.getLayoutsPageUrl(pageName) Method accepts pageName
as a parameter.
Example: how to get Settings page url
var pageUrl = SP.Utilities.Utility.getLayoutsPageUrl("Settings.aspx");