views:

240

answers:

1

Hi,

I have make a custom lookup field and i lookup to the contacts in my top level site. but i want to lookup to a subsite.

dataSource.List = site.RootWeb.Lists["Contacts"];

this is the code i used for the lookup but i reference to the rootweb and i don't want that. Someone?

thx

A: 

Assuming "site" is your current SPWeb, simply look at site.Webs or site.GetSubwebsForCurrentUser(). I believe the former will try to give you all subwebs even if the current user does not have access, so the latter is probably preferred.


Update - I later realized your question title indicated you want the parent web whereas the text of your question indicated you wanted a subweb. Above is a subweb. The parent web is simply site.ParentWeb (once again, assuming "site" is your SPWeb). You can always then check the IsRootWeb property if you care to check for that.


Yet another update - Based on your comment it looks like "site" may be a SPSite instead of a SPWeb. To get a web, you need to do something like site.OpenWeb() (now assuming "site" is a SPSite). It's best to do this within a C# using statement to dispose of the SPWeb propertly when you are done. Note that the OpenWeb method can take no parameters which works fine if the SPSite was instantiated with the full URL to your SPWeb. Or the OpenWeb can take a URL parameter.

Kirk Liemohn