static void Main(string[] args) { int serverindex = 0; string[] servers = Directory.GetFiles(".\\", "server*.cfg"); string user = string.Empty; string pass = string.Empty; string url = string.Empty; GetServerLogin(servers[serverindex], out user, out pass, out url); DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("Menu", typeof(string))); dt.Rows.Add(new object[] { "Browse" }); dt.Rows.Add(new object[] { "Query" }); dt.Rows.Add(new object[] { "Search" }); dt.Rows.Add(new object[] { "Utility" }); dt.Rows.Add(new object[] { "Solutions" }); dt.Rows.Add(new object[] { "Switch Server" }); Result r = new Result() { Data = dt, Header = "Pointed to server " + url }; while (true) { ResultResponse returnCode = r.Draw(); if (returnCode.ConsoleKey == ConsoleKey.Escape) { break; } int itemNumber = -1; if (int.TryParse(returnCode.Response, out itemNumber)) { itemNumber--; string command = (string)dt.DefaultView[itemNumber][0]; switch (command) { case "Browse": Console.Write("\r\nLoading Entities"); Console.CursorLeft = 0; Browser b = new Browser(new Connection(user, pass, url)); b.Begin(); break; case "Query": Query q = new Query(new Connection(user, pass, url)); q.Begin(); break; case "Search": break; case "Utility": Utility u = new Utility(new Connection(user, pass, url)); u.Begin(); break; case "Solutions": Solutions s = new Solutions(new Connection(user, pass, url)); s.Begin(); break; case "Switch Server": serverindex++; if (serverindex > servers.GetUpperBound(0)) { serverindex = 0; } GetServerLogin(servers[serverindex], out user, out pass, out url); r.Header = "Pointed to server " + url; break; default: break; } } } Console.WriteLine("done."); }
public void Begin() { data.Push(new Result() { Data = LoadData(), LogicalName = "querylist", Header = "Query List" }); ResultResponse returnCode = new ResultResponse(); while (true) { bool breakout = false; if (returnCode.ConsoleKey != ConsoleKey.F1) { returnCode = data.Peek().Draw(); } else { returnCode = data.Peek().Draw("F2: Paste New F8: Save As Selection: "); } switch (returnCode.ConsoleKey) { case ConsoleKey.Escape: breakout = true; break; case ConsoleKey.F2: Console.WriteLine("Paste Query:"); StringBuilder newquery = new StringBuilder(); while (true) { string line = Console.ReadLine(); newquery.AppendLine(line); if (line.ToLower().Contains("</fetch>")) { break; } } DataTable newQueryData = Util.GetQueryDataTable(newquery.ToString(), connection); Result newQueryResult = new Result() { Data = newQueryData }; returnCode = newQueryResult.Draw(); switch (returnCode.ConsoleKey) { case ConsoleKey.F8: Console.Write("\r\nFilename to save: "); string filename = Console.ReadLine(); StreamWriter sw = File.CreateText(filename + ".xml"); sw.WriteLine(newquery); sw.Flush(); sw.Close(); breakout = true; break; default: break; } break; default: int returnrow = 0; if (int.TryParse(returnCode.Response, out returnrow)) { if (returnrow >= 1 && returnrow <= data.Peek().Data.Rows.Count) { string query = string.Empty; StreamReader sr = File.OpenText((string)data.Peek().Data.Rows[returnrow - 1]["Queries"] + ".xml"); query = sr.ReadToEnd(); sr.Close(); Regex regex = new Regex(@"\{[\w ]+\}"); MatchCollection matchCollection = regex.Matches(query); if (matchCollection.Count > 0) { Console.WriteLine("\r\nQuery Variables:"); foreach (Match match in matchCollection) { Console.Write(match.Value.Substring(1, match.Value.Length - 2) + ": "); string value = Console.ReadLine(); query = query.Replace(match.Value, value); } } DataTable queryData = Util.GetQueryDataTable(query.ToString(), connection); Result queryResult = new Result() { Data = queryData, LogicalName = queryData.TableName }; returnCode = queryResult.Draw(); if (returnCode.ConsoleKey == ConsoleKey.Delete) { File.Delete((string)data.Peek().Data.Rows[returnrow - 1]["Queries"] + ".xml"); breakout = true; } } } break; } if (breakout) { break; } } }
public void Begin() { data = new Stack <Result>(); data.Push(LoadData()); while (true) { bool breakout = false; ResultResponse returnCode = data.Peek().Draw(); switch (returnCode.ConsoleKey) { case ConsoleKey.Escape: breakout = true; break; default: int selection = -1; if (int.TryParse(returnCode.Response, out selection)) { switch (data.Count) { case 1: Console.Write("\r\nExporting solution? ([U]nmanaged/[M]anaged/[C]ancel): "); string answer = Console.ReadLine(); if (string.IsNullOrEmpty(answer) || answer.ToUpper().Trim().StartsWith("U") || answer.ToUpper().Trim().StartsWith("M")) { ExportSolutionRequest exportSolutionRequest = new ExportSolutionRequest(); exportSolutionRequest.Managed = answer.ToUpper().Trim().StartsWith("M"); exportSolutionRequest.SolutionName = (string)data.Peek().Data.Rows[selection - 1]["Logical Name"]; // solution.UniqueName; ExportSolutionResponse exportSolutionResponse = null; try { exportSolutionResponse = (ExportSolutionResponse)connection.OrganizationService.Execute(exportSolutionRequest); byte[] exportXml = exportSolutionResponse.ExportSolutionFile; string filename = (string)data.Peek().Data.Rows[selection - 1]["Logical Name"] + (exportSolutionRequest.Managed ? "-managed" : "-unmanaged") + ".zip"; File.WriteAllBytes(filename, exportXml); Console.WriteLine("Solution exported to {0}.", filename); Console.WriteLine("\r\nEnter to continue"); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine("\r\nEnter to continue"); Console.ReadLine(); } } break; default: break; } } break; } if (breakout) { break; } } }
public void Begin() { data = new Stack <Result>(); cache = new Dictionary <string, DataTable>(); data.Push(new Result(LoadEntityData())); ResultResponse returnCode = new ResultResponse(); while (data.Count > 0) { if (returnCode.ConsoleKey != ConsoleKey.F1) { returnCode = data.Peek().Draw(); } else { returnCode = data.Peek().Draw("F2: Read F3: Search F4: Open F5: Refresh Selection: "); } string tableType = data.Peek().Data.TableName; if (tableType.Contains(" ")) { tableType = tableType.Substring(0, tableType.IndexOf(" ")); } switch (returnCode.ConsoleKey) { case ConsoleKey.Escape: data.Pop(); break; case ConsoleKey.F1: break; case ConsoleKey.F2: //read if (tableType == "Entity") { string entityToRead = data.Peek().Data.TableName.Substring(7).Replace("(cached)", ""); Console.Write("\r\nGUID, index or Enter: "); string sGuid = Console.ReadLine(); Guid recordID = Guid.Empty; int recordNumber = 0; if (Guid.TryParse(sGuid, out recordID)) { data.Push(new Result(GetRecord(entityToRead, recordID)) { LogicalName = entityToRead }); } else if (int.TryParse(sGuid, out recordNumber)) { data.Push(new Result(GetRecord(entityToRead, Math.Max(0, recordNumber))) { LogicalName = entityToRead }); } else { data.Push(new Result(GetRecord(entityToRead, 1)) { LogicalName = entityToRead }); } } break; case ConsoleKey.F3: //search if (returnCode.Modifiers == 0 && tableType == "Entity") { Console.Write("\r\nAttribute index to search: "); string searchAttributeString = Console.ReadLine(); int searchAttributeIndex = -1; if (int.TryParse(searchAttributeString, out searchAttributeIndex)) { string searchEntity = data.Peek().Data.TableName.Substring(7).Replace("(cached)", ""); string searchAttribute = (string)data.Peek().Data.DefaultView[searchAttributeIndex - 1]["LogicalName"]; string searchAttributeDataType = (string)data.Peek().Data.DefaultView[searchAttributeIndex - 1]["DataType"]; Console.WriteLine(" searching attribute {0}({1})", searchAttribute, searchAttributeDataType); Console.Write("Value to search for: "); string searchValue = Console.ReadLine(); RetrieveEntityRequest retrieveEntityRequest = new RetrieveEntityRequest() { EntityFilters = EntityFilters.Attributes, LogicalName = searchEntity }; RetrieveEntityResponse retrieveEntityResponse = (RetrieveEntityResponse)connection.OrganizationService.Execute(retrieveEntityRequest); EntityMetadata em = (EntityMetadata)retrieveEntityResponse.Results["EntityMetadata"]; string primaryIDAttribute = em.PrimaryIdAttribute; string primaryIDName = em.PrimaryNameAttribute; string filterOperator = "like"; string fetchXml = string.Format(@" <fetch> <entity name='{0}' > <attribute name='{1}' /> <attribute name='{2}' /> <attribute name='{3}' /> <filter> <condition attribute='{3}' operator='{4}' value='{5}' /> </filter> </entity> </fetch>", searchEntity, primaryIDAttribute, primaryIDName, searchAttribute, filterOperator, searchValue); DataTable retVal = new DataTable() { TableName = "Search" }; retVal.Columns.AddRange( new DataColumn[] { new DataColumn(primaryIDAttribute, typeof(Guid)), new DataColumn(primaryIDName, typeof(string)), new DataColumn(searchAttribute, typeof(object)), new DataColumn("Entity", typeof(string)) }); EntityCollection entityCollection = connection.OrganizationService.RetrieveMultiple(new FetchExpression(fetchXml)); if (entityCollection.Entities.Count > 0) { var attDataType = entityCollection.Entities[0][searchAttribute].GetType(); foreach (Entity entity in entityCollection.Entities) { DataRow dataRow = retVal.NewRow(); if (entity.Contains(primaryIDAttribute)) { dataRow[primaryIDAttribute] = entity[primaryIDAttribute]; } if (entity.Contains(primaryIDName)) { dataRow[primaryIDName] = entity[primaryIDName]; } if (entity.Contains(searchAttribute)) { dataRow[searchAttribute] = entity[searchAttribute]; } dataRow["Entity"] = searchEntity; retVal.Rows.Add(dataRow); } } else { DataRow dataRow = retVal.NewRow(); dataRow[primaryIDAttribute] = "No"; dataRow[primaryIDName] = "record"; dataRow[searchAttribute] = "found"; retVal.Rows.Add(dataRow); } data.Push(new Result(retVal, string.Format("Search on {0} for {1} in {2}", searchEntity, searchValue, searchAttribute))); } } break; case ConsoleKey.F4: //open string url = ((OrganizationServiceProxy)connection.OrganizationService).ServiceConfiguration.CurrentServiceEndpoint.Address.Uri.Host; int port = ((OrganizationServiceProxy)connection.OrganizationService).ServiceConfiguration.CurrentServiceEndpoint.Address.Uri.Port; string target = string.Empty; switch (tableType) { case "Entity": string ename = data.Peek().LogicalName; // Data.TableName.Substring(7).Replace("(cached)", ""); target = string.Format("https://{0}:{1}/main.aspx?etn={2}&pagetype=entitylist" , url, port, ename); break; case "Record": //https://test-ifma.crm.dynamics.com/main.aspx?etn=bpt_chapter&pagetype=entityrecord&id=f0adaa03-4d26-e511-80db-c4346bacb92c string[] vals = data.Peek().Data.TableName.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string etn = data.Peek().LogicalName; // vals[1]; Guid id = Guid.Parse(vals[2]); target = string.Format("https://{0}:{1}/main.aspx?etn={2}&pagetype=entityrecord&id={3}" , url, port, etn, id); break; default: break; } //open browser System.Diagnostics.Process.Start(target); break; case ConsoleKey.F5: //refresh string tn = data.Peek().Data.TableName.Substring(7).Replace("(cached)", ""); data.Pop(); data.Push(new Result(GetEntity(tn, true))); break; default: int selection = -1; if (int.TryParse(returnCode.Response, out selection)) { switch (tableType) { case "Search": string targetEntityx = (string)data.Peek().Data.DefaultView[selection - 1]["Entity"]; Guid recordIdx = (Guid)data.Peek().Data.DefaultView[selection - 1][data.Peek().Data.Columns[0].ColumnName]; data.Push(new Result(GetRecord(targetEntityx, recordIdx))); break; case "Entities": if (selection <= data.First().Data.Rows.Count) { string entityName = (string)data.First().Data.DefaultView[selection - 1]["LogicalName"]; DataTable newEntity = GetEntity(entityName); data.Push(new Result(newEntity) { LogicalName = (string)data.First().Data.DefaultView[selection - 1]["LogicalName"] }); } break; case "Entity": DataRowView dr = data.Peek().Data.DefaultView[selection - 1]; switch ((string)dr["DataType"]) { case "Picklist": case "Status": case "State": string targetPicklist = (string)dr["LogicalName"]; string logName = data.Peek().Data.TableName.Substring(7).Replace("(cached)", ""); data.Push(new Result(GetPicklist(logName, targetPicklist))); break; case "Lookup": string targetEntity = (string)dr["MetaData"]; data.Push(new Result(GetEntity(targetEntity)) { LogicalName = targetEntity }); break; default: break; } break; default: break; } } break; } } }