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;
}
}
}