Retrieve Entity Metadata programatically in Microsoft Dynamics 365 CRM

What Metadata does MSCRM has

MSCRM holds metadata for “Entities”

It has Metadata for fields,Relationships(one to Many, Many to One), Privileges and its Properties  i am just naming the commonly used or heard properties.

  • DisplayName
  • SchemaName
  • AttributeType
  • IsAuditEnabled
  • IsRenameable
  • IsManaged
  • IsPrimaryId

How to See EntityMetadata

One can get the metadata Information from the MetadataBrowser solution which needs to be installed in MSCRM Solutions (or)

from XRMToolbox Plugin. (or)

you can do on your own by programtically, using MSCRM SDK..

  • EntityMetadata is metadata for the entity.
  • EntityMetadata has a attributes collection which contains a list of AttributeMetadata fields.

To retrieve the Entity Metadata we need to make RetrieveEntityRequest for which the entity response is captured in the RetrieveEntityResponse by using “Execute” method from Organization Service.

Add the Namespace Microsoft.Xrm.Sdk.Metadata

The required parameters are:

  • EntityFilters(enum)
    • A filter to control how much and what data about the entity is returned .
    • EntityFilters is of type enum and has following options to select (Entity,Default,Attributes,Privileges,Relationships,All)
  • RetrieveAsIfPublished
    • Set true to include unpublished changes to Entity,
    • Setting to false includes only published changes to the Entity.

Either LogicalName or MetadataId is required to inform the request which Entity it is you wish to be returned.

below is the code it looks like

private static EntityMetadata RetriveEntityMetaData(IOrganizationService service, string entityName)
{
RetrieveEntityRequest req = new RetrieveEntityRequest()
{
EntityFilters = EntityFilters.Attributes,
LogicalName = entityName,
RetrieveAsIfPublished = true
};

RetrieveEntityResponse res = (RetrieveEntityResponse)service.Execute(req);
return res.EntityMetadata;
}

AttributeMetadata class is the base class for attributeMetaData and then there are individual attributeMetaData types

  • AttributeMetadata class
    • LookupAttributeClass
    •  DecimalAttributeClass
    •  PicklistAttributeClass
    •  StatusAttributeClass
    • All the other attribute classes

You can see the different types of attribute metadata below from base class

attributemetaata

The below code will be used to retrieve the Attribute Metadata of the entity

var entityMetadata = RetriveEntityMetaData(_service, “contact”);
List<AttributeMetadata> FieldMetadataList = new List<AttributeMetadata>();
FieldMetadataList = entityMetadata.Attributes.ToList();

foreach (var fields in FieldMetadataList)
{
Console.WriteLine(string.Format(“DisplayName: {0}”,      fields.DisplayName.UserLocalizedLabel.Label));

}

where the Displayname retrieval is quite tricky, as there will be null values in “UserLocalizedLabel“. so it will be advised to check for null values before (fields.DisplayName.UserLocalizedLabel.Label)

Below is the output:

contact metadata

Email Router Configurations in MS-CRM 2016 On-Premise

Pre-Requisites

  1. Dynamics CRM On-Premise is installed in the server. An On-premise Organization should be created in the server.
  2. Email Router Configuration Manager exe (Download link: https://www.microsoft.com/en-us/download/details.aspx?id=50373)
  3. Information regarding Mailboxes to connect to a user
  4. Server name/URL FQDN for Exchange Web Services & Version (2007/2010/2013/), or a Pop3 server address.
  • An exchange address will be in this format (http(s)://Server.domain.com/EWS/Exchange.asmx )
  • For Exchange, you can often just append the /EWS/Exchange.asmx to the address they use for Outlook Web Access.
  • If you access OWA via a URL such as https://Mail.MyCompanyDomain.com/OWA. Just replace /OWA with /EWS/Exchange.asmx This will look like  https://Mail.MyCompanyDomain.com/EWS/Exchange.asmx
  • A Pop3 server would have just a server.domain.com format.
  1. We need User Credentials for configuring Mail boxes (usually windows credentials). Credentials will be in the format of an email address (e.g. : username@domain.com) or domain\username.

Microsoft Dynamics CRM URL is required. When we create an organization the CRM web application will be created. URL format looks like https://CRMCompany.CompanyDomain.COM:444  or https://ipaddress:444/

Email Router Configurations on MS-CRM:

  1. Go to Settings –> Email Configurations.
  2. Click on Email Server Profile.1
  3. Click on New Button and Select POP3-SMTP Profile as Email Server Profile.
  4. Enter the following details.3
  5. Click on save and close button.
  6. Go to settings –> Email Configurations –> Mail Boxes.4
  7. Select Active mail Boxes View.
  8. Select the mail box which you want to set and double click on it, Window Opens.10
  9. Enter details and Click on Save button the form.11
  10. Click on Approve EmailNote: Mail boxes will be created from the Teams and Queues we have created, When we create an User in CRM, Provide Email address of the User. Approve the Email. Create a team in MSCRM, add Users to that team (In my case I want to configure Customer service Email , So I have Created a Team in MSCRM with Customer Service name. a Queue is automatically created and assigned to Team. Mailbox is also created against the team in MSCRM).

Setting up the Email Router

  1. Open the Email router configuration manager in the server

5

Configurations profile tab

  1. On Configurations profile tab, you will have to create profile for both directions (incoming and outgoing)6
  2. Click on New button, Email Router Configuration window opens
  3. Enter the following details in the window (Incoming Email Configuration)
  • Profile Name: Incoming
  • Direction: Select Incoming option from dropdown.
  • Email Server Type: Exchange (2007/2010/2013) or POP3 Email Boxes. I am selecting Exchange 2010 0r 2013 option.
  • Protocol: Select Exchange web service.
  • Authentication Type: Select Windows Authentication from dropdown for more information on Authentication types of Email Router (https://technet.microsoft.com/en-us/library/hh699786.aspx)
  • Location : Enter the URL of Exchange server
  • Access Credentials : Select User Specified option from drop down

7

  1. Outgoing E-mail configuration (doing it with SMTP configurations).

Note: Configure the system as per the requirements. Below is the screen shot for outgoing email

8

Note: If you elect to user Exchange for the Outgoing type, you must specify what type of access credentials you are using. Select Administrator if you have an admin account that has permission to all users’ mailboxes, or if you just want to specify the credentials for one user. This can be any user, it does not have to be a Dynamics CRM admin, a windows admin, or anything like that

Deployments Tab

  1. On the Deployments Tab, you can connect to one or more deployments of Dynamics CRM, (perhaps you have a production and a test deployment, and here you can create a connection to both of them).
  • Choose what deployment type you have
  • Dynamics CRM Online if you are using Office 365, my company would be on premise, or online service provider for a 3rd party hosted installation of Dynamics CRM.
  • Enter the URL to your Dynamics CRM Organization.
  • Enter in the credentials for a user that has access to login to Dynamics CRM. This is simply to read settings from Dynamics CRM, and write them back to the system. Usually a system admin, with a full Dynamics CRM license (not just administrative license).
  • Optionally, select a default Profile for the deployment. This will set all users to use this profile in that deployment. The Profile you select will be the mailbox/server that the user(s) will send and receive from.

 

Users, Queues and Forward Mailboxes Tab

  1. Select your deployment and click Load Data.

Note: (Troubleshooting tip) If you get an error at this point, it is a problem with the settings on the deployments tab. Go back, and verify the URL by pasting it into a web browser, and verify the username and password by logging into Dynamics CRM with it.

9

12

2. Once the data loads, you will see all of your users that are set (on the Dynamics CRM user account inside of Dynamics CRM), to use the Email Router as either their incoming or outgoing email method.

13

3. Select a user, and click test Access. (If no profile is assigned because you left that blank on the deployments tab, double click the user first and assign a profile).

Note: Access Denied? Ok, here’s the catch. The user account you setup on the Configuration Profile, that’s whose mailbox you are connecting to. If the user you are testing access for, doesn’t have access to the user’s mailbox that you used (access credential), on the configuration profile, you will get an access denied, (or unable to relay) message. The email address on the user you are assigning to the profile, must match the email address of the mailbox.

14

4. Once Succeed, Click on Publish button.