3

I'm trying to retrieve the url for all lists on the current SharePoint site. I've managed to get the title for each list, I've also found resources explaining how to get the url of one specific list. But my question is how do i get the url of every list on the site. If you take a look at the part url:oList.get_url(). You'd think there was a method like this to retrive the url from the enumerator, but it does not work.

this.get_lists = function(onSuccess, onFailure, url){
        this.setContext(url);

        var oWebsite = this.context.get_web();
        this.collList = oWebsite.get_lists();
        this.context.load(this.collList);

        function onSucceed () {
            var listEnumerator = this.collList.getEnumerator();

            while (listEnumerator.moveNext()){
                var oList = listEnumerator.get_current();
                var current_oList = {
                    title:oList.get_title(),
                    url:oList.get_url()
                }
                onSuccess(current_oList);
            }
        }

        function onFail(sender, args){
            onFailure(args.get_message());
        }

        this.context.executeQueryAsync(
            Function.createDelegate(this, onSucceed), 
            Function.createDelegate(this, onFail)
        );

    };
2

You should explicitly load the URL using the load method

this.context.load(this.collList, "Include(Title, DefaultViewUrl)");

so your onSucceed will be like

function onSucceed () {
        var listEnumerator = this.collList.getEnumerator();

        while (listEnumerator.moveNext()){
            var oList = listEnumerator.get_current();
            var current_oList = {
                title:oList.get_title(),
                url:oList.get_defaultViewUrl()
            }
            onSuccess(current_oList);
        }
    }
  • And what method call do i do once the success callback fires? – Thomas Lindauer Oct 29 '14 at 13:42
  • @ThomasLindauer I have updated the answer – Amal Hashim Oct 29 '14 at 13:44
  • Using load is it possible to add a Where clause? I'm trying to return only document libraries instead of all the lists. I've seen this syntax but can't get it working in javascript var Libraries = clientContext.LoadQuery(Web.Lists.Where(l => l.BaseTemplate == 101)); – Batman Aug 14 '16 at 21:55
0

Even though you asked for JSOM, you could also use REST:

/_api/Web/Lists?$expand=RootFolder&$select=RootFolder/ServerRelativeUrl

You can ask for other properties as well, such as Name. Url didn't seem to work on my SharePoint Online.

0

There is a new JavaScript library added by Patterns and Practices team. That library uses the REST API and easy way in obtaining the result in less lines of code.

We require a pnp.js file, which is available from here To run the below code in IE include fetch.js and promise.js to the code.

<script type="text/javascript" src="/siteassets/scripts/fetch.js"></script> 
<script type="text/javascript" src="/siteassets/scripts/promise.min.js"></script>  
<script type="text/javascript" src="/siteassets/scripts/pnp.min.js"></script> 

The below code returns all lists title and default view url.

$pnp.sp.web.lists.select('Title', 'DefaultViewUrl').get().then(function(data) {
for (var i = 0; i < data.length; i++) {
    console.log(data[i].Title + " - " + data[i].DefaultViewUrl);
}   });

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.