In my application I have a url of a folder as retrieved by folder.ServerRelativeUrl() eg. /consulting/TeamSiteTemplate/Documents/

Now I want to get a list of folders inside this folder. I use this code:

using (ClientContext clientContext = new ClientContext(siteUrl))
   folder = clientContext.Web.GetFolderByServerRelativeUrl(folder.ServerRelativeUrl);

   foreach (Folder childFolder in folder.Folders)
      // process folder

This only seems to work when siteUrl contains the site eg. http://sharepoint/consulting/TeamSiteTemplate - but what if I don't know the site name of the given url? Or don't know if it has a subsite, etc.

So simply:

How can I get a site/subsite part from a relative url - or just the site if no subsite?

or: Is there another way to do this?

  • where is your code running? what type of app is it? Mar 20, 2013 at 18:55

1 Answer 1


Assuming you wait for SP.js to load (which seems safe since your using CSOM), you should have a global variable named "L_Menu_BaseUrl" which will be the absolute url to the current site.

Between this, the folder's server relative url, and a little logic, you should be able to get what you need.

  • 1
    I am using the .net sharepoint libraries - not sure how I would get that global variable. Mar 19, 2013 at 22:33

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.