protected int?RetrieveEntityObjectTypeCode(IWorkflowContext workflowContext, IOrganizationService service) { int?returnValue = null; Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest metadataRequest = new Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest() { LogicalName = workflowContext.PrimaryEntityName }; Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse metadataResponse = service.Execute(metadataRequest) as Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse; if (metadataResponse != null) { returnValue = metadataResponse.EntityMetadata.ObjectTypeCode; } return(returnValue); }
private static void WriteFormControls(CodeWriter writer, Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse entity, Xrm.form xrmform, bool attributes) { if (attributes) { writer.Writeline("getAttribute(name: string): void;"); } if (attributes) { writer.Writeline("getControl(name: string): void;"); } else { writer.Writeline("get(name: string): void;"); } foreach (var tab in xrmform.tabs) { foreach (var col in tab.columns) { foreach (var sec in col.sections) { foreach (var row in sec.rows) { if (row.cell != null && row.cell.control != null && !string.IsNullOrEmpty(row.cell.control.datafieldname)) { var attr = (from a in entity.EntityMetadata.Attributes where a.LogicalName == row.cell.control.datafieldname select a).Single(); WriteAttribute(writer, row.cell.control.id, attr.AttributeType, attributes); } } } } } if (xrmform.header != null && xrmform.header.rows != null) { foreach (var cell in xrmform.header.rows.row) { if (cell.control != null && !string.IsNullOrEmpty(cell.control.datafieldname)) { var attr = (from a in entity.EntityMetadata.Attributes where a.LogicalName == cell.control.datafieldname select a).Single(); WriteAttribute(writer, cell.control.id, attr.AttributeType, attributes); } } } }
protected void SetWorkflowRecordInfo(CodeActivityContext context, IWorkflowContext workflowContext, IOrganizationService service) { // Record info this.WorkflowRecord_LookupID.Set(context, workflowContext.PrimaryEntityId.ToString()); this.WorkflowRecord_LookupEntityName.Set(context, workflowContext.PrimaryEntityName); Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest metadataRequest = new Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest() { LogicalName = workflowContext.PrimaryEntityName }; Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse metadataResponse = service.Execute(metadataRequest) as Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse; if (metadataResponse != null) { this.WorkflowRecord_LookupObjectTypeCode.Set(context, metadataResponse.EntityMetadata.ObjectTypeCode); if (metadataResponse.EntityMetadata.DisplayName != null && metadataResponse.EntityMetadata.DisplayName.UserLocalizedLabel != null) { this.WorkflowRecord_LookupEntityDisplayName.Set(context, metadataResponse.EntityMetadata.DisplayName.UserLocalizedLabel.Label); } } }
protected DataTable BuildDataTable(CodeActivityContext context, IWorkflowContext workflowContext, IOrganizationService service) { TimeZoneSummary timeZone = StaticMethods.CalculateTimeZoneToUse(this.TimeZoneOption.Get(context), workflowContext, service); DataTable table = new DataTable() { TableName = workflowContext.PrimaryEntityName }; table.Columns.AddRange(new DataColumn[] { new DataColumn("Date"), new DataColumn("User"), new DataColumn("Attribute"), new DataColumn("Old Value"), new DataColumn("New Value") }); DateTime oldestUpdate = DateTime.MinValue; if (this.Units != null && this.Number != null && this.Number.Get <int>(context) != 0) { OptionSetValue value = this.Units.Get <OptionSetValue>(context); if (value != null) { switch (value.Value) { case 222540000: oldestUpdate = DateTime.Now.AddYears(this.Number.Get <int>(context) * -1); break; case 222540001: oldestUpdate = DateTime.Now.AddMonths(this.Number.Get <int>(context) * -1); break; case 222540002: oldestUpdate = DateTime.Now.AddDays(this.Number.Get <int>(context) * -7); break; case 222540003: oldestUpdate = DateTime.Now.AddDays(this.Number.Get <int>(context) * -1); break; case 222540004: oldestUpdate = DateTime.Now.AddHours(this.Number.Get <int>(context) * -1); break; default: oldestUpdate = DateTime.Now.AddMinutes(this.Number.Get <int>(context) * -1); break; } } } RetrieveRecordChangeHistoryRequest request = new RetrieveRecordChangeHistoryRequest() { Target = new EntityReference(workflowContext.PrimaryEntityName, workflowContext.PrimaryEntityId), PagingInfo = new PagingInfo() { Count = 100, PageNumber = 1 } }; RetrieveRecordChangeHistoryResponse response = service.Execute(request) as RetrieveRecordChangeHistoryResponse; var detailsToInclude = response.AuditDetailCollection.AuditDetails .Where(ad => ad is AttributeAuditDetail && ad.AuditRecord.Contains("createdon") && ((DateTime)ad.AuditRecord["createdon"]) > oldestUpdate) .OrderByDescending(ad => ((DateTime)ad.AuditRecord["createdon"])) .ToList(); if (detailsToInclude.Any()) { Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest retrieveEntityRequest = new Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest() { EntityFilters = EntityFilters.Attributes, LogicalName = workflowContext.PrimaryEntityName }; Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse retrieveEntityResponse = service.Execute(retrieveEntityRequest) as Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse; EntityMetadata metadata = retrieveEntityResponse.EntityMetadata; foreach (var detail in detailsToInclude.Select(d => d as AttributeAuditDetail).Where(d => d.NewValue != null && d.OldValue != null)) { DateTime dateToModify = (DateTime)detail.AuditRecord["createdon"]; if (dateToModify.Kind != DateTimeKind.Utc) { dateToModify = dateToModify.ToUniversalTime(); } LocalTimeFromUtcTimeRequest timeZoneChangeRequest = new LocalTimeFromUtcTimeRequest() { UtcTime = dateToModify, TimeZoneCode = timeZone.MicrosoftIndex }; LocalTimeFromUtcTimeResponse timeZoneResponse = service.Execute(timeZoneChangeRequest) as LocalTimeFromUtcTimeResponse; DateTime timeZoneSpecificDateTime = timeZoneResponse.LocalTime; var details = detail.NewValue.Attributes.Keys.Union(detail.OldValue.Attributes.Keys) .Distinct() .Select(a => new { AttributeName = a, DisplayName = GetDisplayLabel(metadata, a) }) .OrderBy(a => a.DisplayName); foreach (var item in details) { DataRow newRow = table.NewRow(); newRow["User"] = GetDisplayValue(detail.AuditRecord, "userid"); newRow["Date"] = timeZoneSpecificDateTime.ToString("MM/dd/yyyy h:mm tt"); newRow["Attribute"] = item.DisplayName; newRow["Old Value"] = GetDisplayValue(detail.OldValue, item.AttributeName); newRow["New Value"] = GetDisplayValue(detail.NewValue, item.AttributeName); table.Rows.Add(newRow); } } } return(table); }
public static void Run(string[] args) { try { var sourcefile = args.GetParameter("source", true); var targetfile = args.GetParameter("output", true); var forms = Kipon.WebResources.Tools.Generator.Config.forms.GetFromXmlFile(sourcefile); IOrganizationService service = Kipon.WebResources.Tools.Service.Factory.GetOrganizationService(args); using (var uow = new Entities.CrmUnitOfWork(service)) { using (var writer = new CodeWriter(targetfile)) { writer.Writeline("/// <reference path=\"xrm.d.ts\" />"); writer.Writeline(""); writer.Writeline("declare namespace XrmForm {"); foreach (var form in forms.form) { var xrmEntityForm = (from f in uow.Systemforms.GetQuery() where f.ObjectTypeCode == form.entity && f.Name == form.name select f).SingleOrDefault(); if (xrmEntityForm == null) { Console.WriteLine("Entity " + form.entity + " Form " + form.name + " was not found"); continue; } Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse entity = null; if (entities.ContainsKey(form.entity)) { entity = entities[form.entity]; } else { var req = new Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest { RetrieveAsIfPublished = true, LogicalName = form.entity, EntityFilters = Microsoft.Xrm.Sdk.Metadata.EntityFilters.Attributes }; entity = uow.ExecuteRequest <Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse>(req); entities.Add(form.entity, entity); } #region XrmForm interface writer.Writeline("interface " + form.asname + " {"); var reader = new System.IO.StringReader(xrmEntityForm.FormXml); var serializer = new XmlSerializer(typeof(Kipon.WebResources.Tools.Generator.Xrm.form)); var xrmform = (Kipon.WebResources.Tools.Generator.Xrm.form)serializer.Deserialize(reader); WriteFormControls(writer, entity, xrmform, true); writer.Writeline("ui: " + form.asname + "UI;"); writer.Writeline("}"); #endregion #region entity ui interface writer.Writeline("interface " + form.asname + "UI {"); writer.Writeline("controls: " + form.asname + "UIControls;"); writer.Writeline("tabs: " + form.asname + "UITabs;"); writer.Writeline("}"); #endregion #region controls writer.Writeline("interface " + form.asname + "UIControls {"); WriteFormControls(writer, entity, xrmform, false); writer.Writeline("}"); #endregion #region generate tabs writer.Writeline("interface " + form.asname + "UITabs {"); writer.Writeline("get(name: string): void;"); var ix = 0; foreach (var tab in xrmform.tabs) { writer.Writeline("get(name: \"" + tab.name + "\"): " + form.asname + "UITab" + ix.ToString() + ";"); ix++; } writer.Writeline("}"); ix = 0; foreach (var tab in xrmform.tabs) { writer.Writeline("interface " + form.asname + "UITab" + ix + " extends Xrm.Controls.UiStandardElement, Xrm.Controls.UiFocusable {"); writer.Writeline("sections: " + form.asname + "UITab" + ix + "Sections;"); writer.Writeline("}"); ix++; } ix = 0; foreach (var tab in xrmform.tabs) { writer.Writeline("interface " + form.asname + "UITab" + ix + "Sections {"); writer.Writeline("get(name: string): void;"); foreach (var col in tab.columns) { foreach (var sec in col.sections) { writer.Writeline("get(name: \"" + sec.name + "\"): Xrm.Controls.Section;"); } } writer.Writeline("}"); ix++; } #endregion } writer.Writeline("}"); } } } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); Console.WriteLine("Usage: Kipon.WebResources.Tools generate /url:url /user:user /password:password /source:definitionsourcefilename /output:output.d.name"); } }
protected DataTable BuildDataTable(CodeActivityContext context, IWorkflowContext workflowContext, IOrganizationService service) { DataTable table = new DataTable() { TableName = workflowContext.PrimaryEntityName }; table.Columns.AddRange(new DataColumn[] { new DataColumn("Attribute"), new DataColumn("Old Value"), new DataColumn("New Value") }); RetrieveRecordChangeHistoryRequest request = new RetrieveRecordChangeHistoryRequest() { Target = new EntityReference(workflowContext.PrimaryEntityName, workflowContext.PrimaryEntityId), PagingInfo = new PagingInfo() { Count = 100, PageNumber = 1 } }; RetrieveRecordChangeHistoryResponse response = service.Execute(request) as RetrieveRecordChangeHistoryResponse; if (response != null && response.AuditDetailCollection.Count > 0) { string onlyIfField = LastUpdateWithThisField.Get(context); AttributeAuditDetail detail = null; for (int i = 0; i < response.AuditDetailCollection.Count; i++) { AttributeAuditDetail thisDetail = response.AuditDetailCollection[i] as AttributeAuditDetail; if (thisDetail != null && (String.IsNullOrEmpty(onlyIfField) || (thisDetail.OldValue.Attributes.Keys.Contains(onlyIfField) || thisDetail.NewValue.Attributes.Keys.Contains(onlyIfField)))) { detail = thisDetail; break; } } if (detail != null && detail.NewValue != null && detail.OldValue != null) { Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest retrieveEntityRequest = new Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest() { EntityFilters = EntityFilters.Attributes, LogicalName = workflowContext.PrimaryEntityName }; Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse retrieveEntityResponse = service.Execute(retrieveEntityRequest) as Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse; EntityMetadata metadata = retrieveEntityResponse.EntityMetadata; var details = detail.NewValue.Attributes.Keys.Union(detail.OldValue.Attributes.Keys) .Distinct() .Select(a => new { AttributeName = a, DisplayName = this.GetDisplayLabel(metadata, a), OldValue = String.Empty, NewValue = String.Empty }) .OrderBy(a => a.DisplayName); foreach (var item in details) { DataRow newRow = table.NewRow(); newRow["Attribute"] = item.DisplayName; newRow["Old Value"] = GetDisplayValue(detail.OldValue, item.AttributeName); newRow["New Value"] = GetDisplayValue(detail.NewValue, item.AttributeName); table.Rows.Add(newRow); } } } return(table); }