1

I am building an event receiver inside my sharepoint 2013 . the event receiver will fire when an item is updated. here is the related code:-

public override void ItemUpdated(SPItemEventProperties properties)
        {

   base.ItemUpdated(properties);
   SPWeb spCurrentSite = site.OpenWeb(currenweburl);
                        {

now I am doing the above steps to get a reference to the current Web.. so can i modify my above code to be as follow:-

    public override void ItemUpdated(SPItemEventProperties properties)
            {

   base.ItemUpdated(properties);
   SPWeb spCurrentSite = properties.Web;
                            {

as on both approaches i will get a reference to the current web , but using the second appraoch i have to write only a single line of code .. so can anyone advice if i can follow the second approach ?? or these 2 approaches are not the same ?

2

You should follow the 2nd approach. It is a best practice to use properties.Web inside the event receiver.

Do not instantiate an SPWeb, SPSite, SPList, or SPListItem object within an event receiver. Event receivers that instantiate these objects instead of using the instances passed via the event properties can cause the following issues:

1) Significant additional roundtrips to the database (one write operation can result in up to five additional roundtrips in each event receiver).

2) Calls to the Update method on these instances can cause subsequent Update calls in other registered event receivers to fail.

Bad code

using (SPSite site = new SPSite(properties.WebUrl))
{
using (SPWeb web = site.OpenWeb())
    {
    //your code
    }
}

Best practice

SPWeb web = properties.OpenWeb(); 
or 
SPWeb web = properties.Web; 

SPListItem item = properties.ListItem;

Reference - Best Practices with Event Receivers

So yes both these approaches will give you the same results, but the 2nd approach besides being single line is also the best practice. So, you should follow the 2nd approach.

  • thanks a lot for your reply. maybe it is my fault that i did not describe my question clearly (so i modified my question accordingly).. now my question was not about the differences between creating new instance of the SPWeb (which is not recommended for the reasons you mentioned) and between referencing it .. but my question is if there are any differences between using these 2 appraoches to get a reference of the current web inside my event receiver:- SPWeb spCurrentSite = properties.Web; and SPWeb spCurrentSite = properties.OpenWeb(currenweburl); ?or both approaches will be similar? – john Gu Jan 29 '17 at 1:07
  • can you please advice on my above comment? – john Gu Jan 30 '17 at 1:21
  • 1
    If you want to get hold of a subsite using GUID or URL (server-relative or site-relative) then you should use properties.OpenWeb, whereas properties.Web gives you the SPWeb associated with SPSite. But if they are the same , then it no different – Gautam Sheth Jan 30 '17 at 5:41
  • so you mean using properties.SPWeb i can only reference the site (root site or sub-site) where the event receiver have fired... but using properties.OpenWeb i can get a reference for any site (root site or subsite) even if the event receiver fired outside it ? – john Gu Jan 30 '17 at 12:32
  • 1
    Yes thats correct. – Gautam Sheth Jan 30 '17 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.