public ViewDataAdapter.FetchResult CustomFetchEntities(ViewConfiguration viewConfiguration) { CustomFetchXml converter = new CustomFetchXml(_serviceContext, new XrmConnection()); viewConfiguration = converter.CustomFilterViews(viewConfiguration); OrganizationServiceContext serviceContext = this.Dependencies.GetServiceContext(); Fetch fetch = FilterGlobalRecords(viewConfiguration); fetch = FilterSharedEntityScope(fetch, viewConfiguration); this.AddSelectableFilterToFetchEntity(fetch.Entity, this.Configuration, this.Filter); this.AddWebsiteFilterToFetchEntity(fetch.Entity, this.Configuration); this.AddOrderToFetch(fetch.Entity, this.Order); Fetch fetch2 = fetch; bool? dataPagerEnabled = this.Configuration.DataPagerEnabled; int num1 = dataPagerEnabled.HasValue ? (dataPagerEnabled.GetValueOrDefault() ? 1 : 0) : 1; string pagingCookie = fetch.PagingCookie; int page = this.Page; int pageSize = this.Configuration.PageSize; int num2 = 1; this.AddPaginationToFetch(fetch2, num1 != 0, pagingCookie, page, pageSize, num2 != 0); this.AddSearchFilterToFetchEntity(fetch.Entity, this.Configuration, this.Search, (IEnumerable <string>)null); this.AddMetadataFilterToFetch(fetch, this.Configuration, base.MetaFilter); if (this.ApplyRecordLevelFilters) { this.AddRecordLevelFiltersToFetch(fetch, this.Configuration, CrmEntityPermissionRight.Read); } else { this.TryAssert(fetch, this.Configuration, CrmEntityPermissionRight.Read); } if (this.ApplyRelatedRecordFilter) { this.AddRelatedRecordFilterToFetch(fetch, this.FilterRelationshipName, this.FilterEntityName, this.FilterValue); } if (((IEnumerable <AttributeMetadata>) this.EntityMetadata.Attributes).Any <AttributeMetadata>((Func <AttributeMetadata, bool>)(a => a.LogicalName == "statecode"))) { ViewDataAdapter.AddAttributesToFetchEntity(fetch.Entity, (IEnumerable <string>) new List <string>() { "statecode" }); if (((IEnumerable <AttributeMetadata>) this.EntityMetadata.Attributes).Any <AttributeMetadata>((Func <AttributeMetadata, bool>)(a => a.LogicalName == "statuscode"))) { ViewDataAdapter.AddAttributesToFetchEntity(fetch.Entity, (IEnumerable <string>) new List <string>() { "statuscode" }); } } return(base.FetchEntities(_dependencies.GetServiceContext(), fetch)); // return records; }
private ActionResult GetData(ViewConfiguration viewConfiguration, string sortExpression, string search, string filter, string metaFilter, int page, int pageSize = DefaultPageSize, bool applyRecordLevelFilters = true, bool applyRelatedRecordFilter = false, string filterRelationshipName = null, string filterEntityName = null, string filterAttributeName = null, Guid?filterValue = null, bool overrideMaxPageSize = false, string createdOnFilter = null) { if (viewConfiguration == null) { return(new HttpStatusCodeResult(HttpStatusCode.Forbidden, "Invalid Request.")); } if (pageSize < 0) { pageSize = DefaultPageSize; } viewConfiguration = EnableSearchForPriceList(viewConfiguration); if (pageSize > DefaultMaxPageSize && !overrideMaxPageSize) { Tracing.FrameworkInformation(GetType().FullName, "GetData", "pageSize={0} is greater than the allowed maximum page size of {1}. Page size has been constrained to {1}.", pageSize, DefaultMaxPageSize); pageSize = DefaultMaxPageSize; } var dataAdapterDependencies = new PortalConfigurationDataAdapterDependencies(requestContext: Request.RequestContext, portalName: viewConfiguration.PortalName); #region - DMS Custom Filtering - CustomFetchXml converter = new CustomFetchXml(dataAdapterDependencies.GetServiceContext(), new XrmConnection()); if (metaFilter != null) { if (metaFilter.IndexOf(",date") > 0) { string dateFilterString = metaFilter.Substring(metaFilter.IndexOf("date=") + 5); string[] dateFilters = dateFilterString.Split(new string[] { ",date=" }, StringSplitOptions.None); /*int dateFromIndexStart = metaFilter.LastIndexOf("DateFrom=") + 9; * int dateFromIndexEnd = metaFilter.LastIndexOf("&DateTo=") + 8; * dateFilter = metaFilter.Substring(metaFilter.IndexOf("date=") + 5); * int fieldNameIndex = metaFilter.LastIndexOf("&field=") + 7; * string dateFromValue = metaFilter.Substring(dateFromIndexStart, 10); * string dateToValue = metaFilter.Substring(dateFromIndexEnd, 10); * string entityFieldName = metaFilter.Substring(fieldNameIndex, (metaFilter.Length - fieldNameIndex));*/ foreach (string dateFilter in dateFilters) { string[] dateFilterValues = dateFilter.Split('&'); DateTime?dateFromValue = dateFilterValues[0].Split('=')[1] != "" ? (DateTime?)Convert.ToDateTime(dateFilterValues[0].Split('=')[1]) : null; DateTime?dateToValue = dateFilterValues[1].Split('=')[1] != "" ? (DateTime?)Convert.ToDateTime(dateFilterValues[1].Split('=')[1]) : null; string entityFieldName = dateFilterValues[2].Split('=')[1]; viewConfiguration = converter.SetCustomFetchXml(viewConfiguration, dateFromValue, dateToValue, entityFieldName); int start = metaFilter.LastIndexOf(",date"); metaFilter = metaFilter.Substring(0, start); } } if (metaFilter.IndexOf(",prospect") > 0) { int start = metaFilter.LastIndexOf(",prospect"); viewConfiguration = converter.FilterProspect(viewConfiguration); metaFilter = metaFilter.Substring(0, start); } if (metaFilter.IndexOf(",statecode") > 0) { int start = metaFilter.LastIndexOf(",statecode"); string statecode = metaFilter.Substring(metaFilter.IndexOf("statecode=") + 10); viewConfiguration = converter.FilterRecordsbyStateCode(viewConfiguration, statecode); metaFilter = metaFilter.Substring(0, start); } if (metaFilter.IndexOf(",vehiclecolor") > 0) { int start = metaFilter.LastIndexOf(",vehiclecolor"); string vehicleColor = metaFilter.Substring(metaFilter.IndexOf("vehiclecolor=") + 13); viewConfiguration = converter.FilterRecordsbyVehicleColor(viewConfiguration, vehicleColor); metaFilter = metaFilter.Substring(0, start); } } viewConfiguration = converter.CustomFilterViews(viewConfiguration); // viewConfiguration = converter.FilterRootBusinessUnitRecords(viewConfiguration); #endregion //disable related record filtering for Vehicle Lookup in PO var serviceContext2 = dataAdapterDependencies.GetServiceContext(); SavedQueryView queryView = viewConfiguration.GetSavedQueryView(serviceContext2); if (queryView.Name == "Vehicle Lookup - PO Portal View") { applyRecordLevelFilters = false; } var viewDataAdapter = applyRelatedRecordFilter && (!string.IsNullOrWhiteSpace(filterRelationshipName) && !string.IsNullOrWhiteSpace(filterEntityName)) ? new CustomViewAdapter(viewConfiguration, dataAdapterDependencies, filterRelationshipName, filterEntityName, filterAttributeName, filterValue ?? Guid.Empty, page, search, sortExpression, filter, metaFilter, applyRecordLevelFilters) : new CustomViewAdapter(viewConfiguration, dataAdapterDependencies, page, search, sortExpression, filter, metaFilter, applyRecordLevelFilters); var result = viewDataAdapter.CustomFetchEntities(viewConfiguration); var resultRecords = result.Records; // var filteredRecords = converter.FilterSharedEntityScope(viewConfiguration, result.Records); // var globalRecords = converter.FilterRootBusinessUnitRecords(viewConfiguration, filterEntityName, filterRelationshipName, filterValue, search); //var combinedResults = filteredRecords.Union(globalRecords); // combinedResults = combinedResults.GroupBy(x => x.Id).Select(y => y.First()); //Custom Order By /* string[] order = sortExpression.Split(' '); * var count = 0; * * foreach (var combinedResult in resultRecords) * { * foreach (var attributes in combinedResult.Attributes) * { * var name = attributes.Key; * if (name.Equals(order[0])) * { * count++; * * var value = attributes.Value; * var valueType = value.GetType().Name; * if (valueType == "String") * { * if (order[1] == "DESC") * resultRecords = resultRecords.OrderByDescending(x => x.Contains(order[0]) ? x.Attributes[order[0]] : ""); * else * resultRecords = resultRecords.OrderBy(x => x.Contains(order[0]) ? x.Attributes[order[0]] : ""); * } * else if (valueType == "Money") * { * if (order[1] == "DESC") * resultRecords = resultRecords.OrderByDescending(x => x.Contains(order[0]) ? x.GetAttributeValue<Money>(order[0]).ToString() : ""); * else * resultRecords = resultRecords.OrderBy(x => x.Contains(order[0]) ? x.GetAttributeValue<Money>(order[0]).ToString() : ""); * } * else if (valueType == "OptionSetValue") * { * if (order[1] == "DESC") * resultRecords = resultRecords.OrderByDescending(x => x.Contains(order[0]) ? x.FormattedValues[order[0]] : ""); * else * resultRecords = resultRecords.OrderBy(x => x.Contains(order[0]) ? x.FormattedValues[order[0]] : ""); * } * else if (valueType == "EntityReference") * { * if (order[1] == "DESC") * resultRecords = resultRecords.OrderByDescending(x => x.GetAttributeValue<EntityReference>(order[0]) != null ? x.GetAttributeValue<EntityReference>(order[0]).Name : ""); * else * resultRecords = resultRecords.OrderBy(x => x.GetAttributeValue<EntityReference>(order[0]) != null ? x.GetAttributeValue<EntityReference>(order[0]).Name : ""); * } * break; * } * } * * if (count > 0) * break; * } * //Custom Order By Ends Here*/ if (result.EntityPermissionDenied) { var permissionResult = new EntityPermissionResult(true); return(Json(permissionResult)); } IEnumerable <EntityRecord> records; if (viewConfiguration.EnableEntityPermissions && AdxstudioCrmConfigurationManager.GetCrmSection().ContentMap.Enabled) { var serviceContext = dataAdapterDependencies.GetServiceContext(); var crmEntityPermissionProvider = new CrmEntityPermissionProvider(); records = resultRecords.Select(e => new EntityRecord(e, serviceContext, crmEntityPermissionProvider, viewDataAdapter.EntityMetadata, true)); } else { records = resultRecords.Select(e => new EntityRecord(e, viewDataAdapter.EntityMetadata)); } var totalRecordCount = result.TotalRecordCount; var data = new PaginatedGridData(records, totalRecordCount, page, pageSize); var json = Json(data); json.MaxJsonLength = int.MaxValue; // return(json); }