private void InstallListsViewsWebPartsInstall(string viewTitle, bool installGrid, int arentMessageId, SPFile fileView, bool hasViewFile) { if (fileView == null) { throw new ArgumentNullException(nameof(fileView)); } using (var viewWebManager = fileView.GetLimitedWebPartManager(PersonalizationScope.Shared)) { if (hasViewFile) { SPFile tempFile = null; try { tempFile = oWeb.GetFile($"TempViewStorage/{viewTitle}.aspx"); var tempFileContents = tempFile.GetContents(); fileView.UpdateContentsAndSave(tempFileContents); var tempFileWebManager = tempFile.GetLimitedWebPartManager(PersonalizationScope.Shared); var arrWebParts = new ArrayList(); foreach (SPWebPart webPart in viewWebManager.WebParts) { if (webPart is XsltListViewWebPart) { webPart.Hidden = true; viewWebManager.SaveChanges(webPart); } if (webPart.GetType().ToString() != "Microsoft.SharePoint.WebPartPages.ErrorWebPart" && !(webPart is XsltListViewWebPart)) { arrWebParts.Add(webPart); } } foreach (SPWebPart webPart in arrWebParts) { viewWebManager.DeleteWebPart(webPart); } foreach (SPWebPart webPart in tempFileWebManager.WebParts) { if (webPart.GetType().ToString() != "Microsoft.SharePoint.WebPartPages.ErrorWebPart" && !(webPart is XsltListViewWebPart)) { viewWebManager.AddWebPart(webPart, webPart.ZoneID, webPart.ZoneIndex); } } ApplicationInstallerHelpers.ConnectWebPartConsumersToReportFilter(viewWebManager); addMessage(0, viewTitle, string.Empty, arentMessageId); } catch (Exception ex) { addMessage(ErrorLevels.Error, viewTitle, "Error: " + ex.Message, arentMessageId); Trace.WriteLine(ex.ToString()); } finally { tempFile?.Delete(); viewWebManager.Web.Dispose(); } } else { InstallListsViewsWebPartsInstall(viewTitle, installGrid, arentMessageId, viewWebManager); } } }