Monday, April 8, 2013

SHAREPOITN 2010 Event Receivers Tip

don't  instantiate  SPWeb,SPSite,SPlist or SPListItem Objects with in event reciver. instantiating these Objects in side the event Receiver can cost you  more roundtrips to the database.



don't code like that



  using (SPSite osite = new SPSite(properties.WebUrl))
    {
    using (SPWeb sweb = osite.OpenWeb())
        {
        SPList list = sweb.Lists[properties.ListId];
        SPListItem item = list.GetItemByUniqueId(properties.ListItemId);
     
        }
    }



Code like that

   // Retrieve SPWeb and SPListItem from SPItemEventProperties instead of

// from a new instance of SPSite.
SPWeb web = properties.OpenWeb();
// Operate on the SPWeb object.
SPListItem item = properties.ListItem;
// Operate on an item.

No comments:

Post a Comment