CRM Service Extensions and Dynamic Entity Extensions helper
Useful extension methods for Dynamics CRM 4.0 :
My ex-colleague wrote this I believe - visit his blog for more detail: http://loosechainsaw.com
CRM Service
Dynamic Entity
My ex-colleague wrote this I believe - visit his blog for more detail: http://loosechainsaw.com
CRM Service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | using System; using System.Collections.Generic; using Microsoft.Crm.Sdk; using Microsoft.Crm.Sdk.Query; using Microsoft.Crm.SdkTypeProxy; namespace My.Plugins { public static class CrmServiceExtensions { public static DynamicEntity GetEntityByIdentity( this CrmService crmService, string entityName, string identityFieldName, Guid identity) { QueryExpression query = new QueryExpression(); query.EntityName = entityName; query.ColumnSet = new AllColumns(); query.Criteria.AddCondition(identityFieldName, ConditionOperator.Equal, identity); RetrieveMultipleRequest request = new RetrieveMultipleRequest(); request.ReturnDynamicEntities = true ; request.Query = query; RetrieveMultipleResponse retrieved = (RetrieveMultipleResponse)crmService.Execute(request); if (retrieved.BusinessEntityCollection.BusinessEntities.Count > 0) return (DynamicEntity)retrieved.BusinessEntityCollection.BusinessEntities[0]; return default (DynamicEntity); } public static DynamicEntity GetEntityByIdentity( this ICrmService crmService, string entityName, string identityFieldName, Guid identity) { QueryExpression query = new QueryExpression(); query.EntityName = entityName; query.ColumnSet = new AllColumns(); query.Criteria.AddCondition(identityFieldName, ConditionOperator.Equal, identity); RetrieveMultipleRequest request = new RetrieveMultipleRequest(); request.ReturnDynamicEntities = true ; request.Query = query; RetrieveMultipleResponse retrieved = (RetrieveMultipleResponse)crmService.Execute(request); if (retrieved.BusinessEntityCollection.BusinessEntities.Count > 0) return (DynamicEntity)retrieved.BusinessEntityCollection.BusinessEntities[0]; return default (DynamicEntity); } public static DynamicEntity GetEntityByColumn( this CrmService crmService, string entityName, string columnName, object value) { QueryExpression query = new QueryExpression(); query.EntityName = entityName; query.ColumnSet = new AllColumns(); query.Criteria.AddCondition(columnName, ConditionOperator.Equal, value); RetrieveMultipleRequest request = new RetrieveMultipleRequest(); request.ReturnDynamicEntities = true ; request.Query = query; RetrieveMultipleResponse retrieved = (RetrieveMultipleResponse)crmService.Execute(request); if (retrieved.BusinessEntityCollection.BusinessEntities.Count > 0) return (DynamicEntity)retrieved.BusinessEntityCollection.BusinessEntities[0]; return default (DynamicEntity); } public static DynamicEntity GetEntityByColumn( this ICrmService crmService, string entityName, string columnName, object value) { QueryExpression query = new QueryExpression(); query.EntityName = entityName; query.ColumnSet = new AllColumns(); query.Criteria.AddCondition(columnName, ConditionOperator.Equal, value); RetrieveMultipleRequest request = new RetrieveMultipleRequest(); request.ReturnDynamicEntities = true ; request.Query = query; RetrieveMultipleResponse retrieved = (RetrieveMultipleResponse)crmService.Execute(request); if (retrieved.BusinessEntityCollection.BusinessEntities.Count > 0) return (DynamicEntity)retrieved.BusinessEntityCollection.BusinessEntities[0]; return default (DynamicEntity); } public static DynamicEntity GetEntityByCriteria( this CrmService crmService, string entityName, List<conditionexpression> searchCriteria) { QueryExpression searchExpression = new QueryExpression(); searchExpression.EntityName = entityName; searchExpression.ColumnSet = new AllColumns(); if (searchCriteria.Count > 0) foreach (var conditionExpression in searchCriteria) searchExpression.Criteria.AddCondition(conditionExpression); RetrieveMultipleRequest request = new RetrieveMultipleRequest(); request.ReturnDynamicEntities = true ; request.Query = searchExpression; RetrieveMultipleResponse response = (RetrieveMultipleResponse)crmService.Execute(request); if (response.BusinessEntityCollection.BusinessEntities.Count > 0) return (DynamicEntity)response.BusinessEntityCollection.BusinessEntities[0]; return null ; } public static List<dynamicentity> SelectManyDynamicEntity( this CrmService crmService, string entityName, string searchAttributeName, string searchAttributeValue, ColumnSetBase columnSet) { List<dynamicentity> retrievedDynamicEntities = new List<dynamicentity>(); QueryExpression query = new QueryExpression(); query.EntityName = entityName; query.ColumnSet = columnSet; if (! string .IsNullOrEmpty(searchAttributeName) && ! string .IsNullOrEmpty(searchAttributeValue)) query.Criteria.AddCondition(searchAttributeName, ConditionOperator.Equal, searchAttributeValue); RetrieveMultipleRequest request = new RetrieveMultipleRequest(); request.ReturnDynamicEntities = true ; request.Query = query; RetrieveMultipleResponse retrieved = (RetrieveMultipleResponse)crmService.Execute(request); if (retrieved.BusinessEntityCollection.BusinessEntities.Count > 0) { foreach (BusinessEntity businessEntity in retrieved.BusinessEntityCollection.BusinessEntities) { retrievedDynamicEntities.Add((DynamicEntity)businessEntity); } } return retrievedDynamicEntities; } public static List<dynamicentity> SelectManyDynamicEntityWithSpecifiedConditions( this CrmService crmService, string entityName, List<conditionexpression> conditionExpressions, ColumnSetBase columnSet) { List<dynamicentity> entities = new List<dynamicentity>(); QueryExpression searchQuery = new QueryExpression(); searchQuery.EntityName = entityName; searchQuery.ColumnSet = columnSet; if (conditionExpressions.Count > 0) foreach (var conditionExpression in conditionExpressions) searchQuery.Criteria.AddCondition(conditionExpression); RetrieveMultipleRequest request = new RetrieveMultipleRequest(); request.ReturnDynamicEntities = true ; request.Query = searchQuery; RetrieveMultipleResponse retrievedItems = (RetrieveMultipleResponse)crmService.Execute(request); if (retrievedItems.BusinessEntityCollection.BusinessEntities.Count > 0) foreach (var businessEntity in retrievedItems.BusinessEntityCollection.BusinessEntities) entities.Add((DynamicEntity)businessEntity); return entities; } } } |
Dynamic Entity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | using System; using Microsoft.Crm.Sdk; namespace My.Plugins { public static class DynamicEntityExtensions { public static int GetIntegerStatusValueFor( this DynamicEntity entity, string property) { if (entity.Properties.Contains(property)) return ((Status)(entity.Properties[property])).Value; return Int32.MinValue; } public static Guid GetLookupAsGuidValueOfPropertyFor( this DynamicEntity entity, string property) { if (entity.Properties.Contains(property)) return new Guid(((Lookup)(entity.Properties[property])).Value.ToString()); return Guid.Empty; } public static Guid GetCustomerAsGuidValueOfPropertyFor( this DynamicEntity entity, string property) { if (entity.Properties.Contains(property)) return new Guid(((Customer)(entity.Properties[property])).Value.ToString()); return Guid.Empty; } public static string GetLookupAsStringValueOfPropertyFor( this DynamicEntity entity, string property) { if (entity.Properties.Contains(property)) return (((Lookup)(entity.Properties[property])).name.ToString()); return String.Empty; } public static string GetStringStatusValueFor( this DynamicEntity entity, string property) { if (entity.Properties.Contains(property)) return ((Status)(entity.Properties[property])).name; return String.Empty; } 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 Int32.MinValue; } 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 Double.MinValue; } public static double GetDoubleValueFor( this DynamicEntity entity, string property) { if (entity.Properties.Contains(property)) return ((CrmDouble)(entity.Properties[property])).Value; return Double.MinValue; } public static decimal GetDecimalValueFor( this DynamicEntity entity, string property) { if (entity.Properties.Contains(property)) return ((CrmDecimal)(entity.Properties[property])).Value; return Decimal.MinValue; } public static decimal GetMoneyValueFor( this DynamicEntity entity, string property) { if (entity.Properties.Contains(property)) return ((CrmMoney)(entity.Properties[property])).Value; return Decimal.MinValue; } public static DateTime GetDateTimeValueFor( this DynamicEntity entity, string property) { if (entity.Properties.Contains(property)) return ((CrmDateTime)(entity.Properties[property])).UserTime; return DateTime.MinValue; } public static void ApplyActionTo( this DynamicEntity entity, Action<dynamicentity> action) { action(entity); } public static bool PropertyExistedButNotChanged(DynamicEntity current_employee_entity, DynamicEntity old_employee_entity, string property) { var oldcontains = old_employee_entity.Properties.Contains(property); var currentcontains = current_employee_entity.Properties.Contains(property); bool changed = true ; if (oldcontains && !currentcontains) changed = false ; return changed; } public static bool BothContainProperty(DynamicEntity current_employee_entity, DynamicEntity old_employee_entity, string property) { var oldcontains = old_employee_entity.Properties.Contains(property); var currentcontains = current_employee_entity.Properties.Contains(property); return oldcontains && currentcontains; } public static bool PropertyAdded(DynamicEntity current_employee_entity, DynamicEntity old_employee_entity, string property) { var oldcontains = old_employee_entity.Properties.Contains(property); var currentcontains = current_employee_entity.Properties.Contains(property); bool changed = false ; if (!oldcontains && currentcontains) changed = true ; return changed; } public static void AssignStringValueForField( this DynamicEntity entity, string property, string value) { if (entity.Properties.Contains(property)) entity.Properties[property] = value; else entity.Properties.Add( new StringProperty(property, value)); } public static void AssignBooleanValueForField( this DynamicEntity entity, string property, bool value) { if (entity.Properties.Contains(property)) entity.Properties[property] = new CrmBoolean(value); else entity.Properties.Add( new CrmBooleanProperty(property, new CrmBoolean(value))); } public static void AssignPicklistValueForField( this DynamicEntity entity, string property, int value) { if (entity.Properties.Contains(property)) entity.Properties[property] = new Picklist(value); else entity.Properties.Add( new PicklistProperty(property, new Picklist(value))); } public static void AssignFloatValueForField( this DynamicEntity entity, string property, double value) { if (entity.Properties.Contains(property)) entity.Properties[property] = new CrmFloat(value); else entity.Properties.Add( new CrmFloatProperty(property, new CrmFloat(value))); } public static void AssignDecimalValueForField( this DynamicEntity entity, string property, decimal value) { if (entity.Properties.Contains(property)) entity.Properties[property] = new CrmDecimal(value); else entity.Properties.Add( new CrmDecimalProperty(property, new CrmDecimal(value))); } public static void AssignMoneyValueForField( this DynamicEntity entity, string property, decimal value) { if (entity.Properties.Contains(property)) entity.Properties[property] = new CrmMoney(value); else entity.Properties.Add( new CrmMoneyProperty(property, new CrmMoney(value))); } public static void AssignNumberValueForField( this DynamicEntity entity, string property, int value) { if (entity.Properties.Contains(property)) entity.Properties[property] = new CrmNumber(value); else entity.Properties.Add( new CrmNumberProperty(property, new CrmNumber(value))); } public static void AssignDateTimeValueForField( this DynamicEntity entity, string property, DateTime value) { if (entity.Properties.Contains(property)) entity.Properties[property] = CrmDateTime.FromUniversal(value); else entity.Properties.Add( new CrmDateTimeProperty(property, CrmDateTime.FromUniversal(value))); } public static void AssignLookupValueForField( this DynamicEntity entity, string property, Lookup value) { if (entity.Properties.Contains(property)) entity.Properties[property] = value; else entity.Properties.Add( new LookupProperty(property, value)); } } } |
Comments
Post a Comment