public LoadedDto LoadWebAppChildren(Location location, bool elevatedPrivileges) { // this variable will save all loaded information of all web applications var loadedElements = new LoadedDto(location); var spWebApp = SpLocationHelper.GetWebApplication(location.Id); if (spWebApp == null) { // The web application might have got deleted!? // throw? return(null); } var siCos = spWebApp.Sites; foreach (SPSite spSite in siCos) { Location siteLocation; //try //{ if (elevatedPrivileges) { siteLocation = SpSiteElevation.SelectAsSystem(spSite, SpConverter.ToLocation, location.UniqueId); } else { siteLocation = SpConverter.ToLocation(spSite, location.UniqueId); } // meaning that they are not installed in the farm, rather on site or web level IEnumerable <FeatureDefinition> nonFarmFeatureDefinitions; SPFeatureCollection siteFeatureCollection = SpFeatureHelper.GetFeatureCollection(spSite, elevatedPrivileges); var activatedSiteFeatures = siteFeatureCollection.ToActivatedFeatures(siteLocation, out nonFarmFeatureDefinitions); loadedElements.AddChild(siteLocation, activatedSiteFeatures, nonFarmFeatureDefinitions); if (siteLocation.LockState != LockState.NoAccess && spSite != null && spSite.AllWebs != null) { SPWebCollection allWebs; if (elevatedPrivileges) { allWebs = SpSiteElevation.SelectAsSystem(spSite, SpLocationHelper.GetAllWebs); } else { allWebs = spSite.AllWebs; } foreach (SPWeb spWeb in allWebs) { var webLocation = SpConverter.ToLocation(spWeb, siteLocation.UniqueId); nonFarmFeatureDefinitions = null; SPFeatureCollection webFeatureCollection = SpFeatureHelper.GetFeatureCollection(spWeb, elevatedPrivileges); var activatedWebFeatures = webFeatureCollection.ToActivatedFeatures(webLocation, out nonFarmFeatureDefinitions); loadedElements.AddChild(webLocation, activatedWebFeatures, nonFarmFeatureDefinitions); // https://blogs.technet.microsoft.com/stefan_gossner/2008/12/05/disposing-spweb-and-spsite-objects/ spWeb.Dispose(); } } spSite.Dispose(); // This is the right place for debugging exception handling of lock state of site collections //} //catch (Exception Ex) //{ // throw Ex; //} } return(loadedElements); }