I have the following code and need to obtain the site url to form a hyperlink. How can this be achieved?
Code prints out title but I cant get to the web url . I want the url of the site that the list item is within. i.e. 'mysite.com/sites/site-collection/this-sub-site'
myCtx.executeQueryAsync(function () {
// push new SP.ListItemCollections into an array and tell the ctx to fetch them
for (var i = 0; i < webs.get_data().length; i++) {
var web = webs.get_data()[i], itms;
if (checkForProjectsList(web)) {
itms = webs.get_data()[i].get_lists()
.getByTitle("Project").getItems(query);
queryItemCollections.push(itms);
myCtx.load(itms);
}
}
myCtx.executeQueryAsync(function () {
queryItemCollections.forEach(function (item) {
console.log(item.getItemAtIndex(0).get_item("Title"));
UPDATE ::
<script language="javascript" type="text/javascript">
SP.SOD.executeOrDelayUntilScriptLoaded(function () {
var myCtx = SP.ClientContext.get_current();
var root = myCtx.get_site().get_rootWeb(); // may need to do this if in a sub site
var webs = root.get_webs(); // fetch a reference to all the sub webs
var query = new SP.CamlQuery();
// only need to fetch the first item returned from each Project list
query.set_viewXml('<View Scope="RecursiveAll"><Query></Query><RowLimit>1</RowLimit></View>');
var queryItemCollections = [];
var querySite = [];
// make sure to load the lists data so we can check
// if the "Projects" list exists in each site
myCtx.load(webs, "Include(Id, Lists, Title, ServerRelativeUrl)");
// myCtx.load(webs, "Include(Id, Lists, Title, Url)"); // not working
myCtx.executeQueryAsync(function () {
// push new SP.ListItemCollections into an array and tell the ctx to fetch them
for (var i = 0; i < webs.get_data().length; i++) {
var web = webs.get_data()[i], itms;
if (checkForProjectsList(web)) {
itms = webs.get_data()[i].get_lists()
.getByTitle("Project").getItems(query);
queryItemCollections.push(itms);
querySite.push(web.get_serverRelativeUrl());
//querySite.push(web.get_url());
myCtx.load(itms);
console.log(itms);
//console.log(web.get_title());
console.log(web.get_serverRelativeUrl());
}
}
myCtx.executeQueryAsync(function () {
if (queryItemCollections.length == 0) {
console.log("collection empty");
}
var k=0;
queryItemCollections.forEach(function (item) {
// do work with individual list items here
// console.log(item.getItemAtIndex(0).get_item("Title"));
var oProj=item.getItemAtIndex(0).get_item("ProjectRef");
var oTitle=item.getItemAtIndex(0).get_item("Title");
// $('#example').dataTable().fnAddData( [ oProj,oProj ] );
console.log(querySite[k]);
k++;
$('#example').dataTable().fnAddData( [ oProj,oTitle, '' ] );
});
}, genericFailHandler);
}, genericFailHandler);
function checkForProjectsList(web) {
var lists = web.get_lists().get_data();
for (var j = 0; j < lists.length; j++) {
var list = lists[j];
if (list.get_title() == "Project") {
return true;
}
}
return false;
}
function genericFailHandler(sender, args) {
console.log(args.get_message());
}
}, "SP.js");
</script>
<table cellpadding="0" cellspacing="0" width="100%" class="display" id="example">
<thead><tr >
<th style="text-align:left;">ACE</th>
<th style="text-align:left;">BASE</th>
<th style="text-align:left;">BASE</th>
</tr></thead>
<tbody>
</tbody>
</table>
<script type="text/javascript">
$('#example').dataTable( {
paging: false,
searching: false
} );
</script>