Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharepoint SPDisposeCheck

The SPDisposeCheck utility alerted me to an impproperly disposed SPWeb.Add call. As you can see below, the typical using(SPWeb NewWeb = webs.add(siteUrl ....) method would not work because of the RunWithElevatedPrivileges would make the return newWeb out of context.

By looking at the newWeb = webs.Add() line below, can anyone suggest a way to properly dispose the new SPWeb object? Thanks in advance.

public partial class SiteHelper                         
{                               
    public static SPWeb CreateSiteFromSTP(SPWeb parentWeb, string newSiteSTP, int teamId)   
    {                               
        try                     
        {                       
            SPWeb newWeb = null;        
            SPSecurity.RunWithElevatedPrivileges(delegate()    
            {                   
                string siteUrl = teamId.ToString();         
                SPWebCollection webs = parentWeb.Webs;      
                newWeb = webs.Add(siteUrl,.,.,.,);
                TraceProvider.WriteLine("Activating Feature : MembersFeature ");        
                newWeb.Features.Add(new Guid(TeamSiteAttributes.MembersFeature), true);     
                TraceProvider.WriteLine("Activating Feature : BadgeAwardsFeature ");        
                newWeb.Features.Add(new Guid(TeamSiteAttributes.BadgeAwardsFeature), true);     
                TraceProvider.WriteLine("Activating Feature : ProjectBenefitsFeature ");    
                newWeb.Features.Add(new Guid(TeamSiteAttributes.ProjectBenefitsFeature), true);     
                TraceProvider.WriteLine("Activating Feature : TeamScoreFeature ");          
                newWeb.Features.Add(new Guid(TeamSiteAttributes.TeamScoreFeature), true);   
                newWeb.Update();            
                parentWeb.Update();             
            });                     
            return newWeb;              
        }   
        catch (Exception ex)        
        {                       
            TraceProvider.WriteLine("Error", ex);           
            throw;
        }
    }   
} 
like image 937
user125176 Avatar asked Mar 01 '23 15:03

user125176


1 Answers

SPDisposeCheck is reporting this as an error because it's not smart enough to know what you are doing with newWeb once you've returned it from this method. As long as you dispose of newWeb after your call to CreateSiteFromSTP() then you won't have a memory leak.

If you are confident that you don't have a memory leak in this method, you can set SPDisposeCheck to ignore just this particular warning. Just add the following declaration (with the correct SPDisposeCheckID number you received) above your CreateSiteFromSTP method:

[SPDisposeCheckIgnore(SPDisposeCheckID.SPDisposeCheckID_110, "Caller will dispose")]
like image 159
Alex Angas Avatar answered Mar 18 '23 07:03

Alex Angas