We will now create a control to handle the Boolean attribute type. The CrmBooleanControl functions similarly to our CrmPicklistControl. Instead of a select box, this control will render radio buttons with labels and values based on the options defined in the Microsoft Dynamics CRM attribute.
Adding theCrmBooleanControl class
Right-click the ProgrammingWithDynamicsCrm4.Controls Project in Solution Explorer, and then, under Add, click New Item.
In the Visual C# Items category, select the Class template.
Type CrmBooleanControl.cs in the Name box and click OK.
The CrmBooleanControl extends another existing ASP.NET control, the RadioButtonList. We inherit the CrmBooleanControl class from the System.Web.UI.RadioButtonList class. Example 14-2 provides the full source code for the CrmBoolean control.
Example 14-2. CrmBoolean control source code
using System; using System.Collections.Generic; using System.Text; using System.Web.UI.WebControls; using System.ComponentModel; using System.Configuration; using Microsoft.Crm.SdkTypeProxy.Metadata; using Microsoft.Crm.Sdk.Metadata; using ProgrammingWithDynamicsCrm4.Utilities; namespace ProgrammingWithDynamicsCrm4.Controls { public class CrmBooleanControl : RadioButtonList { public string EntityName { get { object entityName = ViewState["EntityName"]; if (entityName != null) return (string)entityName; else return String.Empty; } set { ViewState["EntityName"] = value; } } public string AttributeName { get { object attributeName = ViewState["AttributeName"]; if (attributeName != null) return (string)attributeName; else return String.Empty; } set { ViewState["AttributeName"] = value; } } [Browsable(false)] [EditorBrowsable(EditorBrowsableState.Never)] public override ListItemCollection Items { get { if (base.Items.Count == 0) this.AddListItems(); return base.Items; } } private void AddListItems() { MetadataService metadataService = CrmServiceUtility.GetMetadataService( ConfigurationManager.AppSettings["CrmServer"], ConfigurationManager.AppSettings["OrgName"]); AttributeMetadata attributeMetadata = MetadataUtility.RetrieveAttributeMetadata(metadataService, EntityName, AttributeName); // Verify that the attribute is a valid picklist if (attributeMetadata.AttributeType.Value != AttributeType.Boolean) throw new Exception( String.Format("{0} is not a valid attribute for the Picklist control.", attributeMetadata.AttributeType)); BooleanAttributeMetadata booleanMetadata = (BooleanAttributeMetadata)attributeMetadata; base.Items.Add( new ListItem(booleanMetadata.TrueOption.Label.UserLocLabel.Label, "1")); base.Items.Add( new ListItem(booleanMetadata.FalseOption.Label.UserLocLabel.Label, "0")); } } }
We need to add two properties to the CrmBooleanControl to enable the integration with the Microsoft Dynamics CRM metadata. Table 14-2 lists the properties we need to add to our control.
Just like we did in the CrmPicklistControl, we need to override and hide a property for the CrmBooleanControl. Because we only want this control to display the options defined on our attribute, we need to override and hide the Items collection.
We add items to the CrmBooleanControl in the same way we added the select options to the CrmPicklistControl. This time we verify that the specified attribute has a type of Boolean. We then convert the attribute metadata to BooleanAttributeMetadata. The BooleanAttributeMetadata contains a TrueOption and a FalseOption. We add two new list items using the option’s label and the value of "0" for the false option and "1" for the true option.
Adding theCrmBooleanControl test page
Open the ProgrammingWithDynamicsCrm4.Web project in Visual Studio.
Right-click the project name in Solution Explorer, and click Add New Item.
Select the Web Form template, and type the name CrmBooleanControlPage.aspx in the Name box. Click OK.
Delete the div tag that was added by Visual Studio when the page was created.
If you have not added the tag prefix to your web.config, you need to register a tag prefix for this page as previously described in the section "CrmPicklistControl." We can test the CrmBooleanControl using the Lead entity’s donotemail attribute by adding <crm: CrmBooleanControl iD="CrmBoolean" runat="server" EntityName="lead" Attribute-Name="donotemail" /> inside of the form tag of the CrmBooleanControlPage.aspx:
<crm:CrmBooleanControl ID="CrmBoolean" runat="server" EntityName="lead" AttributeName="donotemail" />
Compile your solution, and view the page in your browser. You will see two radio buttons, one for each option defined on the attribute (Figure 14-2).