/// <summary> /// SavedQueryView constructor /// </summary> /// <param name="serviceContext">OrganizationServiceContext</param> /// <param name="savedQuery">savedquery entity</param> /// <param name="fetchXmlString">Existing known FetchXML for this view</param> /// <param name="layoutXmlString">Existing known LayoutXML for this view</param> /// <param name="languageCode">Language code used to retrieve the localized attribute label</param> /// <param name="aliasColumnNameStringFormat">A format string used to compose an alias column label. Index 0 is the alias attribute display name, index 1 is the aliased entity's display name. Default is "{0} ({1})".</param> public SavedQueryView(OrganizationServiceContext serviceContext, Entity savedQuery, string fetchXmlString, string layoutXmlString, int?languageCode = 0, string aliasColumnNameStringFormat = DefaultAliasColumnNameStringFormat) { if (serviceContext == null) { throw new ArgumentNullException("serviceContext"); } if (savedQuery == null) { throw new ArgumentNullException("savedQuery"); } ServiceContext = serviceContext; SavedQuery = savedQuery; LanguageCode = languageCode ?? 0; AliasColumnNameStringFormat = aliasColumnNameStringFormat; if (string.IsNullOrEmpty(fetchXmlString)) { fetchXmlString = savedQuery.GetAttributeValue <string>("fetchxml"); } if (!string.IsNullOrEmpty(fetchXmlString)) { FetchXml = XElement.Parse(fetchXmlString); SortExpression = SetSortExpression(FetchXml); var entityElement = FetchXml.Element("entity"); if (entityElement != null) { var entityName = entityElement.Attribute("name").Value; EntityLogicalName = entityName; } var response = (RetrieveEntityResponse)ServiceContext.Execute(new RetrieveEntityRequest { LogicalName = EntityLogicalName, EntityFilters = EntityFilters.Attributes }); if (response == null || response.EntityMetadata == null) { ADXTrace.Instance.TraceError(TraceCategory.Application, string.Format("Failed to get EntityMetadata for entity of type '{0}'.", EntityNamePrivacy.GetEntityName(EntityLogicalName))); return; } EntityMetadata = response.EntityMetadata; PrimaryKeyLogicalName = EntityMetadata.PrimaryIdAttribute; } if (string.IsNullOrEmpty(layoutXmlString)) { layoutXmlString = savedQuery.GetAttributeValue <string>("layoutxml"); } LayoutXml = XElement.Parse(layoutXmlString); var rowElement = LayoutXml.Element("row"); if (rowElement != null) { var cellNames = rowElement.Elements("cell") .Where(cell => cell.Attribute("ishidden") == null || cell.Attribute("ishidden").Value != "1") .Select(cell => cell.Attribute("name")).Where(name => name != null); CellNames = cellNames; var disabledSortCellNames = rowElement.Elements("cell") .Where(cell => cell.Attribute("disableSorting") != null && cell.Attribute("disableSorting").Value == "1") .Where(cell => cell.Attribute("name") != null) .Select(cell => cell.Attribute("name").Value); DisabledSortCellNames = disabledSortCellNames; var cellWidths = rowElement.Elements("cell") .Where(cell => cell.Attribute("ishidden") == null || cell.Attribute("ishidden").Value != "1") .Where(cell => cell.Attribute("name") != null) .ToDictionary(cell => cell.Attribute("name").Value, cell => Convert.ToInt32(cell.Attribute("width").Value)); CellWidths = cellWidths; } Name = ServiceContext.RetrieveLocalizedLabel(savedQuery.ToEntityReference(), "name", LanguageCode); Id = savedQuery.GetAttributeValue <Guid>("savedqueryid"); }