protected override void ExecuteCrmWorkFlowActivity(CodeActivityContext context, LocalWorkflowContext localContext)
        {
            if (context == null)
                throw new ArgumentNullException(nameof(context));
            if (localContext == null)
                throw new ArgumentNullException(nameof(localContext));

            EntityReference emailWithAttachments = EmailWithAttachments.Get(context);
            int deleteSizeMax = DeleteSizeMax.Get(context);
            int deleteSizeMin = DeleteSizeMin.Get(context);
            string extensions = Extensions.Get(context);
            bool appendNotice = AppendNotice.Get(context);

            if (deleteSizeMax == 0) deleteSizeMax = int.MaxValue;
            if (deleteSizeMin > deleteSizeMax)
            {
                localContext.TracingService.Trace("Exception: {0}", "Min:" + deleteSizeMin + " Max:" + deleteSizeMax);
                throw new InvalidPluginExecutionException("Minimum Size Cannot Be Greater Than Maximum Size");
            }

            EntityCollection attachments = GetAttachments(localContext.OrganizationService, emailWithAttachments.Id);
            if (attachments.Entities.Count == 0) return;

            string[] filetypes = new string[0];
            if (!string.IsNullOrEmpty(extensions))
                filetypes = extensions.Replace(".", string.Empty).Split(',');

            StringBuilder notice = new StringBuilder();
            int numberOfAttachmentsDeleted = 0;

            bool delete = false;
            foreach (Entity activityMineAttachment in attachments.Entities)
            {
                delete = false;

                if (activityMineAttachment.GetAttributeValue<int>("filesize") >= deleteSizeMax)
                    delete = true;

                if (activityMineAttachment.GetAttributeValue<int>("filesize") <= deleteSizeMin)
                    delete = true;

                if (filetypes.Length > 0 && delete)
                    delete = ExtensionMatch(filetypes, activityMineAttachment.GetAttributeValue<string>("filename"));

                if (!delete) continue;

                DeleteEmailAttachment(localContext.OrganizationService, activityMineAttachment.Id);
                numberOfAttachmentsDeleted++;

                if (appendNotice)
                    notice.AppendLine("Deleted Attachment: " + activityMineAttachment.GetAttributeValue<string>("filename") + " " +
                                      DateTime.Now.ToShortDateString() + "\r\n");
            }

            if (delete && appendNotice && notice.Length > 0)
                UpdateEmail(localContext.OrganizationService, emailWithAttachments.Id, notice.ToString());

            NumberOfAttachmentsDeleted.Set(context, numberOfAttachmentsDeleted);
        }
        protected override void Execute(CodeActivityContext executionContext)
        {
            ITracingService             tracer         = executionContext.GetExtension <ITracingService>();
            IWorkflowContext            context        = executionContext.GetExtension <IWorkflowContext>();
            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>();
            IOrganizationService        service        = serviceFactory.CreateOrganizationService(context.UserId);

            try
            {
                EntityReference emailWithAttachments = EmailWithAttachments.Get(executionContext);
                string          fileName             = FileName.Get(executionContext);
                bool            appendNotice         = AppendNotice.Get(executionContext);

                EntityCollection attachments = GetAttachments(service, emailWithAttachments.Id);
                if (attachments.Entities.Count == 0)
                {
                    return;
                }

                StringBuilder notice = new StringBuilder();
                int           numberOfAttachmentsDeleted = 0;

                foreach (Entity activityMineAttachment in attachments.Entities)
                {
                    if (!String.Equals(activityMineAttachment.GetAttributeValue <string>("filename"), fileName, StringComparison.CurrentCultureIgnoreCase))
                    {
                        continue;
                    }

                    DeleteEmailAttachment(service, activityMineAttachment.Id);
                    numberOfAttachmentsDeleted++;

                    if (appendNotice)
                    {
                        notice.AppendLine("Deleted Attachment: " + activityMineAttachment.GetAttributeValue <string>("filename") + " " +
                                          DateTime.Now.ToShortDateString() + "\r\n");
                    }
                }

                if (appendNotice && notice.Length > 0)
                {
                    UpdateEmail(service, emailWithAttachments.Id, notice.ToString());
                }

                NumberOfAttachmentsDeleted.Set(executionContext, numberOfAttachmentsDeleted);
            }
            catch (Exception ex)
            {
                tracer.Trace("Exception: {0}", ex.ToString());
            }
        }
Exemplo n.º 3
0
        protected override void ExecuteCrmWorkFlowActivity(CodeActivityContext context, LocalWorkflowContext localContext)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (localContext == null)
            {
                throw new ArgumentNullException(nameof(localContext));
            }

            EntityReference emailWithAttachments = EmailWithAttachments.Get(context);
            string          fileName             = FileName.Get(context);
            bool            appendNotice         = AppendNotice.Get(context);

            EntityCollection attachments = GetAttachments(localContext.OrganizationService, emailWithAttachments.Id);

            if (attachments.Entities.Count == 0)
            {
                return;
            }

            StringBuilder notice = new StringBuilder();
            int           numberOfAttachmentsDeleted = 0;

            foreach (Entity activityMineAttachment in attachments.Entities)
            {
                if (!string.Equals(activityMineAttachment.GetAttributeValue <string>("filename"), fileName, StringComparison.CurrentCultureIgnoreCase))
                {
                    continue;
                }

                DeleteEmailAttachment(localContext.OrganizationService, activityMineAttachment.Id);
                numberOfAttachmentsDeleted++;

                if (appendNotice)
                {
                    notice.AppendLine("Deleted Attachment: " + activityMineAttachment.GetAttributeValue <string>("filename") + " " +
                                      DateTime.Now.ToShortDateString() + "\r\n");
                }
            }

            if (appendNotice && notice.Length > 0)
            {
                UpdateEmail(localContext.OrganizationService, emailWithAttachments.Id, notice.ToString());
            }

            NumberOfAttachmentsDeleted.Set(context, numberOfAttachmentsDeleted);
        }
Exemplo n.º 4
0
        protected override void Execute(CodeActivityContext executionContext)
        {
            ITracingService             tracer         = executionContext.GetExtension <ITracingService>();
            IWorkflowContext            context        = executionContext.GetExtension <IWorkflowContext>();
            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>();
            IOrganizationService        service        = serviceFactory.CreateOrganizationService(context.UserId);

            try
            {
                EntityReference emailWithAttachments = EmailWithAttachments.Get(executionContext);
                int             deleteSizeMax        = DeleteSizeMax.Get(executionContext);
                int             deleteSizeMin        = DeleteSizeMin.Get(executionContext);
                string          extensions           = Extensions.Get(executionContext);
                bool            appendNotice         = AppendNotice.Get(executionContext);

                if (deleteSizeMax == 0)
                {
                    deleteSizeMax = int.MaxValue;
                }
                if (deleteSizeMin > deleteSizeMax)
                {
                    tracer.Trace("Exception: {0}", "Min:" + deleteSizeMin + " Max:" + deleteSizeMax);
                    throw new InvalidPluginExecutionException("Minimum Size Cannot Be Greater Than Maximum Size");
                }

                EntityCollection attachments = GetAttachments(service, emailWithAttachments.Id);
                if (attachments.Entities.Count == 0)
                {
                    return;
                }

                string[] filetypes = new string[0];
                if (!string.IsNullOrEmpty(extensions))
                {
                    filetypes = extensions.Replace(".", string.Empty).Split(',');
                }

                StringBuilder notice = new StringBuilder();
                int           numberOfAttachmentsDeleted = 0;

                bool delete = false;
                foreach (Entity activityMineAttachment in attachments.Entities)
                {
                    delete = false;

                    if (activityMineAttachment.GetAttributeValue <int>("filesize") >= deleteSizeMax)
                    {
                        delete = true;
                    }

                    if (activityMineAttachment.GetAttributeValue <int>("filesize") <= deleteSizeMin)
                    {
                        delete = true;
                    }

                    if (filetypes.Length > 0 && delete)
                    {
                        delete = ExtensionMatch(filetypes, activityMineAttachment.GetAttributeValue <string>("filename"));
                    }

                    if (!delete)
                    {
                        continue;
                    }

                    DeleteEmailAttachment(service, activityMineAttachment.Id);
                    numberOfAttachmentsDeleted++;

                    if (appendNotice)
                    {
                        notice.AppendLine("Deleted Attachment: " + activityMineAttachment.GetAttributeValue <string>("filename") + " " +
                                          DateTime.Now.ToShortDateString() + "\r\n");
                    }
                }

                if (delete && appendNotice && notice.Length > 0)
                {
                    UpdateEmail(service, emailWithAttachments.Id, notice.ToString());
                }

                NumberOfAttachmentsDeleted.Set(executionContext, numberOfAttachmentsDeleted);
            }
            catch (Exception ex)
            {
                tracer.Trace("Exception: {0}", ex.ToString());
            }
        }