TryGetList method in SharePoint 2010

Paul Pascha, 27 november 2009

There are many cases in which you want to retrieve a SPList object. In some cases you can’t just assume the list exists so before referencing it, you’d want to check this first. Within the WSS v3 object model there was no neat way to check for the existence of a list. Indexing into a SPListCollection based on for example the name of a non-existing list resulted in a System.NullReferenceException. For this reason, it was quite common to check for the existence of a SPList whitin a try-catch block.

In SharePoint 2010′s object model the SPListCollection Class has a new method called “TryGetList”. This method does exactly what the name suspects. Within the SPListCollection is being searched to a SPList with the specified name. If the list does not exist, a null reference is returned

SPSite currentSite = SPContext.Current.Site;
SPList mySPList = currentSite.RootWeb.Lists.TryGetList("VoorbeeldLijst");


if (mySPList != null)
{
  ...
  ...
}

Categorieën: Nieuws
Tags:

Geef een reactie

Jouw e-mailadres wordt niet gepubliceerd. Verplichte velden zijn gemarkeerd met *

*

De volgende HTML tags en attributen zijn toegestaan: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Categories

  • Tags

  • Blog Authors

    Amy
    anita
    Anneke
    Danny Burlage
    Dennis Vendel
    Freek Berson
    Gerard
    Iris
    Jasper Oosterveld
    Jean-Claude Chan
    Jorn
    Lab Chicks
    Luc Joziasse
    Maarten van Noort
    Maarten Wijsman
    Marlon
    Martijn Bellaard
    Natasja van Doorn
    Paul Pascha
    Peter Heuvelman
    Rick Slager
    Robert van Son
    Roeland Jimenez
    SanderZ
    Sjoerd Schudde
    Stefan van der Wiele
    Tim Heuperman
    Wortell
  • Archief