Thursday, January 12, 2012

SP 2010 TryGetList()

 When we writing the code using  objectmodel to getting the items in a specific list.we specifi the list name,If that list is not exist in the site it throws null reference exception ,so to overcome this error in moss2010 TryGetList() introduced.

           SPSite Site = SPContext.Current.Site;
            SPWeb Web = Site.OpenWeb();
            SPList  List = Web.Lists["IowaStateUserList"];
           SPList List = Web.Lists.TryGetList("IowaStateUserList");
            if (List != null)
              {
                 Response.Write("List Exist");
              }
             else 
                 Response.Write("List not Exist");

 

No comments:

Post a Comment