public int GetOptionSetValueByLabel(string entityLogicalName, string optionSetLogicalName, string label) { if (string.IsNullOrEmpty(entityLogicalName) || string.IsNullOrEmpty(optionSetLogicalName)) { return(0); } var retrieveAttributeRequest = new RetrieveAttributeRequest { EntityLogicalName = entityLogicalName, LogicalName = optionSetLogicalName }; var retrieveAttributeResponse = (RetrieveAttributeResponse)XrmContext.Execute(retrieveAttributeRequest); var retrievedPicklistAttributeMetadata = (EnumAttributeMetadata)retrieveAttributeResponse.AttributeMetadata; int value = 0; for (int i = 0; i < retrievedPicklistAttributeMetadata.OptionSet.Options.Count(); i++) { if (retrievedPicklistAttributeMetadata.OptionSet.Options[i].Label.LocalizedLabels[0].Label == label) { value = retrievedPicklistAttributeMetadata.OptionSet.Options[i].Value.Value; } } return(value); }
public string GetOptionSetLabelByValue(string entityLogicalName, string optionSetLogicalName, int value) { if (string.IsNullOrEmpty(entityLogicalName) || string.IsNullOrEmpty(optionSetLogicalName)) { return(string.Empty); } var retrieveAttributeRequest = new RetrieveAttributeRequest { EntityLogicalName = entityLogicalName, LogicalName = optionSetLogicalName }; var retrieveAttributeResponse = (RetrieveAttributeResponse)XrmContext.Execute(retrieveAttributeRequest); var retrievedPicklistAttributeMetadata = (EnumAttributeMetadata)retrieveAttributeResponse.AttributeMetadata; var option = retrievedPicklistAttributeMetadata.OptionSet.Options.FirstOrDefault(o => o.Value == value); if (option == null) { return(string.Empty); } var label = option.Label.UserLocalizedLabel.Label; if (option.Value.HasValue) { _optionSetLabelCache[option.Value.Value] = label; } return(label); }
public IDictionary <int, string> GetGlobalOptionSetValues(string optionSetLogicalName) { IDictionary <int, string> optionSet = new Dictionary <int, string>(); if (string.IsNullOrEmpty(optionSetLogicalName)) { return(null); } var retrieveAttributeRequest = new RetrieveOptionSetRequest { Name = optionSetLogicalName }; var retrieveAttributeResponse = (RetrieveOptionSetResponse)XrmContext.Execute(retrieveAttributeRequest); var retrievedPicklistAttributeMetadata = (OptionSetMetadata)retrieveAttributeResponse.OptionSetMetadata; for (int i = 0; i < retrievedPicklistAttributeMetadata.Options.Count(); i++) { optionSet.Add(new KeyValuePair <int, string>(retrievedPicklistAttributeMetadata.Options[i].Value.Value, retrievedPicklistAttributeMetadata.Options[i].Label.LocalizedLabels[0].Label)); } return(optionSet); }
public string GetEntityStatusLabel(string entityLogicalName, int statusCode) { // Create the attribute request (define which entities attribute // information we want to get) entity.LogicalName is our current // name of our entity (ex: contact, account, etc.) RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest { EntityLogicalName = entityLogicalName, LogicalName = "statuscode", RetrieveAsIfPublished = true }; // Execute the request to get the attribute information RetrieveAttributeResponse attributeResponse = (RetrieveAttributeResponse)XrmContext.Execute(attributeRequest); AttributeMetadata attrMetadata = (AttributeMetadata)attributeResponse.AttributeMetadata; // Cast the AttributeMetadata to StatusAttribute data StatusAttributeMetadata statusAttrMetadata = (StatusAttributeMetadata)attrMetadata; // Get the status code label by comparing all of the status code values // possible for the status code drop down list. Once we get a match on // the value we take the label for that value // For every status code value within all of our status codes values // (all of the values in the drop down list) foreach (StatusOptionMetadata statusMeta in statusAttrMetadata.OptionSet.Options) { // Check to see if our current value matches if (statusMeta.Value == statusCode) { // If our numeric value matches, set the string to our status code // label return(statusMeta.Label.UserLocalizedLabel.Label); } } // if we got this far, something didn't add up. return(string.Empty); }
protected void Page_Load(object sender, EventArgs e) { var reference = Entity.GetAttributeValue <EntityReference>("adx_entityform"); var entityFormRecord = XrmContext.CreateQuery("adx_entityform").FirstOrDefault(ef => ef.GetAttributeValue <Guid>("adx_entityformid") == reference.Id); if (entityFormRecord != null) { var recordEntityLogicalName = entityFormRecord.GetAttributeValue <string>("adx_entityname"); Guid recordId; if (Guid.TryParse(Request["id"], out recordId)) { var metadataRequest = new RetrieveEntityRequest { LogicalName = recordEntityLogicalName, EntityFilters = EntityFilters.Attributes }; var metadataResponse = (RetrieveEntityResponse)XrmContext.Execute(metadataRequest); var primaryFieldLogicalName = metadataResponse.EntityMetadata.PrimaryIdAttribute; var permitRecord = XrmContext.CreateQuery(recordEntityLogicalName).FirstOrDefault(r => r.GetAttributeValue <Guid>(primaryFieldLogicalName) == recordId); var permitTypeReference = permitRecord.GetAttributeValue <EntityReference>("adx_permittype"); var permitType = XrmContext.CreateQuery("adx_permittype").FirstOrDefault( srt => srt.GetAttributeValue <Guid>("adx_permittypeid") == permitTypeReference.Id); var entityName = permitType.GetAttributeValue <string>("adx_entityname"); RegardingContactFieldName = permitType.GetAttributeValue <string>("adx_regardingcontactfieldname"); var trueMetadataRequest = new RetrieveEntityRequest { LogicalName = entityName, EntityFilters = EntityFilters.Attributes }; var trueMetadataResponse = (RetrieveEntityResponse)XrmContext.Execute(trueMetadataRequest); var primaryFieldName = trueMetadataResponse.EntityMetadata.PrimaryIdAttribute; var entityId = permitRecord.GetAttributeValue <string>("adx_entityid"); var trueRecordId = Guid.Parse(entityId); var trueRecord = XrmContext.CreateQuery(entityName).FirstOrDefault(r => r.GetAttributeValue <Guid>(primaryFieldName) == trueRecordId); Permit = trueRecord; var permitDataSource = CreateDataSource("PermitDataSource", entityName, primaryFieldName, trueRecordId); var permitFormView = new CrmEntityFormView() { FormName = "Details Form", Mode = FormViewMode.Edit, EntityName = entityName, CssClass = "crmEntityFormView", AutoGenerateSteps = false }; var languageCodeSetting = OrganizationServiceContextExtensions.GetSiteSettingValueByName(ServiceContext, Portal.Website, "Language Code"); if (!string.IsNullOrWhiteSpace(languageCodeSetting)) { int languageCode; if (int.TryParse(languageCodeSetting, out languageCode)) { permitFormView.LanguageCode = languageCode; permitFormView.ContextName = languageCode.ToString(CultureInfo.InvariantCulture); permitDataSource.CrmDataContextName = languageCode.ToString(CultureInfo.InvariantCulture); } } CrmEntityFormViewPanel.Controls.Add(permitFormView); permitFormView.DataSourceID = permitDataSource.ID; var regardingContact = Permit.GetAttributeValue <EntityReference>(RegardingContactFieldName); if (regardingContact == null || Contact == null || regardingContact.Id != Contact.Id) { PermitControls.Enabled = false; PermitControls.Visible = false; AddNoteInline.Visible = false; AddNoteInline.Enabled = false; } else { var dataAdapterDependencies = new PortalConfigurationDataAdapterDependencies(requestContext: Request.RequestContext, portalName: PortalName); var dataAdapter = new AnnotationDataAdapter(dataAdapterDependencies); var annotations = dataAdapter.GetAnnotations(Permit.ToEntityReference(), new List <Order> { new Order("createdon") }, respectPermissions: false); NotesList.DataSource = annotations; NotesList.DataBind(); } } } }
protected void Page_Load(object sender, EventArgs e) { if (ServiceRequestRollupRecord != null) { var serviceRequestTypeReference = ServiceRequestRollupRecord.GetAttributeValue <EntityReference>("adx_servicerequesttype"); var serviceRequestType = XrmContext.CreateQuery("adx_servicerequesttype").FirstOrDefault( srt => srt.GetAttributeValue <Guid>("adx_servicerequesttypeid") == serviceRequestTypeReference.Id); var entityName = serviceRequestType.GetAttributeValue <string>("adx_entityname"); RegardingContactFieldName = serviceRequestType.GetAttributeValue <string>("adx_regardingcontactfieldname"); var trueMetadataRequest = new RetrieveEntityRequest { LogicalName = entityName, EntityFilters = EntityFilters.Attributes }; var trueMetadataResponse = (RetrieveEntityResponse)XrmContext.Execute(trueMetadataRequest); var primaryFieldName = trueMetadataResponse.EntityMetadata.PrimaryIdAttribute; var entityId = ServiceRequestRollupRecord.GetAttributeValue <string>("adx_entityid"); var trueRecordId = Guid.Parse(entityId); var trueRecord = XrmContext.CreateQuery(entityName).FirstOrDefault(r => r.GetAttributeValue <Guid>(primaryFieldName) == trueRecordId); ServiceRequest = trueRecord; var regardingContact = ServiceRequest.GetAttributeValue <EntityReference>(RegardingContactFieldName); if (regardingContact == null || Contact == null || regardingContact.Id != Contact.Id) { AddANote.Enabled = false; AddANote.Visible = false; AddNoteInline.Visible = false; AddNoteInline.Enabled = false; RenderCrmEntityFormView(entityName, primaryFieldName, trueRecordId, FormViewMode.ReadOnly); var dataAdapterDependencies = new PortalConfigurationDataAdapterDependencies(requestContext: Request.RequestContext, portalName: PortalName); var dataAdapter = new AnnotationDataAdapter(dataAdapterDependencies); var annotations = dataAdapter.GetAnnotations(ServiceRequest.ToEntityReference(), new List <Order> { new Order("createdon") }); if (!annotations.Any()) { NotesLabel.Visible = false; NotesList.Visible = false; } NotesList.DataSource = annotations; NotesList.DataBind(); } else { RenderCrmEntityFormView(entityName, primaryFieldName, trueRecordId, FormViewMode.Edit); var dataAdapterDependencies = new PortalConfigurationDataAdapterDependencies(requestContext: Request.RequestContext, portalName: PortalName); var dataAdapter = new AnnotationDataAdapter(dataAdapterDependencies); var annotations = dataAdapter.GetAnnotations(ServiceRequest.ToEntityReference(), new List <Order> { new Order("createdon") }, privacy: AnnotationPrivacy.Web | AnnotationPrivacy.Private | AnnotationPrivacy.Public); NotesList.DataSource = annotations; NotesList.DataBind(); } if (Request.IsAuthenticated && Contact != null) { var dataAdapter = CreateAlertDataAdapter(); var hasAlert = dataAdapter.HasAlert(Contact.ToEntityReference()); AddAlert.Visible = !hasAlert; RemoveAlert.Visible = hasAlert; } else { AddAlertLoginLink.Visible = true; } DisplaySlaDetails(serviceRequestType); } }