CrmBooleanControl

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.

Programming the CrmBooleanControl

Adding theCrmBooleanControl class

  1. Right-click the ProgrammingWithDynamicsCrm4.Controls Project in Solution Explorer, and then, under Add, click New Item.

  2. In the Visual C# Items category, select the Class template.

  3. 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"));
        }
    }
}

CrmBooleanControl Properties

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.

Table 14-2. CrmBooleanControl Properties

Property Name

Type

Description

EntityName

string

Name of the Microsoft Dynamics CRM entity that the picklist attribute belongs to

AttributeName

string

Name of the picklist attribute

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.

Adding Options to the CrmBooleanControl

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.

Testing the CrmBooleanControl

Adding theCrmBooleanControl test page

  1. Open the ProgrammingWithDynamicsCrm4.Web project in Visual Studio.

  2. Right-click the project name in Solution Explorer, and click Add New Item.

  3. Select the Web Form template, and type the name CrmBooleanControlPage.aspx in the Name box. Click OK.

  4. 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).

CrmBooleanControl

Figure 14-2. CrmBooleanControl

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset