private void GetEntityRecords(QuickSearchFetchXml qsfx) { string xml = CreateXml(qsfx.FormattedFetchXml, qsfx.PagingCookie, qsfx.PageNumber == 0 ? 1 : qsfx.PageNumber, 5); FetchExpression query = new FetchExpression(); query.Query = xml; OrganizationRequest request = new OrganizationRequest() { RequestName = "RetrieveMultiple" }; request["Query"] = query; IOrganizationService service = SilverlightUtility.GetSoapService(); AsynchMethodParameters param = new AsynchMethodParameters { Service = service, QSFetchXml = qsfx }; service.BeginExecute(request, new AsyncCallback(GetEntities_Callback), param); }
private void GetEntities_Callback(IAsyncResult result) { try { AsynchMethodParameters param = (AsynchMethodParameters)result.AsyncState; OrganizationResponse response = param.Service.EndExecute(result); EntityCollection results = (EntityCollection)response["EntityCollection"]; param.QSFetchXml.PagingCookie = results.PagingCookie; param.QSFetchXml.HasMoreRecords = results.MoreRecords; //this.Dispatcher.BeginInvoke(() => SetNavButtons(results.MoreRecords)); xdoc = XDocument.Parse(param.QSFetchXml.FormattedFetchXml); var vv = xdoc.Root.Elements("entity").Descendants("attribute").ToList <XElement>(); //var vv = xfetchXml.Elements("entity").Descendants("attribute"); DataTable dt = new DataTable("EntityDataTable"); string pkColumn = vv.Where(a => a.Attribute("isPk") != null && Convert.ToBoolean(a.Attribute("isPk").Value) == true).First <XElement>().Attribute("name").Value; int _columnIndex = 0; foreach (var v in vv.OrderBy(a => Int32.Parse(a.Attribute("displayOrder") != null ? a.Attribute("displayOrder").Value : "1000"))) { // Create a column DataColumn dc1 = new DataColumn(v.Attribute("name").Value); dc1.Caption = v.Attribute("displayName") != null?v.Attribute("displayName").Value : v.Attribute("name").Value; dc1.ReadOnly = true; if (v.Attribute("isPk") != null && Convert.ToBoolean(v.Attribute("isPk").Value)) { dc1.Visibility = System.Windows.Visibility.Collapsed; } else { if (_columnIndex == 0) { dc1.DataType = typeof(HyperlinkButton); dc1.NavigateURLBindingColumn = pkColumn; } else { dc1.DataType = typeof(string); } _columnIndex++; } dc1.AllowResize = true; dc1.AllowSort = true; dc1.AllowReorder = true; if (v.Attribute("columnWidth") != null) { dc1.ColumnWidth = Double.Parse(v.Attribute("columnWidth").Value); } dt.Columns.Add(dc1); } #region For--Each - Populating DataRows foreach (Entity e in results.Entities) { DataRow dr = new DataRow(); for (int i = 0; i < e.Attributes.Count; i++) { if (e.Attributes[i].Value.GetType() == typeof(AliasedValue)) { dr[e.Attributes[i].Key.Replace(".", string.Empty)] = ((AliasedValue)e.Attributes[i].Value).Value.ToString(); } else if (e.Attributes[i].Value.GetType() == typeof(EntityReference)) { dr[e.Attributes[i].Key.Replace(".", string.Empty)] = ((EntityReference)e.Attributes[i].Value).Name; } else if (e.Attributes[i].Value.GetType() == typeof(OptionSetValue)) { dr[e.Attributes[i].Key.Replace(".", string.Empty)] = e.FormattedValues.GetItem(e.Attributes[i].Key); } else if (e.Attributes[i].Value.GetType() == typeof(Money)) { dr[e.Attributes[i].Key.Replace(".", string.Empty)] = e.FormattedValues.GetItem(e.Attributes[i].Key); } else if (e.Attributes[i].Value.GetType() == typeof(Boolean)) { dr[e.Attributes[i].Key.Replace(".", string.Empty)] = e.FormattedValues.GetItem(e.Attributes[i].Key); } //datetime else if (e.Attributes[i].Value.GetType() == typeof(DateTime)) { dr[e.Attributes[i].Key.Replace(".", string.Empty)] = e.FormattedValues.GetItem(e.Attributes[i].Key); } //"partyid" else if (e.Attributes[i].Value.GetType() == typeof(EntityCollection)) { string _str = string.Empty; EntityCollection ec = (EntityCollection)e.Attributes[i].Value; foreach (Entity activityPointer in ec.Entities) { if (activityPointer.GetAttributeValue <EntityReference>("partyid") != null) { _str += string.Format("{0};", activityPointer.GetAttributeValue <EntityReference>("partyid").Name); } } dr[e.Attributes[i].Key.Replace(".", string.Empty)] = _str.TrimEnd(new char[] { ';' }); } else { dr[e.Attributes[i].Key.Replace(".", string.Empty)] = e.Attributes[i].Value.ToString(); } } dt.Rows.Add(dr); } this.Dispatcher.BeginInvoke(() => DataBind(dt, param.QSFetchXml)); #endregion } catch (Exception Ex) { ReportError(Ex); } }