7

Does properties.OpenWeb() create a new SPRequest object or does it refer to the existing SPWeb object already there in the properties object? Should we manually dispose of the SPWeb obtained in this manner?

Is the second line safer?

        SPWeb _web = properties.OpenWeb();
        SPWeb _web = properties.ListItem.ParentList.ParentWeb;
  • By the way, current SPDisposeCheck fails to detect this undisposed SPWeb scenario. – Alexey Krasheninnikov Jan 25 '10 at 9:23
13

In SharePoint 2007 you need to dispose the returned SPWeb if you use properties.OpenWeb()

In SP2010 you should use the SPItemEventProperties.Web property instead. This performs better than OpenWeb and has no dispose issues.

hth Anders Rask

0

Should nobody prove me wrong, I shall presume (until I resort to ILDASM / Reflector) that the safe way in ItemEventReceivers would be to use

SPWeb _web = properties.ListItem.ParentList.ParentWeb;

Surely,

if (properties.ListItem != null) SPWeb _web = properties.ListItem.ParentList.ParentWeb;

or

bool _shouldDispose = false;
try {_web = properties.ListItem.ParentList.ParentWeb;} catch (SPException ex) {_web = properties.OpenWeb(); _shouldDispose=true;}
  • 2
    fyi, depending on the type of item event, properties.ListItem could be null (i.e. ItemDeleted) – Jaap Vossers Feb 2 '10 at 19:31

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.