public static LogErrorMessage ( string msg, bool toConsole = true ) : void | ||
msg | string | |
toConsole | bool | |
return | void |
public static void DoWork(string inputFileSpec) { Logger.OpenLog("CommentUDCXFileNodes"); Logger.LogInfoMessage(String.Format("Scan starting {0}", DateTime.Now.ToString()), true); Logger.LogInfoMessage(inputFileSpec, true); List <UdcxReportOutput> _WriteUDCList = null; Logger.LogInfoMessage(String.Format("AppSettings:"), true); Logger.LogInfoMessage(String.Format("- AppSettings[UseAppModel] = {0}", Program.UseAppModel), true); if (Program.UseAppModel == true) { Logger.LogInfoMessage(String.Format("- AppId = {0}", ConfigurationManager.AppSettings["ClientId"].ToString()), true); } else { string adminUsername = String.Format("{0}{1}", (String.IsNullOrEmpty(Program.AdminDomain) ? "" : String.Format("{0}\\", Program.AdminDomain)), Program.AdminUsername); Logger.LogInfoMessage(String.Format("- Admin Username = {0}", adminUsername), true); } IEnumerable <UdcxReportInput> udcxCSVRows = ImportCSV.ReadMatchingColumns <UdcxReportInput>(inputFileSpec, Constants.CsvDelimeter); if (udcxCSVRows != null) { try { var authRows = udcxCSVRows.Where(x => x.Authentication != null && x.Authentication != Constants.ErrorStatus && x.Authentication.Length > 0); if (authRows != null && authRows.Count() > 0) { _WriteUDCList = new List <UdcxReportOutput>(); Logger.LogInfoMessage(String.Format("Preparing to process a total of {0} Udcx Files ...", authRows.Count()), true); foreach (UdcxReportInput udcxFileInput in authRows) { CommentUDCXFileNode(udcxFileInput, _WriteUDCList); } GenerateStatusReport(_WriteUDCList); } else { Logger.LogInfoMessage("No UDCX File records with authentication nodes found in '" + inputFileSpec + "' File ", true); } } catch (Exception ex) { Logger.LogErrorMessage(String.Format("CommentUDCXFileNode() failed: Error={0}", ex.Message), true); } Logger.LogInfoMessage(String.Format("Scan completed {0}", DateTime.Now.ToString()), true); } else { Logger.LogInfoMessage("No UDCX File records found in '" + inputFileSpec + "' File ", true); } Logger.CloseLog(); }
private static string SafeGetFileAsString(Web web, string serverRelativeFilePath) { try { return(web.GetFileAsString(serverRelativeFilePath)); } catch (Exception ex) { Logger.LogErrorMessage(String.Format("SafeGetFileAsString() failed for File [{0}] of Web [{1}]: Error={2}", serverRelativeFilePath, web.Url, ex.Message), false); return(String.Empty); } }
// Parses the CSV input line and returns a string array where each entry corresponds to a field parsed from the line // Strips double quotes from those fields that contain commas: e.g., 123,"abc,def",456 public static string[] ParseInputLine(string inputLine) { try { if (inputLine.Contains('"')) { int pos1 = inputLine.IndexOf('"'); int pos2 = inputLine.IndexOf('"', pos1 + 1); string left = inputLine.Substring(0, pos1); left = left.Trim(new char[] { '"', ',' }); string center = inputLine.Substring(pos1, pos2 - pos1); center = center.TrimStart(new char[] { '"', ',' }); string right = inputLine.Substring(pos2); right = right.TrimStart(new char[] { '"', ',' }); List <string> result = new List <string>(); string[] temp = null; temp = ParseInputLine(left); if (temp.Length > 0) { result.AddRange(temp); } result.Add(center); temp = ParseInputLine(right); if (temp.Length > 0) { result.AddRange(temp); } return(result.ToArray()); } else { inputLine = inputLine.Trim(new char[] { ',' }); if (String.IsNullOrEmpty(inputLine)) { return(new string[0]); } return(inputLine.Split(',')); } } catch (Exception ex) { Logger.LogErrorMessage(String.Format("ParseInputLine() failed for [{0}]: Error={1}", inputLine, ex.Message), false); return(new string[0]); } }
public static void DoWork(string inputFileSpec) { Logger.OpenLog("CommentUDCXFileNodes"); Logger.LogInfoMessage(String.Format("Scan starting {0}", DateTime.Now.ToString()), true); Logger.LogInfoMessage(inputFileSpec, true); List <UdcxReportOutput> _WriteUDCList = null; IEnumerable <UdcxReportInput> udcxCSVRows = ImportCSV.ReadMatchingColumns <UdcxReportInput>(inputFileSpec, Constants.CsvDelimeter); if (udcxCSVRows != null) { try { var authRows = udcxCSVRows.Where(x => x.Authentication != null && x.Authentication != Constants.ErrorStatus && x.Authentication.Length > 0); if (authRows != null && authRows.Count() > 0) { _WriteUDCList = new List <UdcxReportOutput>(); Logger.LogInfoMessage(String.Format("Preparing to comment a total of {0} Udcx Files Nodes ...", authRows.Count()), true); foreach (UdcxReportInput udcxFileInput in authRows) { CommentUDCXFileNode(udcxFileInput, _WriteUDCList); } GenerateStatusReport(_WriteUDCList); } else { Logger.LogInfoMessage("No valid authentication records found in '" + inputFileSpec + "' File ", true); } } catch (Exception ex) { Logger.LogErrorMessage(String.Format("CommentUDCXFileNode() failed: Error={0}", ex.Message), true); } Logger.LogInfoMessage(String.Format("Scan completed {0}", DateTime.Now.ToString()), true); } else { Logger.LogInfoMessage("No records found in '" + inputFileSpec + "' File ", true); } Logger.CloseLog(); }
public static string[] ReadInputFile(string inputFileSpec, bool hasHeader) { try { if (hasHeader == true) { // remove the header row from the resulting string array... List <string> temp = new List <string>(System.IO.File.ReadAllLines(inputFileSpec)); temp.RemoveAt(0); return(temp.ToArray()); } else { return(System.IO.File.ReadAllLines(inputFileSpec)); } } catch (Exception ex) { Logger.LogErrorMessage(String.Format("ReadInputFile() failed for {0}: Error={1}", inputFileSpec, ex.Message), true); return(new string[0]); } }
private static void CommentUDCXFileNode(UdcxReportInput udcxFileInput, List <UdcxReportOutput> _WriteUDCList) { if (udcxFileInput == null) { return; } string siteUrl = udcxFileInput.SiteUrl; string webUrl = udcxFileInput.WebUrl; string dirName = udcxFileInput.DirName; string leafName = udcxFileInput.LeafName; string authentication = udcxFileInput.Authentication; UdcxReportOutput udcxOutput = new UdcxReportOutput(); udcxOutput.SiteUrl = siteUrl; udcxOutput.WebUrl = webUrl; udcxOutput.DirName = dirName; udcxOutput.LeafName = leafName; udcxOutput.Authentication = authentication; if (dirName.EndsWith("/")) { dirName = dirName.TrimEnd(new char[] { '/' }); } if (leafName.StartsWith("/")) { leafName = leafName.TrimStart(new char[] { '/' }); } string serverRelativeFolderPath = "/" + dirName; string serverRelativeFilePath = "/" + dirName + '/' + leafName; try { Logger.LogInfoMessage(String.Format("Processing UCDX File [{0}/{1}] of Web [{2}] ...", dirName, leafName, webUrl), true); // IMPORTANT: Open the webUrl, not the siteUrl, so Folder.Files.Add() can properly process files of child webs using (ClientContext userContext = Helper.CreateClientContextBasedOnAuthMode(Program.UseAppModel, Program.AdminDomain, Program.AdminUsername, Program.AdminPassword, webUrl)) { Web web = userContext.Web; userContext.Load(web); userContext.ExecuteQuery(); XNamespace xns = "http://schemas.microsoft.com/office/infopath/2006/udc"; XDocument doc = null; Logger.LogInfoMessage(String.Format("Getting contents of UCDX File [{0}] from Web [{1}] ...", serverRelativeFilePath, webUrl), false); // Approach to read File contents depends on Auth Model chosen if (Program.UseAppModel == true) { string originalFileContents = SafeGetFileAsString(web, serverRelativeFilePath); if (String.IsNullOrEmpty(originalFileContents)) { Logger.LogErrorMessage(String.Format("Could not get contents of UCDX File"), false); udcxOutput.Status = Constants.ErrorStatus + ": could not get file contents."; _WriteUDCList.Add(udcxOutput); return; } doc = XDocument.Load(new StringReader(originalFileContents)); } else { FileInformation info = Microsoft.SharePoint.Client.File.OpenBinaryDirect(userContext, serverRelativeFilePath); doc = XDocument.Load(XmlReader.Create(info.Stream)); } Logger.LogInfoMessage(String.Format("Got contents of UCDX File"), false); XElement authElem = doc.Root.Element(xns + "ConnectionInfo").Element(xns + "Authentication"); if (authElem != null) { string authData = authElem.ToString(); authData = authData.Replace("<udc:Authentication xmlns:udc=\"" + xns + "\">", "<udc:Authentication>"); authElem.ReplaceWith(new XComment(authData)); string saveUdcxContent = doc.Declaration.ToString() + doc.ToString(); using (MemoryStream contentStream = new MemoryStream()) { StreamWriter writer = new StreamWriter(contentStream); writer.Write(saveUdcxContent); writer.Flush(); contentStream.Position = 0; Logger.LogInfoMessage(String.Format("Saving contents of UCDX File [{0}] to Web [{1}] ...", serverRelativeFilePath, webUrl), false); // Approach to save File contents depends on Auth Model chosen if (Program.UseAppModel == true) { Folder targetFolder = null; // grab the parent folder in preparation for the file upload... Logger.LogInfoMessage(String.Format("Getting folder [{0}] of Web [{1}] ...", serverRelativeFolderPath, webUrl), false); try { targetFolder = web.GetFolderByServerRelativeUrl(serverRelativeFolderPath); userContext.Load(targetFolder); userContext.ExecuteQuery(); Logger.LogInfoMessage(String.Format("Got folder"), false); } catch (Exception ex) { Logger.LogErrorMessage(String.Format("CommentUDCXFileNode() failed for UDCX File [{0}/{1}] of Web [{2}]: Reason={3}; Error={4}", dirName, leafName, webUrl, "Upload Folder was not Found.", "[" + ex.Message + "] | [" + ex.HResult + "] | [" + ex.Source + "] | [" + ex.StackTrace + "] | [" + ex.TargetSite + "]"), false); udcxOutput.Status = Constants.ErrorStatus + ": Upload Folder was not Found."; _WriteUDCList.Add(udcxOutput); return; } // check-out the file (if needed) in preparation for the file upload... try { Logger.LogInfoMessage(String.Format("Checking out file [{0}] ...", leafName), false); web.CheckOutFile(serverRelativeFilePath); Logger.LogInfoMessage(String.Format("Checked out file"), false); } catch (Exception ex) { Logger.LogErrorMessage(String.Format("CommentUDCXFileNode() failed for UDCX File [{0}/{1}] of Web [{2}]: Reason={3}; Error={4}", dirName, leafName, webUrl, "File Checkout failed.", "[" + ex.Message + "] | [" + ex.HResult + "] | [" + ex.Source + "] | [" + ex.StackTrace + "] | [" + ex.TargetSite + "]"), false); udcxOutput.Status = Constants.ErrorStatus + ": File Checkout failed."; _WriteUDCList.Add(udcxOutput); return; } // upload the modified file... Microsoft.SharePoint.Client.File targetFile = null; Logger.LogInfoMessage(String.Format("Uploading file [{0}] ...", leafName), false); try { targetFile = targetFolder.UploadFile(leafName, contentStream, true); Logger.LogInfoMessage(String.Format("Uploaded file"), false); } catch (Exception ex) { Logger.LogErrorMessage(String.Format("CommentUDCXFileNode() failed for UDCX File [{0}/{1}] of Web [{2}]: Reason={3}; Error={4}", dirName, leafName, webUrl, "File Upload failed.", "[" + ex.Message + "] | [" + ex.HResult + "] | [" + ex.Source + "] | [" + ex.StackTrace + "] | [" + ex.TargetSite + "]"), false); udcxOutput.Status = Constants.ErrorStatus + ": File Upload failed."; _WriteUDCList.Add(udcxOutput); return; } // publish the modified file (executes check-in, publish, and approval as needed)... try { Logger.LogInfoMessage(String.Format("Publishing file [{0}] ...", leafName), false); targetFile.PublishFileToLevel(FileLevel.Published); Logger.LogInfoMessage(String.Format("Published file"), false); } catch (Exception ex) { Logger.LogErrorMessage(String.Format("CommentUDCXFileNode() failed for UDCX File [{0}/{1}] of Web [{2}]: Reason={3}; Error={4}", dirName, leafName, webUrl, "File Publish failed.", "[" + ex.Message + "] | [" + ex.HResult + "] | [" + ex.Source + "] | [" + ex.StackTrace + "] | [" + ex.TargetSite + "]"), false); udcxOutput.Status = Constants.ErrorStatus + ": File Publish failed."; _WriteUDCList.Add(udcxOutput); return; } } else { Microsoft.SharePoint.Client.File.SaveBinaryDirect(userContext, serverRelativeFilePath, contentStream, true); } Logger.LogInfoMessage(String.Format("Saved contents of UCDX File [{0}] to Web [{1}]", serverRelativeFilePath, webUrl), false); udcxOutput.Status = Constants.SuccessStatus; Logger.LogSuccessMessage(String.Format("Updated UCDX File [{0}/{1}] of Web [{2}]", dirName, leafName, webUrl), false); } } else { udcxOutput.Status = Constants.NoAuthNodeFound; Logger.LogWarningMessage(String.Format("Skipped UCDX File [{0}/{1}] of Web [{2}]: Reason={3}", dirName, leafName, webUrl, Constants.NoAuthNodeFound), false); } } } catch (Exception ex) { Logger.LogErrorMessage(String.Format("CommentUDCXFileNode() failed for UDCX File [{0}/{1}] of Web [{2}]: Error={3}", dirName, leafName, webUrl, ex.Message), false); udcxOutput.Status = Constants.ErrorStatus; udcxOutput.ErrorDetails = ex.Message; } _WriteUDCList.Add(udcxOutput); }