1

I need to get the url List in the subsite in share point. Ex:

sharepoint/sitecollection/site1/subsite1/subsite2/subsite3/List

Can any one help?

1

3 Answers 3

1

This is not a hard thing, and it is tons of examples all over the web.

Here is from MSDN:

use SPWeb.GetList:

using (SPSite site = new SPSite("http://localhost/sites/sitecollection"))
         {
            using (SPWeb web = site.OpenWeb("sitecollection/subsite"))
            {
               string listUrl = "/sites/sitecollection/subsite/Lists/Announcements";
               SPList list = web.GetList(listUrl);
               Console.WriteLine("List URL: {0}", list.RootFolder.ServerRelativeUrl);
            }
         }

So the challenge here is mainly to parse out the information from the URL that is not the

/Lists/ListName

part and then use that URL in new SPSite and site.OpenWeb, like

var url = sharepoint/sitecollection/site1/subsite1/subsite2/subsite3
var listUrl = sharepoint/sitecollection/site1/subsite1/subsite2/subsite3/List
using (SPSite site = new SPSite(url))
         {
            using (SPWeb web = site.OpenWeb(url))
            {
               SPList list = web.GetList(listUrl);
               Console.WriteLine("List URL: {0}", list.RootFolder.ServerRelativeUrl);
            }
         }
1
  • Or where you talking about getting a specific lists url? In that case you can use the same approach but instead of getting the actual list by URL you can get it by internal name or title ( web.Lists.GetByTitle("Announcements") ) Commented Jun 5, 2013 at 7:11
0

Try this:

        string url = "http://sharepoint/sitecollection/site1/subsite1/subsite2/subsite3/List/Forms/AllItems.aspx"
        using (SPSite site = new SPSite(url))
        {
            using (SPWeb web = site.OpenWeb())
            {               
                list = web.GetListFromUrl(url);                  
                string siteUrl = web.Url;
                //This is the URL
                string absolute = siteUrl + "/" + list;

            }

        }
0

Try This:

using (SPWeb web = SPContext.Current.Web)//site.OpenWeb())
                {
                    SPList list = web.Lists["ListName"];
---------------
--------------
}

Better we should not give the URL as hard code, if we change the managed path in the other environment then it will give problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.