The correct way to dispose of your new SPWeb is in the area where it was created. If you need to perform additional operations on your new website, just pass the call to the delegate:
public static void CreateSiteFromSTP(SPWeb parentWeb, string newSiteSTP, int teamId, Action<SPWeb> actionOnCreate) {
Then you can simply pass in a method (anonymous or named) that manages your new SPWeb , without responsibility for deletion. This approach also has the advantage that you are not returning SPWeb outside of your elevated block, which is unsupported and unreliable.
In fact, I would be surprised to find that your code really works the way you expect: existing SharePoint objects (in particular parentWeb ) have their permissions set when they were created, and also should not be transferred to an elevated context. The best approach for raising permissions in SharePoint is to use SPSite impersonation. Using the RunAsSystem method defined here , I would reorganize your code as follows:
public static void ElevateToCreateSiteFromSTP(SPWeb parentWeb, string newSiteSTP, int teamId, Action<SPWeb> actionOnCreate) { parentWeb.RunAsSystem(elevWeb => CreateSiteFromSTP(elevWeb, newSiteSTP, teamId, actionOnCreate)); } private static void CreateSiteFromSTP(SPWeb parentWeb, string newSiteSTP, int teamId, Action<SPWeb> actionOnCreate) { try { string siteUrl = teamId.ToString(); SPWebCollection webs = parentWeb.Webs; using(var newWeb = webs.Add(siteUrl, ...)) { var newWebFeatures = newWeb.Features; TraceProvider.WriteLine("Activating Feature : MembersFeature "); newWebFeatures.Add(new Guid(TeamSiteAttributes.MembersFeature), true); TraceProvider.WriteLine("Activating Feature : BadgeAwardsFeature "); newWebFeatures.Add(new Guid(TeamSiteAttributes.BadgeAwardsFeature), true); TraceProvider.WriteLine("Activating Feature : ProjectBenefitsFeature "); newWebFeatures.Add(new Guid(TeamSiteAttributes.ProjectBenefitsFeature), true); TraceProvider.WriteLine("Activating Feature : TeamScoreFeature "); newWebFeatures.Add(new Guid(TeamSiteAttributes.TeamScoreFeature), true); newWeb.Update(); parentWeb.Update(); if(actionOnCreate != null) actionOnCreate(newWeb); } } catch (Exception ex) { TraceProvider.WriteLine("Error", ex); throw; } }
This has the added benefit of decoupling your height problems from logic before creating SPWeb . I also prefer to do this very clearly when my code works with different resolutions.