What I am trying to do is to enable the dialog with the metadata information when I drag and drop a file in a document library. The same dialog that appears when you upload a file through the +New Document ribbon.

I already have a solution using JavaScript with SPServices, however I have everything hardcoded. I need a way to get the full URL in a relative way, without passing manually the name of the document library.

I know so far that _spPageContextInfo.siteServerRelativeUrl gets you the relative URL of the site; also ctx.HttpRoot retrieves the relative URL of the site. But I need this relative URL + the Document Library in which I am positioned.

Suppose that I have the following site: http://mySite.com/Site1/DocLibrary/ThisIsASampleDoc.doc

I need to get the relative URL of: http://mySite.com/Site1/DocLibrary/

  • possible duplicate of How to find relative URL of a document library using CSOM Jul 30, 2014 at 21:25
  • Yeah I have seen this question, but you need to provide manually the name of the library before getting the relative path. What I need is to get that relative url without passing any information of the library in which I am positioned. Is it posible to achieve this?
    – user31620
    Jul 30, 2014 at 21:29
  • so you want the library url, without providing any meaningful information about the library? tough job. if the current url contains the information you want maybe you can parse it, e.g. var page = window.location.pathname.split('/').pop(); Jul 30, 2014 at 21:45
  • Yeah. At this moment what I am doing is applying a script to the site once I enter the document library desired. So the only valuable information is that I am positioned at the document library when the script is loading and being applied. How I mentioned before, right now I have the SP service passing manually the name of the library, and it works perfectly. But I need it to work on 50 different document libraries, so I don't want the same script being loaded for each library, I am planning to add the script to the master page and work according to the document library that will be visited.
    – user31620
    Jul 30, 2014 at 21:57
  • you can grab an example of the libs landing url and first validate if you are on a library, and then extract the library name, maybe resorting to regular expressions to help Jul 30, 2014 at 22:08

2 Answers 2


You could consider the following options to determine the relative Url of the current Document Library:

SP.ListOperation.Selection namespace

Using SP.ListOperation.Selection.getSelectedList() Method you could get the ID of the list being selected:

var currentListId = SP.ListOperation.Selection.getSelectedList();

and then retrieve List object or its properties as demonstrated below:

var currentListId = SP.ListOperation.Selection.getSelectedList();
var ctx = SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getById(currentListId);
       console.log(list.get_rootFolder().get_serverRelativeUrl()); //print relative url of list 
    function(sender, args)
      console.log('Request failed ' + args.get_message() + ':'+ args.get_stackTrace());

ContextInfo structure

With XLV web part is associated ContextInfo structure that is rendered on the client side. The following example demonstrates how to get the relative url of the list:

var ctx = GetCurrentCtx();
var listUrl = ctx.listUrlDir; 

Get the list name using the variable ctx.ListTitle (reference: How to get List Name from JS Object Model - List Name from URL problem). Once you have the name of the current list, you can use the information from @tiago-duarte: How to find relative URL of a document library using CSOM

Open up Chrome's Developer Tools and run "ctx" in the console. Inspect the object and you'll see if has some pretty cool properties.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.