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?

Robert Lindgren
  • 24,520
  • 12
  • 53
  • 79
Chandan Banakar
  • 31
  • 1
  • 1
  • 4
  • Hi! Check out Anders' answer on my question from a while back: http://sharepoint.stackexchange.com/questions/89773/get-absolute-url-to-listitems-default-display-form – Rafał Saltarski Apr 11 '14 at 11:59

3 Answers3

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);
            }
         }
Robert Lindgren
  • 24,520
  • 12
  • 53
  • 79
  • 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") ) – Robert Lindgren Jun 05 '13 at 07:11
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.

Waqas Sarwar MVP
  • 57,008
  • 17
  • 43
  • 79
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;

            }

        }
Tabares
  • 121
  • 6