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?
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?
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);
}
}
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;
}
}
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.