Esempio n. 1
0
        private static List <Tuple <string, string> > RemoveFiles(ICommandContext commandContext)
        {
            var errors = new List <Tuple <string, string> >();

            var targetHashes = (commandContext.Parameters["targets[]"] ?? string.Empty).Split(',');

            if (!targetHashes.Any())
            {
                return(errors);
            }

            var portal              = commandContext.CreatePortalContext();
            var website             = portal.Website.ToEntityReference();
            var security            = commandContext.CreateSecurityProvider();
            var dataServiceProvider = commandContext.CreateDependencyProvider().GetDependency <ICmsDataServiceProvider>();

            foreach (var targetHash in targetHashes)
            {
                var serviceContext = commandContext.CreateServiceContext();

                Entity target;

                if (!TryGetTargetEntity(serviceContext, targetHash, website, out target))
                {
                    errors.Add(new Tuple <string, string>(targetHash, ResourceManager.GetString("Unable_To_Retrieve_Target_Entity_For_Given_Hash_Error")));

                    continue;
                }

                try
                {
                    OrganizationServiceContextInfo serviceContextInfo;
                    EntitySetInfo entitySetInfo;

                    if (dataServiceProvider != null &&
                        OrganizationServiceContextInfo.TryGet(serviceContext.GetType(), out serviceContextInfo) &&
                        serviceContextInfo.EntitySetsByEntityLogicalName.TryGetValue(target.LogicalName, out entitySetInfo))
                    {
                        dataServiceProvider.DeleteEntity(serviceContext, entitySetInfo.Property.Name, target.Id);
                    }
                    else
                    {
                        if (!security.TryAssert(serviceContext, target, CrmEntityRight.Change))
                        {
                            errors.Add(new Tuple <string, string>(GetDisplayName(target), ResourceManager.GetString("Delete_Permission_Denied_For_Target_Entity_Error")));

                            continue;
                        }

                        CrmEntityInactiveInfo inactiveInfo;

                        if (CrmEntityInactiveInfo.TryGetInfo(target.LogicalName, out inactiveInfo))
                        {
                            serviceContext.SetState(inactiveInfo.InactiveState, inactiveInfo.InactiveStatus, target);
                        }
                        else
                        {
                            serviceContext.DeleteObject(target);
                            serviceContext.SaveChanges();
                        }
                    }
                }
                catch (Exception e)
                {
                    ADXTrace.Instance.TraceError(TraceCategory.Application, string.Format("{0} {1}", ResourceManager.GetString("Deleting_File_Exception"), e.ToString()));
                    errors.Add(new Tuple <string, string>(GetDisplayName(target), e.Message));
                }
            }

            return(errors);
        }