tags:

views:

302

answers:

3

Beside query string root folder

how do i get the current doc library list(ListName) from below example url

http://site/subsite/ListName/Forms/AllItems.aspx

any standard way?

A: 

Try this

using (SPSite site = new SPSite("http://site/subsite/ListName/Forms/AllItems.aspx"))
using (SPWeb web = site.OpenWeb()) {
  SPList list = web.GetListFromUrl("http://site/subsite/ListName/Forms/AllItems.aspx");
}
Janis Veinbergs
if i need this dynamically, for eg, the url is a home page of the site, will it throw exception if the page doesnt contain the list, or the page url doesnt equal to a list url
I didn't understand your question, but you can also use relative urls. Also, you may want to try if SPWeb.GetList will work for you instad of GetListFromUrl.And, if list does not exist, it will surely throw you an exception (Probably SPException)
Janis Veinbergs
+1  A: 
SPWeb web = SPContext.Current.Web;
string listname = "Not a list";
try
{
    SPList list = web.GetList(HttpContext.Current.Request.RawUrl);
    if (null != list)
    {
        listname = list.Title;
    }
}
catch (System.IO.FileNotFoundException) { }
HttpContext.Current.Response.Write(listname);
Rich Bennema
If the current URL is valid for a list, SPContext.Current.List would probably also work.
Tom Clarkson
A: 

Has this been resolved? Similar issue. Just trying to get the current document library that user is in now.

Rosh Malai