Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

The list "Region" exists at the parent web but the following code throws error. Is there anything else or other way I could reference a list that is on parent site?

SPList regionList = web.ParentWeb.GetList("Region");
share|improve this question

2 Answers 2

The GetList method of a web object requires a url to the list. Here's what intellisense says:

Returns the list that is associated with the specified site-relative URL. A string that contains the site-relative URL for a list, for example, /Lists/Announcements.

So in your case, you may want to change your code snippet like the following:

SPList regionList = web.ParentWeb.GetList("/Lists/Region");
share|improve this answer
    
I am going to try this method in few and let you know. Thanks –  lu A Jun 13 '11 at 14:48

I believe the SPWeb.GetList method uses a server-relative URL to get the list see here (http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.getlist.aspx).

You could add a using statement for the web and then get the list by it's name from the SPWeb.Lists collection:

Using (SPWeb web = web.ParentWeb)
{ SPList list = web.Lists["Region"]; }

Thanks,

Alex

share|improve this answer
    
I am going to try this method in few and let you know. Thanks –  lu A Jun 13 '11 at 14:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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