1.     
Use
SPList.GetItems instead of SPList.Items
Apply Filters, specify only fields needed to make the query more efficient.
Apply pagination in increments of no more than 2000 items.
SPQuery query = new
SPQuery();
SPListItemCollection
spListItems ; 
string lastItemIdOnPage =
null; // Page position.
int itemCount = 2000
while (itemCount == 2000)
{
    // Include only the fields you will use.
    query.ViewFields = "<FieldRef
Name=\"ID\"/><FieldRef
Name=\"ContentTypeId\"/>";  
    query.RowLimit = 2000; // Only select the
top 2000.
    // Include items in a subfolder (if
necessary).
    query.ViewAttributes =
"Scope=\"Recursive\"";
    StringBuilder sb = new StringBuilder();
    // To make the query order by ID and stop
scanning the table, specify the OrderBy override attribute.
    sb.Append("<OrderBy Override=\"TRUE\"><FieldRef
Name=\"ID\"/></OrderBy>");
    //.. Append more text as necessary ..
    query.Query = sb.ToString();
    // Get 2,000 more items.
    SPListItemCollectionPosition pos = new
SPListItemCollectionPosition(lastItemIdOnPage);
    query.ListItemCollectionPosition = pos;
//Page info.
    spListItems = spList.GetItems(query);
    lastItemIdOnPage =
spListItems.ListItemCollectionPosition.PagingInfo;
    // Code to enumerate the spListItems.
    // If itemCount <2000, finish the
enumeration.
    itemCount = spListItems.Count;
}
2.     
Instead
of using SPList.Items.GetItemById,
use SPList.GetItemById(int
id, string field1, params string[] fields). Specify the item
identifier and the field that you want.
3.     
Use SPList.ItemCount instead of SPList.Items.Count.
4.     
Use SPList.GetItemByUniqueId(System.Guid)
instead of SPList.Items[System.Guid]
5.     
Use SPList.GetItemById(System.Int32) instead of SPList.Items[System.Int32]
6.     
Use
SPList.GetItemById(System.Int32) instead of SPList.Items.GetItemById(System.Int32)
7.     
Use SPWeb.Lists[GUID]
or SPWeb.GetList(strURL)
is preferable to using SPWeb.Lists[strDisplayName]. 
·        
Using
the GUID is preferable because it is unique, permanent, and requires only a
single database lookup.
·        
The
display name indexer retrieves the names of all the lists in the site and then
does a string comparison with them.
8.     
Use
DeleteByID method instead of Delete()
SPSite
site = new SPSite("site url");
SPWeb
web = site.OpenWeb();
SPList
list = web.Lists["custom list name"];
SPListItem
item = list.GetItemById(1);
SPFile
file = web.GetFile(item.Url);
SPFileVersionCollection
collection = file.Versions;
ArrayList
idList = new ArrayList();
foreach
(SPFileVersion ver in collection)
{
  idList.Add(ver.ID);
}
foreach
(int verID in idList)
{
try
{
  collection.DeleteByID(verID);
}
catch
(Exception ex)
{
  MessageBox.Show(ex.Message);  
}
}
 
 
No comments:
Post a Comment