Home > Development > CRM Dynamic Entity Extensions

CRM Dynamic Entity Extensions

September 10, 2010 Leave a comment Go to comments

Dynamic Entities can be a real PITA. Here some code to help lessen the hurt. I have developed a heap of extension methods to let you access field values without all the nasty casts and property contains nonsense.

Some of this is in the new CRM SDK but are not extensions on DynamicEntity itself.

If this helps you then send me a money order 🙂

using System;
using Microsoft.Crm.Sdk;

namespace Microsoft.CRM.ThirdParty.Extensions
{
    public static class DynamicEntityExtensions
    {
        public static string GetStringValueOfPropertyFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return entity.Properties[property].ToString();
            return String.Empty;
        }

        public static string GetKeyAsStringValueOfPropertyFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((Key)(entity.Properties[property])).Value.ToString();
            return String.Empty;
        }

        public static Guid GetKeyAsGuidValueOfPropertyFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return new Guid(((Key)(entity.Properties[property])).Value.ToString());
            return Guid.Empty;
        }

        public static int GetIntegerValueFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((CrmNumber)(entity.Properties[property])).Value;
            return 0;
        }

        public static int GetPicklistValueOfPropertyFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((Picklist)entity.Properties[property]).Value;
            return Int32.MinValue;
        }

        public static string GetPicklistNameOfPropertyFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((Picklist)entity.Properties[property]).name;
            return String.Empty;
        }

        public static double GetFloatValueFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((CrmFloat)(entity.Properties[property])).Value;
            return 0;
        }

        public static double GetDoubleValueFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((CrmDouble)(entity.Properties[property])).Value;
            return 0;
        }

        public static decimal GetDecimalValueFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((CrmDecimal)(entity.Properties[property])).Value;
            return 0;
        }

        public static decimal GetMoneyValueFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((CrmMoney)(entity.Properties[property])).Value;
            return 0;
        }

        public static DateTime GetDateTimeValueFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((CrmDateTime)(entity.Properties[property])).UserTime;
            return DateTime.MinValue;
        }
    }
}

Hope this helps you.

Blair…

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: