SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
where is your code running? what type of app is it? – andrew.petersen15 Mar 20 '13 at 18:55

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.

share|improve this answer
I am using the .net sharepoint libraries - not sure how I would get that global variable. – Adam Butler Mar 19 '13 at 22:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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