
332
CHAPTER 10 Building ScalaBle applicationS inSharepoint
object, many programmers assume that you must call SPList.Items.Add(). Actually, you may retrieve
an empty
SPListItemCollection without querying the database by calling SPList.GetItems() and
passing an
SPQuery object designed to return no rows. So to add an item to a SharePoint list, use the
following code instead:
SPListItem newItem = SPList.GetItems(new SPQuery(){Query = “0”}).Add();
This way, no matter how large your list becomes, you can always add new items without incurring a
performance penalty that increases as the list grows.
In addition to adding items, SharePoint programmers sometimes make the mistake of looking up the
number of items in the list using the following method:
SPList.Items.Count
As with the previous example, this code loads all items in the list before returning the count of those
items. To avoid this, instead make use of the
.ItemCount property:
int itemCount = SPList.ItemCount;
When querying list data, unless your code actually needs to iterate over every single item in the list,
you should always use CAML queries to retrieve the list items you need. Just as with list views, if
your query lters on an indexed eld, it performs much better. Furthermore, by specifying an item
limit with your query you can avoid running into query throttling limits.
If the list you try to query is extremely large (mi ...