11

I am developing an event receiver, using the ItemUpdating event, and I noticed that the properties parameter has an "OpenWeb" method, with this description:

Retrieves the Web site that is associated with the event that occurred.

while it also has a "Web" property, with this description:

Gets the parent Web site of the list associated with the event that occurred.

what is the difference?

10

The only difference is that properties.Web is cached for the duration of the event (after the first access to it).

So properties.Web is implemented to check if it has a cached SPWeb if so return that if not call properties.OpenWeb() and cache the result.

Neither of them will cause a memory leak as they are created from an SPSite which is disposed at the end of the event

  • so.. i should always use Web, to avoid getting an SPWeb that's already cached? what's the point of having OpenWeb be public then? – Nacht May 29 '12 at 22:53
  • 2
    OpenWeb is mainly for backwards compability. Web didn't exist in WSS 3.0 – Per Jakobsen May 30 '12 at 6:53
  • Why is OpenWeb not deprecated then? – Nacht Mar 20 '13 at 22:28
  • SharePoint don't generally deprecate things just because there now is a better option – Per Jakobsen Mar 21 '13 at 7:20
0

Please check this post https://stackoverflow.com/questions/8786383/what-the-difference-between-spcontext-current-web-site-openweb-listslist

On the same line check http://msdn.microsoft.com/en-us/library/ee724407.aspx

So using SPWeb web = properties.OpenWeb(); is much more efficient.

  • i don't see that conclusion at all! the first link says don't use OpenWeb, as it will leak memory, and the second link says nearly the same thing. Also, neither of those show a time where both OpenWeb() and Web are available from the same object. – Nacht May 29 '12 at 6:36

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.