MOSS Forum

 
Home » Forum » MOSS       Ask a questionRSS Feeds

List Access through Object Model

  Asked By: Tracie Bryan         Date: Dec 02, 2009      Category: MOSS      Views: 310
 

I have always faced this problem. While trying to access a list
through indexing, we normally use the following syntax.

SPSite site = new SPSite("url");
SPWeb web = site.OpenWeb();
SPList list = web.Lists["ListName"];


The 3rd line normally throws an ArgumentException if the ListName is wrong
or if such a list doesn't exist. I also tried GetList() and GetListFromUrl()
or so, also to access a list. But all these methods throw exceptions like
File Not Found and so on. Is there a better way to access a List through
Object Model? Please advise. I always have to put the above piece of code in
a Try..Catch block and catch the argument exception and suppress it. I
googled it and found some implementation using extension methods. But there
also, they have the Try..Catch kind of implementation. Isn't there a more
elegant way to check whether a list exists (other than the above approach or
looping through all lists and then finding the matching instance)?

Tagged:          

 

8 Answers Found

 
Answer #1       Answered By: Jocelyn Shelton          Answered On: Dec 02, 2009       

If you're on 2010, you can use the TryToGetList("ListName") method.
Otherwise I think you're stuck with Try...Catch or a loop.

 
Answer #2       Answered By: Joey Soto          Answered On: Dec 02, 2009       

Ah -- looks like someone wrote a function just for that purpose in SPExLib.

http://spexlib.codeplex.com/

 
Answer #3       Answered By: Gerard Randall          Answered On: Dec 02, 2009       

There are only really two ways to go at this.



1) Put the code you are using (or any of the variations that try to get
a list by name) in a Try/Catch block and deal with the exception

2) Get the collection of lists by accessing the lists property of the
web. Then use foreach to step through the lists until you find the one you
want by matching the name. this avoids an exception if the list isn't
found.



#1 is probably best if you have a large number of lists, but #2 can be
faster if you only have a limited number or lists.

 
Answer #4       Answered By: Audra Mccormick          Answered On: Dec 02, 2009       

What about this:

//Check if list exists
public static bool ListExists(SPWeb web, string listName)
{
return web.Lists.Cast<SPList>().Any(list =>
string.Equals(list.Title, listName));
}

 
Answer #5       Answered By: Christina Lewis          Answered On: Dec 02, 2009       

Try:

if (web.GetFolder("/Lists/listName").Exists)
{
//code here
}


Andy Burns' test for which of the other two accessors is faster:
www.novolocus.com/.../

According to
(www.sharepointdev.net/.../how-to-avoid--\
spwebliststasks-exception--value-does-not-fall-within-expected-range-46547.shtml\
) you can use this function to test if a list exists:

public static bool ListExists(SPWeb web, string listName)
{
return web.Lists.Cast<SPList>().Any(list => string.Equals(list.Title,
listName));
}

 
Answer #6       Answered By: Meaghan Webster          Answered On: Dec 02, 2009       

But if I had to check for Pages Library or any other
library, I will have to have a diff. url (as it would be only /Pages for
Pages library). So I would have to live with the looping approach itself.
Thanks a lot.

 
Answer #7       Answered By: Sunny Delacruz          Answered On: Dec 02, 2009       

But isn't it like looping through all lists in a
web using foreach loop, with the only difference being you are using LINQ?

 
Answer #8       Answered By: .net Guru          Answered On: Dec 02, 2009       

I believe so, yes. Just a shorter notation. You would want to follow
advice and use the Try/Catch block if you have a large collection of lists.

 
Didn't find what you were looking for? Find more on List Access through Object Model Or get search suggestion and latest updates.


Your Answer
  • Answer should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].