views:

272

answers:

3

Hello All,

Its a kind of newbie question:

We have a web part with user controls and a SP list as data source.

For different kind of sites say team sites, publishing portals etc the path for the respective list would be different. eg: Team Site it would be http://<Server>/Lists/<List Name> and for Publishing portal with Localization http://<Server>/<Locale>/Lists/<List Name>.

How do you manage the web part to be working no matter where the list is placed in the site collection?

Please suggest.

+3  A: 

You should expose a property on the web part that that points to the URL of the list. You can then annotate that property so that SharePoint will automatically allow for the property to be set in the "Editor Zone". (IE the right hand panel that appears when you "Edit Web Part")

You can use the following attributes to affect the way SP renders/persists the setting:

  • Personalizable
  • WebBrowsable
  • WebDescription
  • WebDisplayName
  • Category
nikmd23
+1  A: 

Hi,

another way to solve you problem would be to let the web part iterate through all the site collection's websites (SPWeb) and search there for the required list.

If you're accessing the websites via the object model it doesn't matter whether a site is localized or not. You can retrieve all the lists of a website by using its SPWeb.Lists property.

Flo
That would cause a lot of performance issues on a site collection with hundreds or thousands of web sites. Implementing a cache mechanism for the URL would help, but better to design a solution taht doesn't have to iterate at every page request.
Magnus Johansson
Yeahm, you're right Magnus, a caching mechanism would be important on huge portals to speed the request up.
Flo
+3  A: 

On most web sites I have a Configuration list in the root of the site collection. The list consists of key/value pairs. It is restricted to administrator access only and stores settings such as this. As it's just another SharePoint list it's very easy to edit.

So the web part would execute an SPQuery over the Configuration list and look for a particular key (e.g. "List Data Source Location"). The URL would be returned and can then be used as the data source. There is no need to set the key or URL on every instance of the web part, although you may like to implement a default that can be overridden.

(As an aside, a better alternative to using your own Configuration list if you need to store more complex data structures is SPConfigStore.)

Alex Angas