1. First see the Difference between two? And then USE.
web.Lists[“Demo”] and web.GetList(“http://localhost:90/Demo/AllItems.aspx”) In my case http://usctap3452/sites/sa/SubCategory/AllItems.aspx 🙂
web.Lists[ListName] method :In this case, it loads the metadata* of the all lists in that specific SPWeb object. Then it does SPList.Title comparison with metadata of all the lists returned and then it returns the matching list from the SPWeb.Lists collection.
web.GetList(URL) method : In this case, first retrieves the list GUID from the url (database hit), then it loads the metadata for that specific list.
Conclusion : if u have a Large collection of lists in urs Site avoid using web.Lists[] method
2. Which one to use objList.Items.Count or objList.ItemsCount.
objList.Items.Count:First it will load all the SPListItems in the memory and then it will find out the total count.Bad Practice
objList.ItemsCount:ItemCount is a part of metadata of the SPList object and this will get generated whenver we create a SPList instance. So there is no any overburden to the list to find out its total number of list items.
Conclusion :Always use objList.ItemsCount
3. Never create Do not instantiate SPWeb, SPSite, SPList, or SPListItem objects within an
public override void ItemAdded(SPItemEventProperties properties)
{
using (SPSite objSite = new SPSite(properties.WebUrl))
{
using (SPWeb objWeb = objSite.OpenWeb())
{
SPList objList = objWeb.Lists[properties.ListId];
SPListItem objListItem = oList.GetItemByUniqueId(properties.ListItemId);
}
}
}
this is bad practice
public override void ItemAdded(SPItemEventProperties properties)
{
SPListItem oItem = properties.ListItem;
}
Conclusion :Always use second One