0

I am trying to retrieve all document libraries in a site collection using an SP.Service and place them in a container with their correspondent url pointing to the internal library. So far I have retrieved the names of the document libraries, however I haven't been able to obtain the desired url. Can someone help me here?

Below is the code that I am using:

      var context, lists;

      ExecuteOrDelayUntilScriptLoaded(retrieveAllListProperties, "sp.js");

      function retrieveAllListProperties()
      {
        // Get the Current SP Context
        context = SP.ClientContext.get_current();
        lists = context.get_web().get_lists();
        context.load(lists);

        context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
      }

      function onQuerySucceeded() 
      {
          var listInfo = '';
          var listEnumerator = lists.getEnumerator();

          while (listEnumerator.moveNext()) {
              var oList = listEnumerator.get_current();
              listInfo += 'Title: ' + oList.get_title() + '\n'; // I am getting the title of the document library here. I also need the url of that document library
              //
              //HOW DO I GET THE URL OF THE ACTUAL oLIST ??? PLEASE HELP
              //
          }
          console.log(listInfo);
      }

    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + 
            '\n' + args.get_stackTrace());
    }
1

You can get the list url from the get_defaultViewUrl method...

when you load the lists you need to use the include statement to retrieve the defaultViewUrl property (it isn't included with the list results by default). Change your context.load line to:

context.load(lists, 'Include(Title,DefaultViewUrl)');

Then inside of your iterator loop you can use

oList.get_defaultViewUrl()

Then you have a choice of either leaving that result as the URL you use, or you can use some string manipulation to chop off everything after the last slash. You can do that with substring and lastIndexOf:

var viewUrl = oList.get_defaultViewUrl();
var listUrl = viewUrl.substring(0,viewUrl.lastIndexOf('/')+1);
2
  • Thank you very much, this solves my problem. It was exactly what I was looking for. – user31620 Aug 29 '14 at 20:29
  • Great, good to hear -- good luck with your project! – John-M Aug 29 '14 at 20:31

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.