Saturday, April 20, 2013
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.
- Remember that All Elevated Objects Must Remain Inside a RunWithElevatedPrivileges Block.
- All Elevated object which created in side the RunWithElevatedPrivileges not returned to outside of the RunWithElevatedPrivileges block.
- If the SPListItem object is passed outside of the RunWithElevatedPrivileges block, it retains its underlying SPRequest object and continues to be elevated. Code that expects to be running under the current user's credentials will have privilege elevation problems if it uses this SPListItem object.
always use using block with SPSite ,SPWeb like that
Guid GwebID = SPContext.Current.Web.ID;
Guid GsiteID = SPContext.Current.Site.ID;
using (SPSite site = new SPSite(GsiteID))
site.AllowUnsafeUpdates = true;
using (SPWeb web = site.OpenWeb(GwebID))
web.AllowUnsafeUpdates = true;
// Perform administrative actions
// Handle or re-throw an exception.
site.AllowUnsafeUpdates = false;