views:

310

answers:

1

I want to get a item from a list without loading all the items of the list. I know I can do it by calling SPList.getElementbyId(myID), but if I don't know if this myID exists in the list, how do I verify it? Yes I could use

SPListItem myItem = myList.Items[myID];
if (myItem == null)
{
   // log that we don't have this item
}

However, this way, when I'm calling myList.Items, all the items of the list are loaded in the in myList object (and if I have 40,000 items it is realy a performace issue), so I would rather use:

SPListitem myItem = myList.getElementById(myID);

This way I will not call the Items property and we will no need to load those items. Now the issue is that the method getElementById will throw an exception if that ID does notexist in the list. It is not a solution to put it in a try/catch statment because we might want to verify several houndreds of IDs and handling all ther throws will lead again to a performance issue. Any ther ideas to check for the existence of a item with a given id?

+2  A: 

You can build a query with all the ids your checking run that and compare the returned list to the ids your checking and determine which are valid or not.

This way your only retrieving what you need while still finding those missing.

savageguy
Yea, SPQuery is the way. Your query could look like this: <Where><Eq><FieldRef Name='ID'/><Value Type='Counter'>" + ID + "</value></Eq></Where>
drax
And the querry will not load the whole content of Items?Thanks for the hint!Any other ideas would be apreciated.
Anatol