All-in-One Guide to C# Plugin Data Types: Working with Strings, Currency, Lookups, and More

In Dynamics 365 and Power Platform, C# plugins play a crucial role in extending the functionality of your applications. One of the key aspect’s developers need to grasp is how to handle various data types effectively. This guide will walk you through the most commonly used data types in C# plugins, including strings, currency, lookups, option sets, and more.

Introduction to C# Plugins

C# plugins are custom business logic that you can implement in Dynamics 365 to execute in response to specific events, like creating, updating, or deleting records. They allow you to manipulate data and interact with the system in powerful ways. Understanding how to work with different data types is essential for writing effective plugins.

Retrieving Entities

Before you can manipulate data types, you first need to retrieve the entity record you want to work with. Here’s how you can do that:

public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context =(IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
// Retrieve entity by ID
Entity entity = service.Retrieve(“entity_logical_name”, context.PrimaryEntityId, new ColumnSet(true));
}  

Working with Different Data Types

String

The most common data types are strings. They can represent text values.

Getting a String Value:string name = entity.GetAttributeValue<string>(“string_attribute_name”);
Setting a String Value:entity[“string_attribute_name”] = “New String Value”;

Currency

Currency is represented by the Money type in Dynamics 365.

Getting a Currency Value:Money amount = entity.GetAttributeValue<Money>(“currency_attribute_name”);
decimal currencyValue = amount?.Value ?? 0;
Setting a Currency Value:entity[“currency_attribute_name”] = new Money(150.00m); // Set to 150.00

Lookups

Lookup fields refer to related entities. They are represented by the EntityReference type.

Getting a Lookup Value:EntityReference lookup = entity.GetAttributeValue<EntityReference>(“lookup_attribute_name”);
if (lookup != null)
{    
Guid lookupId = lookup.Id;     string lookupName = lookup.Name; // Requires another retrieve call to get the name
}
Setting a Lookup Value:entity[“lookup_attribute_name”] = new EntityReference(“related_entity_logical_name”, lookupId);

Option Sets (Picklists)

Option sets are used to represent a list of choices. They are represented by the OptionSetValue type.

Getting an Option Set Value:OptionSetValue optionSetValue = entity.GetAttributeValue<OptionSetValue>(“optionset_attribute_name”); int selectedValue = optionSetValue?.Value ?? 0;
Setting an Option Set Value:entity[“optionset_attribute_name”] = new OptionSetValue(1); // Assuming 1 is a valid option

Multiselect Option Set

Multiselect option sets allow multiple selections from a list.

Getting a Value:IEnumerable<OptionSetValue> multiSelectOptions = entity.GetAttributeValue<IEnumerable<OptionSetValue>>(“multiselect_optionset”);
Setting a Value:entity[“multiselect_optionset”] = new List<OptionSetValue>
{    
new OptionSetValue(1),    
new OptionSetValue(2)
}; // Assuming 1 and 2 are valid options

Boolean Values or Two Options

Boolean fields represent true/false values.

Getting a Boolean Value:bool? isActive = entity.GetAttributeValue<bool?>(“boolean_attribute_name”);
Setting a Boolean Value:entity[“boolean_attribute_name”] = true; // or false
Getting a Boolean Value:OptionSetValue twoOptionsValue = entity.GetAttributeValue<OptionSetValue>(“two_options_attribute”); bool isSelected = twoOptionsValue?.Value == 1; // Assuming 1 is ‘Yes’
Setting a Boolean Value:entity[“two_options_attribute”] = new OptionSetValue(1); // Set to ‘Yes’

DateTime

DateTime fields are used for date and time information.

Getting a DateTime Value:DateTime? createdOn = entity.GetAttributeValue<DateTime?>(“datetime_attribute_name”);
Setting a DateTime Value:entity[“datetime_attribute_name”] = DateTime.UtcNow; // Set to current date and time

Image

Image fields store binary data like photos or documents.

Getting an Image:var image = entity.GetAttributeValue<EntityImage>(“image_attribute”);
byte[] imageData = image?.ImageData; // Assuming you handle the image type properly

Whole Number

Whole numbers are represented as int.

Getting a Value:int wholeNumber = entity.GetAttributeValue<int>(“whole_number_attribute”);
Setting a Value:entity[“whole_number_attribute”] = 42;

Floating Point Number

Floating point numbers allow for decimal values.

Getting a Value:float floatingPointNumber = entity.GetAttributeValue<float>(“floating_point_attribute”);
Setting a Value:entity[“floating_point_attribute”] = 3.14f;

Decimal Number

Decimal fields store precise decimal values.

Getting a Value:decimal decimalNumber = entity.GetAttributeValue<decimal>(“decimal_number_attribute”);
Setting a Value:entity[“decimal_number_attribute”] = 123.45m;

Setting Data Types

After retrieving the desired values and possibly making changes, you need to update the entity. This is done using the Update method.

service.Update(entity);   

Some Additional Data Types you Might Encounter in Dynamics 365 C# Plugins such as :

1. Guid

  • Used for unique identifiers, especially in custom or unique fields outside of standard lookup references.
Getting a Value:Guid uniqueId = entity.GetAttributeValue<Guid>(“unique_attribute”);
Setting a Value:entity[“unique_attribute”] = new Guid(“d3c1d9c8-7438-44b5-91b1-f40241b0f84d”);

2. Composite Fields

  • Composite fields are fields with multiple parts, often seen in address fields (e.g., street, city, state).
Getting a Value:string city = entity.GetAttributeValue<string>(“address1_city”); string state = entity.GetAttributeValue<string>(“address1_stateorprovince”);
Setting a Value:entity[“address1_city”] = “Seattle”;
entity[“address1_stateorprovince”] = “WA”;

3. Unique Identifier (Primary Key)

  • Id provides the primary key of the record, often used when retrieving or updating records
Getting a Value:Guid entityId = entity.Id;
Setting a Value:Entity newEntity = new Entity(“entity_logical_name”)
{    
Id = Guid.NewGuid()
};

4. PartyList (Used in Activities)

  • PartyList fields represent participants in activities like emails and appointments, allowing multiple users or entities.
Getting a Value:string city = entity.GetAttributeValue<string>(“address1_city”);
EntityCollection partyList = entity.GetAttributeValue<EntityCollection>(“to”); foreach (Entity party in partyList.Entities)
{     EntityReference partyRef = party.GetAttributeValue<EntityReference>(“partyid”);    
// Do something with partyRef
}  
Setting a Value:Entity party = new Entity(“activityparty”);
party[“partyid”] = new EntityReference(“contact”, new Guid(“c3e4b159-64af-4c3d-b894-6d62007dbe79”));  

EntityCollection partyList = new EntityCollection(new List<Entity> { party });
entity[“to”] = partyList;

Conclusion

Handling various data types in Dynamics 365 plugins is critical for creating robust and adaptable solutions. Knowing how to work with fields such as text, currencies, lookups, option sets, and others enables you to manage data more precisely and conduct custom activities in our CRM. With these examples, you now have the ability to retrieve and set values for a variety of common data types, making it easy to create plugins that match our organization’s requirements. As you gain experience, you’ll notice that handling data in plugins becomes easier, allowing you to focus on developing smart and effective solutions.

We hope you found this article useful, and if you would like to discuss anything, you can reach out to us at transform@cloudfronts.com


Share Story :

SEARCH BLOGS :

FOLLOW CLOUDFRONTS BLOG :


Secured By miniOrange