I have the below code which is looping through list names and trying to get the URL. However the code only returns the last list name url as it skips over the "currentcontext.executeQueryAsync(Function.createDelegate(this, result), Function.createDelegate(this, oncListQueryFailed));" the first and second time.
I'm getting the following JS Error:
Error: The property or field 'ServerRelativeUrl' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
Not sure why?
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
var list;
var listRootFolder;
ExecuteOrDelayUntilScriptLoaded(init, "sp.js");
function init() {
var Lists = ["List 1", "List 2", "List 3"];
//load site
Lists.forEach(function(listTitle) {
console.log(listTitle);
var currentcontext = new SP.ClientContext('/StrategicProjectOffice');
list = currentcontext.get_web().get_lists().getByTitle(listTitle);
listRootFolder = list.get_rootFolder();
currentcontext.load(listRootFolder);
currentcontext.executeQueryAsync(Function.createDelegate(this, result), Function.createDelegate(this, oncListQueryFailed));
});
}
function result() {
var listURL = listRootFolder.get_serverRelativeUrl();
console.log(listURL);
var fullUrl = _spPageContextInfo.webAbsoluteUrl + "/SitePages/Project Page - " + listRootFolder.get_name() + ".aspx";
console.log(fullUrl);
}
function oncListQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>