Check Requirement Level through Code

Some time we need to check requirement level of specific attribute in run time, this can be done using metadata webservice. For example I am taking accountnumber attribute of account entity, I have changed it’s Requirement level to Business Required for my demo purpose so let’s check through code, I have created a website project in Visual studio with default aspx form and a command button on it, as metadata webservic reference to your website project. I have written below code onclick of that button

           CRMMetadata.CrmAuthenticationToken token = new CRMMetadata.CrmAuthenticationToken();

            token.OrganizationName = “OrgName“; //your organization name

            token.AuthenticationType = 0;

            // Create the metadata Web service.

            MetadataService metadataService = new MetadataService();

            metadataService.Url = “Metadatawebservice URL“; //you need replace it with metadatawebservice url

            metadataService.CrmAuthenticationTokenValue = token;

            metadataService.Credentials = System.Net.CredentialCache.DefaultCredentials;

            metadataService.PreAuthenticate = true;

            // Create the request.

            RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest();

            attributeRequest.EntityLogicalName = EntityName.account.ToString();

            attributeRequest.LogicalName = “accountnumber“; //I want to check account number

            attributeRequest.RetrieveAsIfPublished = false;

            RetrieveAttributeResponse attributeResponse = (RetrieveAttributeResponse)metadataService.Execute(attributeRequest);

string RequirementLevel = attributeResponse.AttributeMetadata.RequiredLevel.Value.ToString();

you will get value of  RequirmentLevel as “Required” if it is set as “Business Required” or if it is set to “Business Recommended” it will return you “Recommended”

Hope it can help somebody !!!

Leave a comment

Filed under Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s