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);
        }
Esempio n. 2
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 noteWithAttachment = NoteWithAttachment.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");
                }

                Entity note = GetNote(service, noteWithAttachment.Id);
                if (!CheckForAttachment(note))
                {
                    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;

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

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

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

                if (delete)
                {
                    numberOfAttachmentsDeleted++;

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

                    UpdateNote(service, note, notice.ToString());
                }

                NumberOfAttachmentsDeleted.Set(executionContext, numberOfAttachmentsDeleted);
            }
            catch (Exception ex)
            {
                tracer.Trace("Exception: {0}", ex.ToString());
            }
        }
        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 noteWithAttachment = NoteWithAttachment.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");
            }

            Entity note = GetNote(localContext.OrganizationService, noteWithAttachment.Id);

            if (!CheckForAttachment(note))
            {
                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 = note.GetAttributeValue <int>("filesize") >= deleteSizeMax;

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

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

            if (delete)
            {
                numberOfAttachmentsDeleted++;

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

                UpdateNote(localContext.OrganizationService, note, notice.ToString());
            }

            NumberOfAttachmentsDeleted.Set(context, numberOfAttachmentsDeleted);
        }