SHAREPOITN 2010 Event Receivers Tip

Monday, April 8, 2013

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.

share this post
Share to Facebook Share to Twitter Share to Google+ Share to Stumble Upon Share to Evernote Share to Blogger Share to Email Share to Yahoo Messenger More...