0

I am getting a warning from the spdisposecheck in one method I executed on the feature activated event.

private void CopyAttendees(SPWeb currentWeb)
  {
   try
    {
      SPWeb parentWeb = currentWeb.ParentWeb;
      SPList parentWebAttendeeList = parentWeb.GetSafeListByName(MeetingsCommon.Constants.LISTS_ATTENDEES_NAME);
      SPList currentWebAttendeeList = currentWeb.GetSafeListByName(MeetingsCommon.Constants.LISTS_ATTENDEES_NAME);
      foreach (SPListItem itemParentWebAttendeeList in parentWebAttendeeList.Items)
       {
        SPListItem itemCurrentWebAttendeeList = currentWebAttendeeList.Items.Add();
        itemParentWebAttendeeList.MapFieldValue(MeetingsCommon.Constants.FIELDS_TEXT_TITLE_NAME, itemCurrentWebAttendeeList);
        itemParentWebAttendeeList.MapFieldValue(MeetingsCommon.Constants.FIELDS_ATTENDEEROLE_NAME, itemCurrentWebAttendeeList);
        itemParentWebAttendeeList.MapFieldValue(MeetingsCommon.Constants.FIELDS_ATTENDEEATTENDANCE_NAME, itemCurrentWebAttendeeList);
        itemCurrentWebAttendeeList.SystemUpdate();
       }
     }
   catch (Exception ex)
    {
     throw;
    }
 }

 public override void FeatureActivated(SPFeatureReceiverProperties properties)
   {                
      Logger.LogDebug("NLMeetingsSiteConfigNLBDMeetingEventReceiver", "FeatureActivated(SPFeatureReceiverProperties properties)", "BEGIN");
      try
        {
          string siteUrl = (properties.Feature.Parent as SPWeb).Url;
          SPSecurity.RunWithElevatedPrivileges(() =>
           {
             using (SPSite currentSite = new SPSite(siteUrl))
              {
                using (SPWeb currentWeb = currentSite.OpenWeb())
                 {
                   SetCustomMasterPage(currentWeb);
                   ModifyHomePage(currentWeb);
                   CopyAttendees(currentWeb);
                   SetSecurity(currentWeb);
                   ConfigureWeb(currentWeb);
                 }
              }
           }
        }
    }

2 Answers 2

2

In this post you can find this:

An earlier version of this article recommended that the calling application should dispose of the SPWeb.ParentWeb. This is no longer the official guidance. The dispose cleanup is handled automatically by the SharePoint framework.

Also you can find this note:

This best practice addresses the issue identified by the SharePoint Dispose Checker Tool as SPDisposeCheckID_170.

and this example:

using (SPSite site = new SPSite(http://localhost)) 
{
    using (SPWeb web = site.OpenWeb())
    {
        SPList list = web.Lists["Announcements"];
        SPWeb parentWeb = list.ParentWeb; //No explicit dispose required.
    }
}

As you can see some times dispose checker works incorrect.

0

In your example you cannot do any harm in disposing of the web. The golden rule is "Did you create the web", i.e. you have based everything off a brand new site/web created on the URL of the feature parent which is a safe way of doing things. Any further webs can be safely disposed of because they only exist due to your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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