Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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;
share|improve this question
    
By the way, current SPDisposeCheck fails to detect this undisposed SPWeb scenario. –  Alexey Krasheninnikov Jan 25 '10 at 9:23
add comment

2 Answers

up vote 11 down vote accepted

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

share|improve this answer
add comment

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;}
share|improve this answer
1  
fyi, depending on the type of item event, properties.ListItem could be null (i.e. ItemDeleted) –  Jaap Vossers Feb 2 '10 at 19:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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