3

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);
   clientContext.Load(folder);
   clientContext.Load(folder.Folders);
   clientContext.ExecuteQuery();

   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?

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

1 Answer 1

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
  • 1
    I am using the .net sharepoint libraries - not sure how I would get that global variable. Commented Mar 19, 2013 at 22:33

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.