private static void ResetMappingFile(LockedMasterPageFilesInput masterPageFile) { if (masterPageFile == null) { return; } string setupFileDirName = masterPageFile.SetupFileDirName; string setupFileName = masterPageFile.SetupFileName; string setupFileExtension = masterPageFile.SetupFileExtension; string siteUrl = masterPageFile.SiteCollection; string webAppUrl = masterPageFile.WebApplication; string webUrl = masterPageFile.WebUrl; LockedMasterPageFilesOutput csvObject = new LockedMasterPageFilesOutput(); csvObject.SetupFileDirName = setupFileDirName; csvObject.SetupFileName = setupFileName; csvObject.WebApplication = webAppUrl; csvObject.WebUrl = webUrl; csvObject.ExecutionDateTime = DateTime.Now.ToString(); csvObject.Status = Constants.Success; // clean the inputs if (setupFileDirName.EndsWith("/")) { setupFileDirName = setupFileDirName.TrimEnd(new char[] { '/' }); } if (setupFileName.StartsWith("/")) { setupFileName = setupFileName.TrimStart(new char[] { '/' }); } if (webUrl.EndsWith("/")) { webUrl = webUrl.TrimEnd(new char[] { '/' }); } if (webAppUrl.EndsWith("/")) { webAppUrl = webAppUrl.TrimEnd(new char[] { '/' }); } // e.g., "https://ppeTeams.contoso.com/sites/test/_catalogs/masterpage/Sample.master" string targetFilePath = setupFileDirName + '/' + setupFileName; // e.g., "https://ppeTeams.contoso.com/sites/test/_catalogs/masterpage/__DeviceChannelMappings.aspx" string mappingFilePath = setupFileDirName + '/' + Constants.DeviceChannelMappingFileName; // e.g., "/_catalogs/masterpage/Sample.master" // e.g., "/_catalogs/masterpage/folder/Sample.master" // e.g., "/sites/testSite/_catalogs/masterpage/Sample.master" // e.g., "/sites/testSite/_catalogs/masterpage/folder/Sample.master" // e.g., "/sites/testSite/childWeb/_catalogs/masterpage/Sample.master" // e.g., "/sites/testSite/childWeb/_catalogs/masterpage/folder/Sample.master" string serverRelativeMappingFilePath = mappingFilePath.Substring(webAppUrl.Length); if (setupFileExtension.Equals("master", StringComparison.InvariantCultureIgnoreCase) == false) { // ignore anything that is not a master page file Logger.LogWarningMessage(String.Format("Skipping file [not a Master Page]: {0}", targetFilePath), true); return; } try { Logger.LogInfoMessage(String.Format("Processing File: {0} ...", targetFilePath), true); Logger.LogInfoMessage(String.Format(" setupFileDirName= {0} ...", setupFileDirName), false); Logger.LogInfoMessage(String.Format(" setupFileName= {0} ...", setupFileName), false); Logger.LogInfoMessage(String.Format(" targetFilePath= {0} ...", targetFilePath), false); Logger.LogInfoMessage(String.Format(" mappingFilePath= {0} ...", mappingFilePath), false); Logger.LogInfoMessage(String.Format(" serverRelativeFilePath= {0} ...", serverRelativeMappingFilePath), false); Logger.LogInfoMessage(String.Format(" webAppUrl= {0} ...", webAppUrl), false); Logger.LogInfoMessage(String.Format(" webUrl= {0} ...", webUrl), false); using (ClientContext userContext = Helper.CreateAuthenticatedUserContext(Program.AdminDomain, Program.AdminUsername, Program.AdminPassword, webUrl)) { Web web = userContext.Web; userContext.Load(web); userContext.ExecuteQuery(); // Get Device Channel Mapping File string originalFileContents = Helper.SafeGetFileAsString(web, serverRelativeMappingFilePath); if (String.IsNullOrEmpty(originalFileContents)) { // bail if Mapping file not found.. Logger.LogWarningMessage(String.Format("Skipping file: [{0}]; Mapping File not found: [{1}]", targetFilePath, serverRelativeMappingFilePath), true); return; } string tempFileContents = originalFileContents.ToLower(); if (tempFileContents.Contains(setupFileName.ToLower()) == false) { // bail if MP file not referenced in Mapping file.. Logger.LogWarningMessage(String.Format("Skipping file: [{0}]; Mapping File does not reference the master page file: [{1}]", targetFilePath, serverRelativeMappingFilePath), true); return; } // grab the current master page settings for the web. Helper.MasterPageInfo mpi = Helper.GetMasterPageInfo(web); string currentCustomMasterPageUrl = mpi.CustomMasterPageUrl; string currentCustomMasterPage = currentCustomMasterPageUrl.Substring(currentCustomMasterPageUrl.LastIndexOf("/") + 1); // Edit Device Channel Mapping File so it now references the correct/current master page files. // TODO: this is a case-sensitive operation; add case-insensitive logic if it becomes necessary... string updatedFileContents = originalFileContents.Replace(setupFileName, currentCustomMasterPage); // Did the case-sensitive replacement operation fail? tempFileContents = updatedFileContents.ToLower(); if (tempFileContents.Contains(setupFileName.ToLower()) == true) { // bail if replacement operation failed due to case-sensitivity Logger.LogErrorMessage(String.Format("ResetMappingFile() failed for file {0}: Error={1}", targetFilePath, "Casing of Master Page References in Mapping File does not match the casing of the Master Page [" + setupFileName + "] specified in the input file"), true ); Logger.LogWarningMessage(String.Format("Update the casing of the Master Page entry [{0}] of the input file to match the casing used in the Mapping File [Contents={1}]", setupFileName, originalFileContents), false); return; } Logger.LogInfoMessage(String.Format("Reset Mapping File [{0}] to reference [{1}]", serverRelativeMappingFilePath, currentCustomMasterPage), true); // Upload Modified Device Channel Mapping File Helper.UploadDeviceChannelMappingFile(web, serverRelativeMappingFilePath, updatedFileContents, "File reset by Transformation Console"); // Backup Original Device Channel Mapping File Helper.UploadDeviceChannelMappingFile(web, serverRelativeMappingFilePath + ".bak", originalFileContents, "Backup created by Transformation Console"); csvObject.MappingFile = serverRelativeMappingFilePath; csvObject.MappingBackup = serverRelativeMappingFilePath + ".bak"; csvObject.MappingMasterPageRef = currentCustomMasterPage; csvObject.Status = Constants.Success; FileUtility.WriteCsVintoFile(csvOutputFileSpec, csvObject, ref csvOutputFileHasHeader); } } catch (Exception ex) { Logger.LogErrorMessage(String.Format("ResetMappingFile() failed for file {0}: Error={1}", targetFilePath, ex.Message), true); ExceptionCsv.WriteException( webAppUrl, siteUrl, webUrl, "MappingFile", ex.Message, ex.ToString(), "ResetMappingFile", ex.GetType().ToString(), String.Format("ResetMappingFile() failed for file {0}", targetFilePath) ); } }