Exemplo n.º 1
0
        protected override int ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues)
        {
            ADXTrace.Instance.TraceInfo(TraceCategory.Application, "Begin");

            if (!CanUpdate)
            {
                throw new NotSupportedException("Update not supported.");
            }

            var id         = keys["ID"] as Guid?;
            var entityName = keys["Name"] as string;

            if (id == null || entityName == null)
            {
                throw new ArgumentException("Update requires the 'ID' and 'Name' to be specified as DataKeyNames.", "keys");
            }

            var rowsAffected     = 0;
            var updatedEventArgs = new CrmDataSourceViewUpdatedEventArgs();

            try
            {
                var entity = new Entity(entityName)
                {
                    Id = id.Value
                };

                SetEntityAttributes(entity, values);

                _crmDataContext.Attach(entity);

                _crmDataContext.UpdateObject(entity);

                var result = _crmDataContext.SaveChanges();

                if (result.HasError)
                {
                    rowsAffected = 0;
                }
                else
                {
                    updatedEventArgs.Entity = entity;
                    rowsAffected            = 1;
                }
            }
            catch (Exception e)
            {
                ADXTrace.Instance.TraceError(TraceCategory.Application, e.ToString());

                updatedEventArgs.Exception        = e;
                updatedEventArgs.ExceptionHandled = true;
            }

            ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("End: rowsAffected={0}", rowsAffected));

            OnUpdated(updatedEventArgs);

            return(rowsAffected);
        }
Exemplo n.º 2
0
        private void AddCartToSupportRequest(ShoppingCart myCart)
        {
            var context = new CrmOrganizationServiceContext(new CrmConnection("Xrm"));

            var supportRequest = new Entity("adx_supportrequest")
            {
                Id = CurrentStepEntityID
            };

            supportRequest.Attributes["adx_shoppingcartid"] = myCart.Entity.ToEntityReference();

            context.Attach(supportRequest);

            context.UpdateObject(supportRequest);

            context.SaveChanges();
        }
Exemplo n.º 3
0
        public static bool BulkSave(EntityCollection entities, CrmConnection connection = null)
        {
            OrganizationService srv = new OrganizationService(connection ?? XrmConnection.Connection);

            using (CrmOrganizationServiceContext service = new CrmOrganizationServiceContext(srv))
            {
                foreach (Entity e in entities.Entities)
                {
                    if (e.Id.Equals(Guid.Empty))
                    {
                        e.Id = Guid.NewGuid();
                        // service.Attach(e);
                        service.AddObject(e);
                    }
                    else
                    {
                        service.Attach(e);
                        service.UpdateObject(e);
                    }
                }
                SaveChangesResultCollection result = service.SaveChanges(Microsoft.Xrm.Sdk.Client.SaveChangesOptions.None);
                return(!result.HasError);
            }
        }
Exemplo n.º 4
0
        public static void HashAllPasswords(Func <string, string> PasswordHasher)
        {
            OrganizationService srv = new OrganizationService(XrmConnection.Connection);

            using (CrmOrganizationServiceContext service = new CrmOrganizationServiceContext(srv))
            {
                IQueryable <Entity> query = from member in service.CreateQuery("appl_webuser")
                                            select member;


                foreach (Entity e in query.ToList())
                {
                    string password = e.GetAttributeValue <string>("appl_passwordhash");
                    if (!string.IsNullOrEmpty(password) && password.Length < 15)
                    {
                        string hash = PasswordHasher(password);
                        e["appl_passwordhash"] = hash;
                        // e.EntityState = EntityState.Changed;
                        service.UpdateObject(e);
                    }
                }
                service.SaveChanges();
            }
        }
        protected override int ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues)
        {
            Tracing.FrameworkInformation("CrmDataSourceView", "ExecuteUpdate", "Begin");

            if (!CanUpdate)
            {
                throw new NotSupportedException("Update not supported.");
            }

            var id         = keys["ID"] as Guid?;
            var entityName = keys["Name"] as string;

            if (id == null || entityName == null)
            {
                throw new ArgumentException("Update requires the 'ID' and 'Name' to be specified as DataKeyNames.", "keys");
            }

            var rowsAffected     = 0;
            var updatedEventArgs = new CrmDataSourceViewUpdatedEventArgs();

            try
            {
                var entity = _crmDataContext.Retrieve(entityName, id.Value, new ColumnSet(true));

                if (entity == null)
                {
                    throw new NullReferenceException("The {0} entity with ID='{1}' could not be found.".FormatWith(id, entityName));
                }

                SetEntityAttributes(entity, values);

                _crmDataContext.Attach(entity);

                _crmDataContext.UpdateObject(entity);

                var result = _crmDataContext.SaveChanges();

                if (result.HasError)
                {
                    rowsAffected = 0;
                }
                else
                {
                    updatedEventArgs.Entity = entity;
                    rowsAffected            = 1;
                }
            }
            catch (Exception e)
            {
                Tracing.FrameworkError("CrmDataSourceView", "ExecuteUpdate", "{0}", e);

                updatedEventArgs.Exception        = e;
                updatedEventArgs.ExceptionHandled = true;
            }

            Tracing.FrameworkInformation("CrmDataSourceView", "ExecuteUpdate", "End: rowsAffected={0}", rowsAffected);

            OnUpdated(updatedEventArgs);

            return(rowsAffected);
        }