web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :

Dynamics CRM 2011 Portal Development – CrmMetadataDataSource

Zhongchen Zhou Profile Picture Zhongchen Zhou 681

Dynamics CRM 2011 Portal Development

CrmMetadataDataSource data source control connects to Dynamics CRM and retrieve  Metadata and make it available for other controls to bind to, without requiring code. It only supports retrieving data.

It can retrieve metadata for all entities (EntityMetadata[]) filtered by EntityFilters enum value set through MetadataFlags, attributes metadata for one single entity (AttributeMetadata[]) filter by EntityFilters enum value set through EntityFlags, and attribute metadata of type PicklistAttributeMetadata/StatusAttributeMetadata/StateAttributeMetadata  for one single entity (IEnumerable <new { sting OptionLabel, int OptionValue}>) ordered by value specified in SortExpression.

SelectParameters is ignored while retrieve metadata from Dynamics CRM.

Retrieve metadata for all entities

To retrieve metadata for all entities, leave both EntityName and AttributeName empty, and optionally specify MetadataFlags to limit the amount of information returned. MetadataFlags takes a value of EntityFilters type.

[Flags]
 public enum EntityFilters
 {
 All = 15,
 Attributes = 2,
 Default = 1,
 Entity = 1,
 Privileges = 4,
 Relationships = 8
 }

Example:

<crm:CrmMetadataDataSource ID="CrmMetadataDataSource1" runat="server" MetadataFlags="Attributes">
</crm:CrmMetadataDataSource>
<asp:GridView ID="GridView1" runat="server" DataSourceID="CrmMetadataDataSource1">
<Columns>
 <asp:BoundField DataField="DisplayName.UserLocalizedLabel.Label" HeaderText="Display Name" />
 <asp:TemplateField HeaderText="First Attribute">
 <ItemTemplate>
 <asp:Literal runat="server" Text='<%# Eval("Attributes[0].DisplayName.UserLocalizedLabel.Label") %>' />
 </ItemTemplate>
 </asp:TemplateField>
 <asp:TemplateField HeaderText="Second Attribute">
 <ItemTemplate>
 <asp:Literal runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Attributes[1].DisplayName.UserLocalizedLabel.Label") %>' />
 </ItemTemplate>
 </asp:TemplateField>
 <asp:TemplateField HeaderText="Third Attribute">
 <ItemTemplate>
 <asp:Literal runat="server" Text='<%# ((EntityMetadata)Container.DataItem).Attributes[2].DisplayName.UserLocalizedLabel == null ? string.Empty : ((EntityMetadata)Container.DataItem).Attributes[2].DisplayName.UserLocalizedLabel.Label.ToLower() %>' />
 </ItemTemplate>
 </asp:TemplateField>
 </Columns>
 </asp:GridView>

Result:

Tips on Data-Binding Expressions

1.

<%# Eval("expression") %>

is a short cut for

<%# DataBinder.Eval(Container.DataItem, "expression") %>

2. Container.DataItem can be used within <%# and %> delimiters to refer to  the current data item of the naming container.

3. Any publicly scoped code within the <%# and %>delimiters to execute that code and return a value during page processing.

4. For the cast ((EntityMetadata)Container.DataItem) in the example to work, register Microsoft.Xrm.Sdk.Metadata namespace in web.config

<system.web>
    <pages viewStateEncryptionMode="Always" validateRequest="true">
        <namespaces>
            <add namespace="Microsoft.Xrm.Sdk.Metadata"/>
        </namespaces>
    </pages>
</system.web>

More information regarding to Data-Binding Expressions can be found Data-Binding Expressions Overview and DataBinder.Eval Method

Notes:

At the moment there is a bug in Microsoft.Xrm.Client.Messages.OrganizationServiceContextExtensions.RetrieveAllEntities(this OrganizationServiceContext service, EntityFilters entityFilters = 1, bool retrieveAsIfPublished = false) method. Parameter with name “MetadataId” should not be added to the organization request, because this cause an exception with message “Unrecognized request parameter: MetadataId”.

Retrieve metadata for one single entity

To retrieve metadata for one single entity, leave AttributeName empty, specify EntityName and optionally specify EntityFlags  to limit the amount of information returned. EntityFlags  takes a value of EntityFilters type.

Example


<crm:CrmMetadataDataSource ID="CrmMetadataDataSource2" runat="server" EntityName="account" EntityFlags="All">
 </crm:CrmMetadataDataSource>
 <asp:GridView ID="GridView2" runat="server" DataSourceID="CrmMetadataDataSource2">
 <Columns>
 <asp:BoundField DataField="DisplayName.UserLocalizedLabel.Label" HeaderText="Display Name" />
 </Columns>
 </asp:GridView>

Result

Retrieve metadata for one attribute from specified entity

To retrieve metadata for one attribute from specified entity, specify AttributeName and EntityName and optionally specify SortExpression to control the order (Value/OptionValue, Label/OptionLabel, State, DefaultStatus can be used to sort).

This is limited to attributes of type type PicklistAttributeMetadata, StatusAttributeMetadata or StateAttributeMetadata only. The return value will be of type (IEnumerable <new { sting OptionLabel, int OptionValue}>).

Example:


<crm:CrmMetadataDataSource ID="CrmMetadataDataSource3" runat="server" EntityName="account" AttributeName="industrycode" SortExpression="Label DESC">
 </crm:CrmMetadataDataSource>
 <asp:GridView ID="GridView3" runat="server" DataSourceID="CrmMetadataDataSource3">
 </asp:GridView>


Result:

More Information can be found Use CrmMetadataDataSource Control


This was originally posted here.

Comments

*This post is locked for comments