0
votes

I am working in sharepoint server 2010. My requirement is to access the all the lists from the current site.

In my sharepoint server I have these things below: 1) Sitecollection which contains sub-sites

Ex: http://servername:portNo/SiteA/default.aspx where SiteA is subsite.

2)WebApplication and sites under it.

Ex: http://servername:portNo/sites/ProjectA/default.aspx where ProjectA is a site under the webapplication

Now I need to get all the lists which is present in the current site for which I am using below code

SPSite SiteCollection = SPContext.Current.Site;
SPWeb web = SiteCollection.AllWebs[CurrentSiteAddress];            
List<SPList> listCollection = new List<SPList>();
SPListCollection **lists** = web.Lists;

The above code works when the current site is a sitecollection i.e main site and its sub-sites. However the code breaks when my current site is a site under web-application.

Exception Message: There is no Web named \"/sites/ProjectA\"."

But when debug below code,

SPListCollection lists = web.Lists;

lists collection does not get populated. But after some delay lists collection gets populated and works without any problem. Can anyone tell me what could be the reason for this? and How can I resolver this issue?

1

1 Answers

0
votes

try this:

string siteurl = SPContext.Current.Site.Url;
string weburl = SPContext.Current.Web.ServerRelativeUrl;

using (SPWeb safeWeb = new SPSite(siteurl).OpenWeb(weburl))
{
    SPListCollection lists = web.Lists;
    foreach (SPList list in lists)
    {
      //do whatever you want with your list(s)
    }
}