Example #1
0
        private static IEnumerable <T> GetEntitiesLightHelper <T>
            (IOrganizationService service, FilterExpression filter)
            where T : Entity
        {
            try
            {
                var logicalName = (Activator.CreateInstance <T>()).LogicalName;

                var query = new QueryExpression(logicalName)
                {
                    ColumnSet = new ColumnSet(logicalName + "id"),
                    PageInfo  = new PagingInfo()
                    {
                        PageNumber = 1
                    },
                    Criteria = filter
                };

                return(GetEntitiesHelper <T>(service, query));
            }
            catch (Exception ex)
            {
                throw new Exception(HU.ErrorMessageWrapper(ex));
            };
        }
Example #2
0
        private static EntityMetadata MetadataHelper <T>
            (IOrganizationService service, EntityFilters filter)
            where T : Entity
        {
            try
            {
                var logicalName = (Activator.CreateInstance <T>()).LogicalName;

                var req     = new OrganizationRequest();
                var params_ = new ParameterCollection();

                params_.Add(@"LogicalName", logicalName);
                params_.Add(@"EntityFilters", filter);
                params_.Add(@"MetadataId", Guid.Empty);
                params_.Add(@"RetrieveAsIfPublished", true);

                req.RequestName = @"RetrieveEntity";
                req.Parameters.AddRange(params_);

                var resp = service.Execute(req);

                var metadata = resp.Results.FirstOrDefault();

                if (null == metadata.Value)
                {
                    return(null);
                }

                return((EntityMetadata)metadata.Value);
            }
            catch (Exception ex)
            {
                throw new Exception(HU.ErrorMessageWrapper(ex));
            };
        }
Example #3
0
        /// <summary>
        /// Executes the plug-in.
        /// </summary>
        /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
        /// <see cref="IPluginExecutionContext"/>,
        /// <see cref="IOrganizationService"/>
        /// and <see cref="ITracingService"/>
        /// </param>
        /// <remarks>
        /// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
        /// The plug-in's Execute method should be written to be stateless as the constructor
        /// is not called for every invocation of the plug-in. Also, multiple system threads
        /// could execute the plug-in at the same time. All per invocation state information
        /// is stored in the context. This means that you should not use global variables in plug-ins.
        /// </remarks>
        protected void ExecuteAccountPostPlugin(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            var isUpdate = MatchesEventOperation(localContext, EventOperation.Update);

            var accountManager = new ManagerAccount(
                localContext.TracingService,
                localContext.PluginExecutionContext,
                localContext.OrganizationService,
                localContext.OrganizationAdminService);

            try
            {
                accountManager.FooPlugin(localContext.PluginExecutionContext.PrimaryEntityId, isUpdate);
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(
                          HU.ErrorMessageWrapper(ex));
            }
        }
Example #4
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="U"></typeparam>
        /// <param name="service"></param>
        /// <param name="id"></param>
        /// <param name="attributeName"></param>
        /// <returns></returns>
        public static IEnumerable <T> GetEntitiesIntersectTable <T, U>
            (IOrganizationService service, Guid id, string attributeName)
            where T : Entity
            where U : Entity
        {
            try
            {
                var filter = new FilterExpression
                {
                    FilterOperator = LogicalOperator.And,
                    Conditions     =
                    {
                        new ConditionExpression
                        {
                            AttributeName = attributeName,
                            Operator      = ConditionOperator.Equal,
                            Values        = { id }
                        }
                    }
                };

                return(GetEntitiesIntersectTableHelper <T, U>(service, filter));
            }
            catch (Exception ex)
            {
                throw new Exception(HU.ErrorMessageWrapper(ex));
            };
        }
Example #5
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="expr"></param>
 /// <returns></returns>
 public static string StringOf <T>(Expression <Func <T, object> > expr)
 {
     try
     {
         return(StaticReflection.GetMemberName <T>(expr).ToLower());
     }
     catch (Exception ex)
     {
         throw new Exception(HU.ErrorMessageWrapper(ex));
     };
 }
Example #6
0
 private static EntityMetadata EntityMetadata <T>
     (IOrganizationService service)
     where T : Entity
 {
     try
     {
         return(MetadataHelper <T>(service, EntityFilters.Entity));
     }
     catch (Exception ex)
     {
         throw new Exception(HU.ErrorMessageWrapper(ex));
     };
 }
Example #7
0
 public static int GetEntitiesCountByDictionary <T>
     (IOrganizationService service, Dictionary <string, object> dictionary)
     where T : Entity
 {
     try
     {
         return(GetEntitiesLightByDictionary <T>(service, dictionary).Count());
     }
     catch (Exception ex)
     {
         throw new Exception(HU.ErrorMessageWrapper(ex));
     };
 }
Example #8
0
 public static int GetEntitiesCountByLookup <T>
     (IOrganizationService service, Guid id, string attributeName)
     where T : Entity
 {
     try
     {
         return(GetEntitiesLightByLookup <T>(service, id, attributeName).Count());
     }
     catch (Exception ex)
     {
         throw new Exception(HU.ErrorMessageWrapper(ex));
     };
 }
Example #9
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="service"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static T GetEntity <T>(IOrganizationService service, Guid id)
            where T : Entity
        {
            try
            {
                var logicalName = (Activator.CreateInstance <T>()).LogicalName;

                return(((Entity)service.Retrieve(logicalName, id, new ColumnSet(true))).ToEntity <T>());
            }
            catch (Exception ex)
            {
                throw new Exception(HU.ErrorMessageWrapper(ex));
            };
        }
Example #10
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="U"></typeparam>
        /// <typeparam name="V"></typeparam>
        /// <param name="service"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static IEnumerable <T> GetEntitiesIntersectTable <T, U, V>
            (IOrganizationService service, Guid id)
            where T : Entity
            where U : Entity
            where V : Entity
        {
            try
            {
                var metadataV         = EntityMetadata <V>(service);
                var primaryAttributeV = metadataV.PrimaryIdAttribute;

                return(GetEntitiesIntersectTable <T, U>(service, id, primaryAttributeV));
            }
            catch (Exception ex)
            {
                throw new Exception(HU.ErrorMessageWrapper(ex));
            };
        }
Example #11
0
        private static IEnumerable <T> GetEntitiesIntersectTableHelper <T, U>
            (IOrganizationService service, FilterExpression filter)
            where T : Entity
            where U : Entity
        {
            try
            {
                var logicalNameT = (Activator.CreateInstance <T>()).LogicalName;
                var logicalNameU = (Activator.CreateInstance <U>()).LogicalName;

                var metadataT         = EntityMetadata <T>(service);
                var primaryAttributeT = metadataT.PrimaryIdAttribute;

                QueryExpression query = new QueryExpression()
                {
                    EntityName = logicalNameT,
                    ColumnSet  = new ColumnSet(true),
                    PageInfo   = new PagingInfo()
                    {
                        PageNumber = 1
                    },
                    LinkEntities =
                    {
                        new LinkEntity
                        {
                            LinkFromEntityName    = logicalNameT,
                            LinkFromAttributeName = primaryAttributeT,
                            LinkToEntityName      = logicalNameU,
                            LinkToAttributeName   = primaryAttributeT,
                            LinkCriteria          = filter
                        }
                    }
                };

                return(GetEntitiesHelper <T>(service, query));
            }
            catch (Exception ex)
            {
                throw new Exception(HU.ErrorMessageWrapper(ex));
            };
        }
Example #12
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="service"></param>
        /// <param name="target"></param>
        /// <param name="relatedEntity"></param>
        /// <param name="relation"></param>
        public static void DisassociateEntities
            (IOrganizationService service, Entity target, Entity relatedEntity, string relation)
        {
            try
            {
                var request = new DisassociateRequest
                {
                    Target          = new EntityReference(target.LogicalName, target.Id),
                    RelatedEntities =
                        new EntityReferenceCollection
                    {
                        new EntityReference(relatedEntity.LogicalName, relatedEntity.Id)
                    },
                    Relationship = new Relationship(relation)
                };

                service.Execute(request);
            }
            catch (Exception ex)
            {
                throw new Exception(HU.ErrorMessageWrapper(ex));
            };
        }
Example #13
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="service"></param>
        /// <param name="dictionary"></param>
        /// <returns></returns>
        public static IEnumerable <T> GetEntitiesLightByDictionary <T>
            (IOrganizationService service, Dictionary <string, object> dictionary)
            where T : Entity
        {
            try
            {
                var filter = new FilterExpression
                {
                    FilterOperator = LogicalOperator.And
                };

                foreach (KeyValuePair <string, object> c in dictionary)
                {
                    filter.AddCondition(c.Key, ConditionOperator.Equal, c.Value);
                }

                return(GetEntitiesLightHelper <T>(service, filter));
            }
            catch (Exception ex)
            {
                throw new Exception(HU.ErrorMessageWrapper(ex));
            };
        }