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

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);
            }
         }
  • 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 5 '13 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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