private void ParseFetchXml(FetchQuerySettings querySettings) { jQueryObject fetchElement = querySettings.FetchXml; // Get the entities and link entities - only support 1 level deep jQueryObject entityElement = fetchElement.Find("entity"); string logicalName = entityElement.GetAttribute("name"); EntityQuery rootEntity; // Get query from cache or create new if (!EntityLookup.ContainsKey(logicalName)) { rootEntity = new EntityQuery(); rootEntity.LogicalName = logicalName; rootEntity.Attributes = new Dictionary<string, AttributeQuery>(); EntityLookup[rootEntity.LogicalName] = rootEntity; } else { rootEntity = EntityLookup[logicalName]; } // Get Linked Entities(1 deep) jQueryObject linkEntities = entityElement.Find("link-entity"); linkEntities.Each(delegate(int index, Element element) { EntityQuery link = new EntityQuery(); link.Attributes = new Dictionary<string, AttributeQuery>(); link.AliasName = element.GetAttribute("alias").ToString(); link.LogicalName = element.GetAttribute("name").ToString(); if (!EntityLookup.ContainsKey(link.LogicalName)) { EntityLookup[link.LogicalName] = link; } else { string alias = link.AliasName; link = EntityLookup[link.LogicalName]; link.AliasName = alias; } if (!AliasEntityLookup.ContainsKey(link.AliasName)) { AliasEntityLookup[link.AliasName] = link; } }); querySettings.RootEntity = rootEntity; // Issue #35 - Add any lookup/picklist quick find fields that are not included in results attributes will cause a format execption // because we don't have the metadata - this means that 'name' is not appended to the attribute // Add the search string and adjust any lookup columns jQueryObject conditions = fetchElement.Find("filter[isquickfindfields='1']"); conditions.First().Children().Each(delegate(int index, Element element) { logicalName = element.GetAttribute("attribute").ToString(); jQueryObject e = jQuery.FromElement(element); jQueryObject p =e.Parents("link-entity"); if (!querySettings.RootEntity.Attributes.ContainsKey(logicalName)) { AttributeQuery attribute = new AttributeQuery(); attribute.LogicalName = logicalName; attribute.Columns = new List<Column>(); querySettings.RootEntity.Attributes[logicalName] = attribute; } }); }
private void GetViewDefinition(bool isQuickFind, string viewName) { // Get the Quick Find View for the entity string getviewfetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'> <entity name='savedquery'> <attribute name='name' /> <attribute name='fetchxml' /> <attribute name='layoutxml' /> <attribute name='returnedtypecode' /> <filter type='and'> <filter type='or'>"; foreach (string entity in Entities) { int? typeCode = (int?)Script.Literal("Mscrm.EntityPropUtil.EntityTypeName2CodeMap[{0}]", entity); getviewfetchXml += @"<condition attribute='returnedtypecode' operator='eq' value='" + typeCode.ToString() + @"'/>"; } getviewfetchXml += @"</filter>"; if (isQuickFind) { getviewfetchXml += @"<condition attribute='isquickfindquery' operator='eq' value='1'/> <condition attribute='isdefault' operator='eq' value='1'/>"; } else if (viewName != null && viewName.Length > 0) { getviewfetchXml += @"<condition attribute='name' operator='eq' value='" + XmlHelper.Encode(viewName) + @"'/>"; } else { // Get default associated view getviewfetchXml += @"<condition attribute='querytype' operator='eq' value='2'/> <condition attribute='isdefault' operator='eq' value='1'/>"; } getviewfetchXml += @"</filter> </entity> </fetch>"; // Get the Quick Find View EntityCollection quickFindQuery = OrganizationServiceProxy.RetrieveMultiple(getviewfetchXml); Dictionary<string, Entity> entityLookup = new Dictionary<string, Entity>(); // Preseve the requested view order foreach (Entity view in quickFindQuery.Entities) { entityLookup[view.GetAttributeValueString("returnedtypecode")] = view; } foreach (string typeName in Entities) { Entity view = entityLookup[typeName]; string fetchXml = view.GetAttributeValueString("fetchxml"); string layoutXml = view.GetAttributeValueString("layoutxml"); EntityQuery query; if (EntityLookup.ContainsKey(typeName)) { query = EntityLookup[typeName]; } else { query = new EntityQuery(); query.LogicalName = typeName; query.Views = new Dictionary<string, FetchQuerySettings>(); query.Attributes = new Dictionary<string, AttributeQuery>(); EntityLookup[typeName] = query; } // Parse the fetch and layout to get the attributes and columns FetchQuerySettings config = Parse(fetchXml, layoutXml); query.Views[view.GetAttributeValueString("name")] = config; if (isQuickFind) { query.QuickFindQuery = config; } } }
private List<Column> ParseLayoutXml(EntityQuery rootEntity, string layoutXml) { jQueryObject layout = jQuery.FromHtml(layoutXml); jQueryObject cells = layout.Find("cell"); List<Column> columns = new List<Column>(); cells.Each(delegate(int index, Element element) { string cellName = element.GetAttribute("name").ToString(); string logicalName = cellName; EntityQuery entity; AttributeQuery attribute; // Is this an alias attribute? int pos = cellName.IndexOf('.'); if (pos > -1) { // Aliased entity string alias = cellName.Substr(0, pos); logicalName = cellName.Substr(pos + 1); entity = AliasEntityLookup[alias]; } else { // Root entity entity=rootEntity; } // Does the attribute allready exist? if (entity.Attributes.ContainsKey(logicalName)) { // Already exists attribute = entity.Attributes[logicalName]; } else { // New attribute = new AttributeQuery(); attribute.Columns = new List<Column>(); attribute.LogicalName = logicalName; entity.Attributes[attribute.LogicalName] = attribute; } // Add column int width = int.Parse(element.GetAttribute("width").ToString()); object disableSorting = element.GetAttribute("disableSorting"); Column col = GridDataViewBinder.NewColumn(attribute.LogicalName, attribute.LogicalName, width); // Display name get's queried later col.Sortable = !(disableSorting != null && disableSorting.ToString() == "1"); attribute.Columns.Add(col); columns.Add(col); }); return columns; }