Work with attribute metadata

 

Applies To: Dynamics CRM 2015

This topic uses code snippets from the Sample: Work with attribute metadata to show how to perform common tasks when working with attributes.

You create attributes by defining one of the AttributeMetadata types and then passing it to the CreateAttributeRequest message.

The following sample defines the AttributeMetadata for a number of different types of attributes and adds them to a List<AttributeMetadata>. At the end of the code the attribute definitions are passed to an instance of the CreateAttributeRequest class and the attribute is created using the Execute method.

The following sample assumes that the current customization prefix is ‘new’ because that is the default customization prefix for the organization solution publisher. You should use the customization prefix for the solution publisher that makes sense for your solution context.

No code example is currently available or this language may not be supported.

This sample shows how to retrieve the AttributeMetadata for an attribute using the RetrieveAttributeRequest. This sample retrieves the metadata for a custom StringAttributeMetadata attribute called ‘new_string’ from the Contact entity that was created in Create attributes.

System_CAPS_noteNote

Because RetrieveAsIfPublished is true, this request returns the current unpublished definition of this attribute. You might use this if you are creating an Attribute editor and you want to retrieve the unpublished definition of the attribute. Otherwise, you should not specify RetrieveAsIfPublished. More information: 649ec32a-1ae1-4966-96fa-3543f4a05509#BKMK_RetrieveUnpublishedMetadata

No code example is currently available or this language may not be supported.

This sample shows how to update an attribute. This sample uses the UpdateAttributeRequest to change the AttributeMetadata.DisplayName property of a previously retrieved custom attribute for the Contact entity.

No code example is currently available or this language may not be supported.

This sample shows how to create a lookup attribute.

A lookup attribute is created by using the CreateOneToManyRequest.

No code example is currently available or this language may not be supported.

This sample shows how to create a PicklistAttributeMetadata attribute that is associated with a global option set.

The following sample uses CreateAttributeRequest to set the options for a PicklistAttributeMetadata attribute to use a global option set with a name represented by the string variable _globalOptionSetName. More information: Customize global option sets

No code example is currently available or this language may not be supported.

This sample shows how to insert a new Status Reason option for StatusAttributeMetadata attribute.

The following sample uses the InsertStatusValueRequest to specify a new option for the Contact entity Contact.StatusCode attribute that is valid when the Contact.StateCode is 0 (Active). The IOrganizationService.Execute method processes the request.

The following sample allows two valid Status Reason options for active contacts: Active and Dormant.

No code example is currently available or this language may not be supported.

This sample shows how to change the label for an option in a StateAttributeMetadata attribute.

The following sample uses UpdateStateValueRequest to change the Contact.StateCode option label from Active to Open.

No code example is currently available or this language may not be supported.

You cannot add or remove StateCode options, but you can change the labels for the options.

This sample shows how to add a new option to a local option set. The following sampleuses InsertOptionValueRequest to add a new option to a custom PicklistAttributeMetadata attribute for the Contact entity.

No code example is currently available or this language may not be supported.

This sample shows how to change the order of options in a local option set. The following sampleretrieves a custom PicklistAttributeMetadata attribute and changes the order of the original options using the OrderBy LINQ function to sort items in ascending order by the label text. Then it uses OrderOptionRequest to set the new order of the options for the attribute.

Use the OrderByDecending linq function to order the items in descending order.

No code example is currently available or this language may not be supported.

This sample shows how to delete attributes stored in a List<AttributeMetadata> that were created for the Contact entity in Create attributes. For each AttributeMetadata the DeleteAttributeRequest prepares the request that is processed using IOrganizationService.Execute.

No code example is currently available or this language may not be supported.

© 2016 Microsoft. All rights reserved. Copyright

Community Additions

ADD
Show: