0

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>
  • Which URL you want to obtain? – Niranjan Kulkarni Nov 2 '15 at 9:50
  • url of the web within collection as it loops – Raj Chahal Nov 2 '15 at 10:01
  • url of the web is that url in your list item collection?? – Niranjan Kulkarni Nov 2 '15 at 10:03
  • I think yes, not sure what you mean. I'll update question with more code – Raj Chahal Nov 2 '15 at 10:05
  • Did you mean the display url of the Item? – Atish Dipongkor - MVP Nov 2 '15 at 10:24
0

You'll need to get the information from the web object in your loop and somehow store it for use later:

You access web object URLs in the JavaScript Object Model with web.get_url(). You could put them in an array separate from your web collections or in some kind of object structure that keeps them together -- mostly a matter of what is convenient for you.

One note, since I'm familiar with the code you're talking about, make sure that you include Url in the ctx.load statement for the web objects.

Here is an entire working snippet:

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 = [];
    // make sure to load the lists data so we can check
    // if the "Projects" list exists in each site
    // ** note the load for Url **
    myCtx.load(webs, "Include(Id, Lists, ServerRelativeUrl)");
    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)) {
                console.log("url: " + web.get_serverRelativeUrl());
                itms = webs.get_data()[i].get_lists()
                           .getByTitle("Projects").getItems(query);
                queryItemCollections.push(itms);
                myCtx.load(itms);
            }
        }
        myCtx.executeQueryAsync(function () {
            if (queryItemCollections.length == 0) {
                console.log("collection empty");
            }
            queryItemCollections.forEach(function (item) {
                // do work with individual list items here
                console.log(item.getItemAtIndex(0).get_item("Title"));
            });
        }, 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() == "Projects") {
                return true;
            }
        }
        return false;
    }
    function genericFailHandler(sender, args) {
        console.log(args.get_message());
    }
}, "SP.js");
  • Added myCtx.load(webs, "Include(Id, Lists, Title, ServerRelativeUrl)"); this gives relative, so tried: myCtx.load(webs, "Include(Id, Lists, Title, Url)"); now console reports 'Field or property "Url" does not exist.' – Raj Chahal Nov 2 '15 at 14:41
  • Tried the new code, thanks but still get same error 'Field or property "Url" does not exist.' weird? – Raj Chahal Nov 2 '15 at 15:35
  • Yep that works, For the last bit where I need to report all data from one place in the section where I also report on list info : queryItemCollections.forEach(function (item) {..... I created another array, which ties in with the 1st loop for (var i = 0 ... I've update the original question, is this robust enough? – Raj Chahal Nov 2 '15 at 15:42
  • As long as you call the same index in each array it should provide what you're looking for – John-M Nov 2 '15 at 16:53

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.