2

I have created a Site Named Application Test. In this site i again Create a Sub site Named Sub Application I add in site url "http://SP2010:2222/" in Visual Studio Project Project Scope is Site Now i when i try to access the List(TestList) From Sub site(Sub Application) it give error List Does not Exist. Following Code to Access the List(TestList):

using (SPSite sites = new SPSite(SPContext.Current.Site.Url))
    {
        using (SPWeb web = sites.OpenWeb())
        {
          SPList CurrentList = web.Lists["TestList"]; // Error comes here
        }
    }

anybody have solution?

5

You first have to get the SPWeb object related to the sub site.

Something like using(SPWeb web = sites.OpenWeb("subsiteurl")) may help.

Your code is actually trying to get the TestList at the root web.

  • i don't have static url for subsite. – Vikalp Kapadiya Jul 11 '12 at 8:48
  • you have to get the url using another way in this case. Maybe you should update your question to show more background (what do you want to achieve) – Steve B Jul 11 '12 at 8:51
2

A new method ‘TryGetList’ is introduced in SharePoint 2010 to check whether the list exists or not.

Try the following code:

SPSecurity.RunWithElevatedPrivileges(() =>
{
    using (SPSite oSite = new SPSite(SPContext.Current.Web.Url))
        using (SPWeb oWeb = oSite.OpenWeb())
        {
            SPList myList = oWeb.Lists.TryGetList(listName);

            if (myList != null)
            {
            //Stmts
            }   
        }
});
0

According to your question i got the below things, 1) You have created a site "Application Test". In this site you created anothe site "SubApplication". No my question is "Application Test" site url is "http://sp2010:2222"?

2) If you are running the code in "Application Test" site level, then the thing is when ever "SPWeb web = sites.OpenWeb()" code it opens the Root web in the site. To open the site "Sub Application" you have to write "SPWeb web = sites.OpenWeb('[SubApplicationURL]')

3) Test list is in "ApplicationTest" site or "Sub Application"?

4) If it is in "ApplicationTest", and your code is running in "SubApplication" level, you have to write the code to access the parent web and then need to acces the list in that site.

5) Permissions are also concerns to access the list. Run the code with elevated previlizes will help to not to get permission issues.

  • (1) yes Application Test Url is "sp2010:2222" (2) Scope is site level (3) TestList is in "Sub Application" (5) its already done with administrator privileges – Vikalp Kapadiya Jul 12 '12 at 4:51
  • 1
    Hi vikalp, Sorry for the late replay. So you are in site level, then you need to access a list in sub site level, you have to open that sub site and then need to access the list. Code is SpWEB web=ROOTSITE.OpenWeb(["SubApplisctionURL"]); here ROOT site is site collection, Then get the list SPLIST list=web.lists["TestList"] – suresh Jul 16 '12 at 5:00
0

Try this, this should work. I have tested this code, myself:

string _siteUrl = SPContext.Current.Site.Url + "/mysubsite";
using (SPSite siteColl = new SPSite(_siteUrl))
{
    using (SPWeb web = siteColl.OpenWeb())
    {
       // SPList CurrentList = web.Lists["TestList"]; // This will not work
       // web.Lists[] must have a GUID, or use web.GetList("/Lists/MyList");
        Guid guid = new Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
        SPList list = web.Lists[guid];
        SPQuery query = new SPQuery();
        query.Query = "<Where><Eq><FieldRef Name='ID'/><Value Type='Number'>1</Value></Eq></Where>";
        SPListItemCollection listCol = list.GetItems(query);
        if (listCol != null)
        {
            Response.Write(listCol[0]["Title"].ToString());
        }
    }
}
-1

code you have written will try to find list from current web only so try this code:

string webURL = "/<webname>";

using (SPWeb web = site.OpenWeb(webURL))
{
    // and as per best practices you should use web.Lists.TryGetList(ListName);
    // It will increase performance.
} 

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.