Work with Attributes
This topic uses code snippets from the Sample: Work with Attributes to show how to perform common tasks when working with attributes.
In This Topic
Create a Picklist That Uses a Global Option Set
Insert a New Option in a Local Option Set
Create 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.
Retrieve an Attribute
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.
Note |
|---|
| 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. For more information, see Retrieving unpublished metadata. |
Update an Attribute
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.
Create a Lookup Attribute
This sample shows how to create a lookup attribute.
A lookup attribute is created by using the CreateOneToManyRequest.
Create a Picklist That Uses a Global Option Set
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. For more information, see Customize Global Option Sets.
Insert a New Status Value
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.
Update a State Value
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.
You cannot add or remove StateCode options, but you can change the labels for the options.
Insert a New Option in a Local Option Set
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.
Change the Order of Options in a Local Option Set
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 OrderByLINQ 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.
Delete an Attribute
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.
See Also
Microsoft Dynamics CRM 2011
Send comments about this topic to Microsoft.
© 2013 Microsoft Corporation. All rights reserved.
Note