/// <summary> /// Gets as lookup. /// </summary> /// <param name="entityName">Name of the entity.</param> /// <param name="attributeName">Name of the attribute.</param> /// <returns>Result.</returns> public Result GetAsLookup(string entityName, string attributeName) { try { string fetchXML = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' no-lock='true'> <entity name='{0}'> <attribute name='{1}' /> <order attribute='{1}' descending='true' /> </entity> </fetch>"; fetchXML = string.Format(fetchXML, entityName, attributeName); DataCollection <Entity> entities = xrmService.RetrieveMultiple(new FetchExpression(fetchXML)).Entities; if (entities != null && entities.Count > 0) { foreach (Entity item in entities) { ObjectCarrier.SetValue(item.Id.ToString(), item[attributeName].ToString()); } } return(new Result(false, string.Empty, ObjectCarrier.GetAllValues(), logSystem)); } catch (Exception ex) { return(new Result(true, MethodBase.GetCurrentMethod().ToString() + " : " + ExceptionHandler.HandleException(ex), null, logSystem)); } }
/// <summary> /// Gets the option sets. /// </summary> /// <param name="entityName">Name of the entity.</param> /// <param name="attributeName">Name of the attribute.</param> /// <returns>Result.</returns> public Result GetOptionSets(string entityName, string attributeName) { try { logSystem.CreateLog(System.Reflection.MethodBase.GetCurrentMethod().ToString() + " is called"); string AttributeName = attributeName; string EntityLogicalName = entityName; RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest { EntityFilters = EntityFilters.All, LogicalName = EntityLogicalName }; RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)xrmService.Execute(retrieveDetails); Microsoft.Xrm.Sdk.Metadata.EntityMetadata metadata = retrieveEntityResponseObj.EntityMetadata; Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => String.Equals (attribute.LogicalName, attributeName, StringComparison.OrdinalIgnoreCase)) as Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata; foreach (OptionMetadata item in picklistMetadata.OptionSet.Options) { ObjectCarrier.SetValue(item.Value.ToString(), item.Label.UserLocalizedLabel.Label); } return(new Result(false, string.Empty, ObjectCarrier.GetAllValues(), logSystem)); } catch (Exception ex) { return(new Result(true, MethodBase.GetCurrentMethod().ToString() + " : " + ExceptionHandler.HandleException(ex), null, logSystem)); } }
/// <summary> /// Gets as lookup. /// </summary> /// <param name="entityName">Name of the entity.</param> /// <param name="attributeName">Name of the attribute.</param> /// <returns>Result.</returns> public Result GetAsLookup(string entityName, string attributeName) { var watch = Stopwatch.StartNew(); try { string fetchXML = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' no-lock='true'> <entity name='{0}'> <attribute name='{1}' /> <order attribute='{1}' descending='true' /> </entity> </fetch>"; fetchXML = string.Format(fetchXML, entityName, attributeName); DataCollection <Entity> entities = xrmService.RetrieveMultiple(new FetchExpression(fetchXML)).Entities; if (entities != null && entities.Count > 0) { foreach (Entity item in entities) { ObjectCarrier.SetValue(item.Id.ToString(), item[attributeName].ToString()); } } return(new Result(false, string.Empty, ObjectCarrier.GetAllValues(), logSystem)); } catch (Exception ex) { return(new Result(true, MethodBase.GetCurrentMethod().ToString() + " : " + ExceptionHandler.HandleException(ex), null, logSystem)); } finally { watch.Stop(); logSystem.CreateLog($"{ MethodBase.GetCurrentMethod().ToString() } completed in { watch.ElapsedMilliseconds } ms", System.Diagnostics.EventLogEntryType.Information); } }