protected override void Execute(CodeActivityContext executionContext) { Boolean EnableLoggingValue = EnableLogging.Get(executionContext); string ProductUriValue = ProductUri.Get(executionContext); string AppSIDValue = AppSID.Get(executionContext); string AppKeyValue = AppKey.Get(executionContext); string LogFilePath = LogFile.Get(executionContext); EntityReference Attachment = AttachmentId.Get(executionContext); CloudAppConfig config = new CloudAppConfig(); config.ProductUri = ProductUriValue; config.AppSID = AppSIDValue; config.AppKey = AppKeyValue; IWorkflowContext context = executionContext.GetExtension <IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); try { if (EnableLoggingValue) { Log("WorkFlow Started", LogFilePath); } if (EnableLoggingValue) { Log("Retrieving Attachment From CRM", LogFilePath); } Entity ThisAttachment = service.Retrieve("annotation", Attachment.Id, new ColumnSet(new string[] { "filename", "documentbody", "mimetype" })); if (ThisAttachment != null) { if (EnableLoggingValue) { Log("Attachment Retrieved Successfully", LogFilePath); } if (ThisAttachment.Contains("mimetype") && ThisAttachment.Contains("documentbody")) { string FileName = "Aspose .NET AutoMerge Attachment (" + DateTime.Now.ToString() + ").docx"; if (ThisAttachment.Contains("filename")) { FileName = ThisAttachment["filename"].ToString(); } config.FileName = FileName; byte[] DocumentBody = Convert.FromBase64String(ThisAttachment["documentbody"].ToString()); MemoryStream fileStream = new MemoryStream(DocumentBody); if (EnableLoggingValue) { Log("Upload Attachment on Storage", LogFilePath); } UploadFileOnStorage(config, fileStream); } } } catch (Exception ex) { Log(ex.Message, LogFilePath); throw ex; } }
protected override void Execute(CodeActivityContext executionContext) { EntityReference DocumentTemplateIdValue = DocumentTemplateId.Get(executionContext); Boolean EnableLoggingValue = EnableLogging.Get(executionContext); string ProductUriValue = ProductUri.Get(executionContext); string AppSIDValue = AppSID.Get(executionContext); string AppKeyValue = AppKey.Get(executionContext); Boolean DeleteTemplateValue = DeleteTemplate.Get(executionContext); Boolean DeleteDocumentValue = DeleteDocument.Get(executionContext); OutputAttachmentId.Set(executionContext, new EntityReference("annotation", Guid.Empty)); CloudAppConfig config = new CloudAppConfig(); config.ProductUri = ProductUriValue; config.AppSID = AppSIDValue; config.AppKey = AppKeyValue; IWorkflowContext context = executionContext.GetExtension <IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); try { if (EnableLoggingValue) { Log("WorkFlow Started", service); } string PrimaryEntityName = context.PrimaryEntityName; Guid PrimaryEntityId = context.PrimaryEntityId; if (EnableLoggingValue) { Log("Retrieving Attachment From Template", service); } QueryExpression RetrieveNoteQuery = new QueryExpression("annotation"); RetrieveNoteQuery.ColumnSet = new ColumnSet(new string[] { "filename", "documentbody", "mimetype" }); RetrieveNoteQuery.Criteria.AddCondition(new ConditionExpression("objectid", ConditionOperator.Equal, DocumentTemplateIdValue.Id)); EntityCollection TemplateAttachments = service.RetrieveMultiple(RetrieveNoteQuery); if (EnableLoggingValue) { Log("Attachment Retrieved Successfully", service); } if (TemplateAttachments != null && TemplateAttachments.Entities.Count > 0) { Entity AttachmentTemplate = TemplateAttachments.Entities[0]; if (AttachmentTemplate.Contains("mimetype") && AttachmentTemplate.Contains("documentbody")) { string FileName = ""; if (AttachmentTemplate.Contains("filename")) { FileName = AttachmentTemplate["filename"].ToString(); } config.FileName = FileName; byte[] DocumentBody = Convert.FromBase64String(AttachmentTemplate["documentbody"].ToString()); MemoryStream fileStream = new MemoryStream(DocumentBody); if (EnableLoggingValue) { Log("Upload Template on Storage", service); } UploadFileOnStorage(config, fileStream); if (EnableLoggingValue) { Log("Get Fields List", service); } string[] Fields = GetFieldsName(config).ToArray(); if (EnableLoggingValue) { Log("Retrieving Fields Values From CRM", service); } Entity PrimaryEntity = service.Retrieve(PrimaryEntityName, PrimaryEntityId, new ColumnSet(Fields)); string[] Values = new string[Fields.Length]; if (PrimaryEntity != null) { for (int i = 0; i < Fields.Length; i++) { if (PrimaryEntity.Contains(Fields[i])) { if (PrimaryEntity[Fields[i]].GetType() == typeof(OptionSetValue)) { Values[i] = PrimaryEntity.FormattedValues[Fields[i]].ToString(); } else if (PrimaryEntity[Fields[i]].GetType() == typeof(EntityReference)) { Values[i] = ((EntityReference)PrimaryEntity[Fields[i]]).Name; } else { Values[i] = PrimaryEntity[Fields[i]].ToString(); } } else { Values[i] = ""; } } } if (EnableLoggingValue) { Log("Generating Xml", service); } string Xml = GenerateXML(Fields, Values); if (EnableLoggingValue) { Log("Executing MailMerge", service); } string OutputFileName = ExecuteMailMerge(config, Xml); if (EnableLoggingValue) { Log("Downloading File From Cloud", service); } MemoryStream OutputFile = DownloadFile(config, OutputFileName); if (EnableLoggingValue) { Log("Generating CRM Attachment", service); } byte[] byteData = OutputFile.ToArray(); string encodedData = System.Convert.ToBase64String(byteData); Entity NewNote = new Entity("annotation"); NewNote.Attributes.Add("objectid", new EntityReference(PrimaryEntityName, PrimaryEntityId)); NewNote.Attributes.Add("subject", FileName); NewNote.Attributes.Add("documentbody", encodedData); NewNote.Attributes.Add("mimetype", @"application/vnd.openxmlformats-officedocument.wordprocessingml.document"); NewNote.Attributes.Add("notetext", "Document Created using Aspose Cloud"); NewNote.Attributes.Add("filename", FileName); Guid NewNoteId = service.Create(NewNote); if (EnableLoggingValue) { Log("Removing Documents from Storage", service); } if (DeleteTemplateValue) { DeleteDocumentFromStorage(config, FileName); } if (DeleteDocumentValue) { DeleteDocumentFromStorage(config, OutputFileName); } OutputAttachmentId.Set(executionContext, new EntityReference("annotation", NewNoteId)); } else { if (EnableLoggingValue) { Log("Attachment Doesnot contain any document", service); } } } else { if (EnableLoggingValue) { Log("No Attachments in the Template Provided", service); } } if (EnableLoggingValue) { Log("Workflow Executed Successfully", service); } } catch (Exception ex) { Log(ex.Message, service); throw ex; } }