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");
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communityThe 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");
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");
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