protected void LimitQueryToCurrentRecord(IOrganizationService service, ref string fetchXml, string entityName, Guid entityId) { Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest metadataRequest = new Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest() { LogicalName = entityName }; Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse metadataResponse = service.Execute(metadataRequest) as Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse; if (metadataResponse == null) { return; } XmlDocument fetchXmlDoc = new XmlDocument(); fetchXmlDoc.LoadXml(fetchXml); this.UpdateConditionRecursively(fetchXmlDoc, fetchXmlDoc.FirstChild, entityId, entityName, metadataResponse.EntityMetadata.PrimaryIdAttribute); fetchXml = fetchXmlDoc.OuterXml; }
protected List <ColumnInformation> BuildTableInformation(IOrganizationService service, string fetchXml, string layoutXml) { List <QueryAttribute> fetchXmlColumns = new List <QueryAttribute>(); XmlDocument doc = new XmlDocument(); doc.LoadXml(fetchXml); XmlNode entityNode = doc["fetch"]["entity"]; string primaryEntity = entityNode.Attributes["name"].Value; this.RetrieveQueryAttributeRecursively(entityNode, fetchXmlColumns); List <QueryAttribute> layoutXmlColumns = new List <QueryAttribute>(); if (!String.IsNullOrEmpty(layoutXml)) { doc = new XmlDocument(); doc.LoadXml(layoutXml); foreach (XmlNode cell in doc["grid"]["row"].ChildNodes) { string columnPresentationValue = cell.Attributes["name"].Value; if (columnPresentationValue.Contains(".")) { layoutXmlColumns.Add(new QueryAttribute() { EntityAlias = columnPresentationValue.Split('.')[0], Attribute = columnPresentationValue.Split('.')[1] }); } else { layoutXmlColumns.Add(new QueryAttribute() { Attribute = columnPresentationValue, EntityName = primaryEntity }); } } foreach (QueryAttribute attribute in layoutXmlColumns.Where(qa => !String.IsNullOrWhiteSpace(qa.EntityAlias))) { QueryAttribute attributeFromFetch = fetchXmlColumns.FirstOrDefault(f => f.EntityAlias == attribute.EntityAlias); if (attributeFromFetch != null) { attribute.EntityName = attributeFromFetch.EntityName; } } } List <string> distintEntityNames = layoutXmlColumns.Select(lo => lo.EntityName).Union(fetchXmlColumns.Select(f => f.EntityName)).Distinct().ToList(); foreach (string entityName in distintEntityNames) { Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest request = new Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest() { EntityFilters = EntityFilters.Attributes, LogicalName = entityName }; Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse response = service.Execute(request) as Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse; EntityMetadata metadata = response.EntityMetadata; foreach (QueryAttribute queryAttribute in layoutXmlColumns.Union(fetchXmlColumns).Where(c => c.EntityName == entityName && !String.IsNullOrWhiteSpace(c.Attribute))) { if (!String.IsNullOrWhiteSpace(queryAttribute.AttribueAlias)) { queryAttribute.AttributeLabel = queryAttribute.AttribueAlias; } else { AttributeMetadata attributeMetadata = metadata.Attributes.FirstOrDefault(a => a.LogicalName == queryAttribute.Attribute); Label displayLabel = attributeMetadata.DisplayName; if (displayLabel != null && displayLabel.UserLocalizedLabel != null) { queryAttribute.AttributeLabel = displayLabel.UserLocalizedLabel.Label; } } } } List <ColumnInformation> returnColumns = new List <ColumnInformation>(); List <QueryAttribute> toReturn = (layoutXmlColumns.Count > 0) ? layoutXmlColumns : fetchXmlColumns; foreach (QueryAttribute qa in toReturn) { ColumnInformation ci = new ColumnInformation(); ci.Header = qa.AttributeLabel; if (!String.IsNullOrEmpty(qa.EntityAlias)) { ci.QueryResultAlias = String.Format("{0}.{1}", qa.EntityAlias, qa.Attribute); } else if (!String.IsNullOrEmpty(qa.AttribueAlias)) { ci.QueryResultAlias = qa.AttribueAlias; } else { ci.QueryResultAlias = qa.Attribute; } returnColumns.Add(ci); returnColumns.Add(new ColumnInformation() { Header = String.Format(MetadataHeaderFormat, ci.Header, "RawValue") }); } return(returnColumns); }