protected override void Execute(CodeActivityContext executionContext) { EntityReference Template = DocumentTemplateId.Get(executionContext); Boolean Logging = EnableLogging.Get(executionContext); string LicenseFilePath = LicenseFile.Get(executionContext); string LogFilePath = LogFile.Get(executionContext); bool savePrimary = SavePrimary.Get(executionContext); string saveAs = SaveAs.Get(executionContext); OutputAttachmentId.Set(executionContext, new EntityReference("annotation", Guid.Empty)); try { if (Logging) { Log("Workflow Executed", LogFilePath); } IWorkflowContext context = executionContext.GetExtension <IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); string PrimaryEntityName = context.PrimaryEntityName; Guid PrimaryEntityId = context.PrimaryEntityId; try { if (Logging) { Log("Enable Licensing", LogFilePath); } if (LicenseFilePath != "" && File.Exists(LicenseFilePath)) { License Lic = new License(); Lic.SetLicense(LicenseFilePath); if (Logging) { Log("License Set", LogFilePath); } } } catch (Exception ex) { Log("Error while applying license: " + ex.Message, LogFilePath); } QueryExpression RetrieveNoteQuery = new QueryExpression("annotation"); RetrieveNoteQuery.ColumnSet = new ColumnSet(new string[] { "subject", "documentbody" }); RetrieveNoteQuery.Criteria.AddCondition(new ConditionExpression("objectid", ConditionOperator.Equal, Template.Id)); if (Logging) { Log("Executing Query to retrieve Template Attachment", LogFilePath); } EntityCollection Notes = service.RetrieveMultiple(RetrieveNoteQuery); if (Logging) { Log("Attachment Retrieved Successfully", LogFilePath); } if (Notes.Entities.Count > 0) { Entity Note = Notes[0]; string FileName = ""; if (Note.Contains("filename")) { FileName = Note["filename"].ToString(); } if (Note.Contains("documentbody")) { if (Logging) { Log("Attachment Read Successfully", LogFilePath); } byte[] DocumentBody = Convert.FromBase64String(Note["documentbody"].ToString()); MemoryStream fileStream = new MemoryStream(DocumentBody); if (Logging) { Log("Reading Document in Aspose.Words", LogFilePath); } Document doc = new Document(fileStream); if (Logging) { Log("Getting Fields list", LogFilePath); } string[] fields = doc.MailMerge.GetFieldNames(); if (Logging) { Log("Getting list of fields for entity", LogFilePath); } Entity PrimaryEntity = service.Retrieve(PrimaryEntityName, PrimaryEntityId, new ColumnSet(fields)); if (Logging) { Log("Retrieved Contact entity", LogFilePath); } if (PrimaryEntity != null) { string[] values = new string[fields.Length]; 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 (Logging) { Log("Executing Mail Merge", LogFilePath); } doc.MailMerge.Execute(fields, values); MemoryStream UpdateDoc = new MemoryStream(); if (Logging) { Log("Saving Document", LogFilePath); } switch (saveAs.ToLower()) { case "bmp": doc.Save(UpdateDoc, SaveFormat.Bmp); break; case "doc": doc.Save(UpdateDoc, SaveFormat.Doc); break; case "html": doc.Save(UpdateDoc, SaveFormat.Html); break; case "jpeg": doc.Save(UpdateDoc, SaveFormat.Jpeg); break; case "pdf": doc.Save(UpdateDoc, SaveFormat.Pdf); break; case "png": doc.Save(UpdateDoc, SaveFormat.Png); break; case "rtf": doc.Save(UpdateDoc, SaveFormat.Rtf); break; case "text": case "txt": doc.Save(UpdateDoc, SaveFormat.Text); break; default: doc.Save(UpdateDoc, SaveFormat.Docx); break; } byte[] byteData = UpdateDoc.ToArray(); // Encode the data using base64. string encodedData = System.Convert.ToBase64String(byteData); if (Logging) { Log("Creating Attachment", LogFilePath); } Entity NewNote = new Entity("annotation"); // Im going to add Note to entity if (savePrimary) { NewNote.Attributes.Add("objectid", new EntityReference(PrimaryEntityName, PrimaryEntityId)); } NewNote.Attributes.Add("subject", FileName != "" ? FileName : "Aspose .NET AutoMerge Created Document." + saveAs); // Set EncodedData to Document Body NewNote.Attributes.Add("documentbody", encodedData); // Set the type of attachment NewNote.Attributes.Add("mimetype", @"application/vnd.openxmlformats-officedocument.wordprocessingml.document"); NewNote.Attributes.Add("notetext", "Document Created using template"); // Set the File Name NewNote.Attributes.Add("filename", FileName != "" ? FileName : "Aspose .NET AutoMerge Created Document." + saveAs); Guid NewNoteId = service.Create(NewNote); OutputAttachmentId.Set(executionContext, new EntityReference("annotation", NewNoteId)); if (Logging) { Log("Successfull", LogFilePath); } } } } if (Logging) { Log("Workflow Executed Successfully", LogFilePath); } } catch (Exception ex) { Log(ex.Message, LogFilePath); if (ex.InnerException != null) { Log(ex.InnerException.Message, LogFilePath); } } }
protected override void Execute(CodeActivityContext executionContext) { bool Logging = EnableLogging.Get(executionContext); string LicenseFilePath = LicenseFile.Get(executionContext); string LogFilePath = LogFile.Get(executionContext); int detectIn = DetectIn.Get(executionContext); OutputAttachmentId.Set(executionContext, new EntityReference("annotation", Guid.Empty)); if (Logging) { Log("Execution Started", LogFilePath); } // Create a CRM Service in Workflow. IWorkflowContext context = executionContext.GetExtension <IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); try { if (Logging) { Log("Enable Licensing", LogFilePath); } if (LicenseFilePath != "" && File.Exists(LicenseFilePath)) { License Lic = new License(); Lic.SetLicense(LicenseFilePath); if (Logging) { Log("License Set", LogFilePath); } } } catch (Exception ex) { Log("Error while applying license: " + ex.Message, LogFilePath); } if (detectIn == 0) // Under this record. { Guid ThisRecordId = context.PrimaryEntityId; string RecordType = context.PrimaryEntityName; Document Result = new Document(); DocumentBuilder ResultWriter = new DocumentBuilder(Result); if (Logging) { Log("Working under all attachments under this record", LogFilePath); } QueryExpression RetrieveNoteQuery = new QueryExpression("annotation"); RetrieveNoteQuery.ColumnSet = new ColumnSet(new string[] { "filename", "subject", "documentbody" }); RetrieveNoteQuery.Criteria.AddCondition(new ConditionExpression("objectid", ConditionOperator.Equal, ThisRecordId)); if (Logging) { Log("Executing Query to retrieve All Notes within this record", LogFilePath); } EntityCollection Notes = service.RetrieveMultiple(RetrieveNoteQuery); foreach (Entity Note in Notes.Entities) { try { if (Note.Contains("documentbody")) { string FileName = ""; if (Note.Contains("filename")) { FileName = Note["filename"].ToString(); } byte[] DocumentBody = Convert.FromBase64String(Note["documentbody"].ToString()); MemoryStream fileStream = new MemoryStream(DocumentBody); Document doc = new Document(fileStream); ResultWriter.Writeln("Comparing Document: " + FileName); ResultWriter.StartTable(); foreach (Entity OtherNote in Notes.Entities) { if (OtherNote.Id != Note.Id) { if (OtherNote.Contains("documentbody")) { string OtherFileName = ""; if (OtherNote.Contains("filename")) { OtherFileName = OtherNote["filename"].ToString(); } byte[] OtherDocumentBody = Convert.FromBase64String(OtherNote["documentbody"].ToString()); MemoryStream fileStream2 = new MemoryStream(OtherDocumentBody); Document doc2 = new Document(fileStream); ResultWriter.InsertCell(); ResultWriter.Write(OtherFileName); doc.Compare(doc2, "a", DateTime.Now); if (doc.Revisions.Count == 0) { ResultWriter.InsertCell(); ResultWriter.Write("Duplicate Documents"); } ResultWriter.EndRow(); } } } ResultWriter.EndTable(); } } catch (Exception ex) { Log("Error while applying license: " + ex.Message, LogFilePath); } } MemoryStream UpdateDoc = new MemoryStream(); if (Logging) { Log("Saving Document", LogFilePath); } Result.Save(UpdateDoc, SaveFormat.Docx); byte[] byteData = UpdateDoc.ToArray(); // Encode the data using base64. string encodedData = System.Convert.ToBase64String(byteData); if (Logging) { Log("Creating Attachment for result", LogFilePath); } // Add a Node to the entity. Entity NewNote = new Entity("annotation"); NewNote.Attributes.Add("objectid", new EntityReference(RecordType, ThisRecordId)); NewNote.Attributes.Add("subject", "Duplicate detection report"); // Set EncodedData to Document Body. NewNote.Attributes.Add("documentbody", encodedData); // Set the type of attachment. NewNote.Attributes.Add("mimetype", @"application\ms-word"); NewNote.Attributes.Add("notetext", "Duplicate detection report"); // Set the filename. NewNote.Attributes.Add("filename", "Duplicate detection report"); Guid NewNoteId = service.Create(NewNote); OutputAttachmentId.Set(executionContext, new EntityReference("annotation", NewNoteId)); if (Logging) { Log("Attachment Created Successfully", LogFilePath); } } else if (detectIn == 1) // Under this entity. { Guid ThisRecordId = context.PrimaryEntityId; string RecordType = context.PrimaryEntityName; Document Result = new Document(); DocumentBuilder ResultWriter = new DocumentBuilder(Result); if (Logging) { Log("Working under all attachments under this Entity", LogFilePath); } QueryExpression RetrieveNoteQuery = new QueryExpression("annotation"); RetrieveNoteQuery.ColumnSet = new ColumnSet(new string[] { "filename", "subject", "documentbody", "objectid" }); if (Logging) { Log("Executing Query to retrieve All Notes within this Entity", LogFilePath); } EntityCollection Notes = service.RetrieveMultiple(RetrieveNoteQuery); foreach (Entity Note in Notes.Entities) { if (Note.Contains("objectid") && ((EntityReference)Note["objectid"]).LogicalName == RecordType) { try { if (Note.Contains("documentbody")) { string FileName = ""; if (Note.Contains("filename")) { FileName = Note["filename"].ToString(); } byte[] DocumentBody = Convert.FromBase64String(Note["documentbody"].ToString()); MemoryStream fileStream = new MemoryStream(DocumentBody); Document doc = new Document(fileStream); ResultWriter.Writeln("Comparing Document: " + FileName); ResultWriter.StartTable(); foreach (Entity OtherNote in Notes.Entities) { if (OtherNote.Id != Note.Id) { if (OtherNote.Contains("documentbody")) { string OtherFileName = ""; if (OtherNote.Contains("filename")) { OtherFileName = OtherNote["filename"].ToString(); } byte[] OtherDocumentBody = Convert.FromBase64String(OtherNote["documentbody"].ToString()); MemoryStream fileStream2 = new MemoryStream(OtherDocumentBody); Document doc2 = new Document(fileStream); ResultWriter.InsertCell(); ResultWriter.Write(OtherFileName); doc.Compare(doc2, "a", DateTime.Now); if (doc.Revisions.Count == 0) { ResultWriter.InsertCell(); ResultWriter.Write("Duplicate Documents"); } ResultWriter.EndRow(); } } } ResultWriter.EndTable(); } } catch (Exception ex) { Log("Error while applying license: " + ex.Message, LogFilePath); } } } MemoryStream UpdateDoc = new MemoryStream(); if (Logging) { Log("Saving Document", LogFilePath); } Result.Save(UpdateDoc, SaveFormat.Docx); byte[] byteData = UpdateDoc.ToArray(); // Encode the data using base64. string encodedData = System.Convert.ToBase64String(byteData); if (Logging) { Log("Creating Attachment for result", LogFilePath); } // Add a Note do the endity. Entity NewNote = new Entity("annotation"); NewNote.Attributes.Add("objectid", new EntityReference(RecordType, ThisRecordId)); NewNote.Attributes.Add("subject", "Duplicate detection report"); // Set EncodedData to Document Body. NewNote.Attributes.Add("documentbody", encodedData); // Set the type of attachment. NewNote.Attributes.Add("mimetype", @"application\ms-word"); NewNote.Attributes.Add("notetext", "Duplicate detection report"); // Set the filename. NewNote.Attributes.Add("filename", "Duplicate detection report"); Guid NewNoteId = service.Create(NewNote); OutputAttachmentId.Set(executionContext, new EntityReference("annotation", NewNoteId)); if (Logging) { Log("Attachment Created Successfully", LogFilePath); } } else if (detectIn == 2) // Under whole organization. { Guid ThisRecordId = context.PrimaryEntityId; string RecordType = context.PrimaryEntityName; Document Result = new Document(); DocumentBuilder ResultWriter = new DocumentBuilder(Result); if (Logging) { Log("Working under all attachments under this Entity", LogFilePath); } QueryExpression RetrieveNoteQuery = new QueryExpression("annotation"); if (Logging) { Log("Executing Query to retrieve All Notes within this Entity", LogFilePath); } EntityCollection Notes = service.RetrieveMultiple(RetrieveNoteQuery); foreach (Entity Note in Notes.Entities) { try { if (Note.Contains("documentbody")) { string FileName = ""; if (Note.Contains("filename")) { FileName = Note["filename"].ToString(); } byte[] DocumentBody = Convert.FromBase64String(Note["documentbody"].ToString()); MemoryStream fileStream = new MemoryStream(DocumentBody); Document doc = new Document(fileStream); ResultWriter.Writeln("Comparing Document: " + FileName); ResultWriter.StartTable(); foreach (Entity OtherNote in Notes.Entities) { if (OtherNote.Id != Note.Id) { if (OtherNote.Contains("documentbody")) { string OtherFileName = ""; if (OtherNote.Contains("filename")) { OtherFileName = OtherNote["filename"].ToString(); } byte[] OtherDocumentBody = Convert.FromBase64String(OtherNote["documentbody"].ToString()); MemoryStream fileStream2 = new MemoryStream(OtherDocumentBody); Document doc2 = new Document(fileStream); ResultWriter.InsertCell(); ResultWriter.Write(OtherFileName); doc.Compare(doc2, "a", DateTime.Now); if (doc.Revisions.Count == 0) { ResultWriter.InsertCell(); ResultWriter.Write("Duplicate Documents"); } ResultWriter.EndRow(); } } } ResultWriter.EndTable(); } } catch (Exception ex) { Log("Error while applying license: " + ex.Message, LogFilePath); } } MemoryStream UpdateDoc = new MemoryStream(); if (Logging) { Log("Saving Document", LogFilePath); } Result.Save(UpdateDoc, SaveFormat.Docx); byte[] byteData = UpdateDoc.ToArray(); // Encode the data using base64. string encodedData = System.Convert.ToBase64String(byteData); if (Logging) { Log("Creating Attachment for result", LogFilePath); } // Add a Node to the entity. Entity NewNote = new Entity("annotation"); NewNote.Attributes.Add("objectid", new EntityReference(RecordType, ThisRecordId)); NewNote.Attributes.Add("subject", "Duplicate detection report"); // Set EncodedData to Document Body. NewNote.Attributes.Add("documentbody", encodedData); // Set the type of attachment. NewNote.Attributes.Add("mimetype", @"application\ms-word"); NewNote.Attributes.Add("notetext", "Duplicate detection report"); // Set the filename. NewNote.Attributes.Add("filename", "Duplicate detection report"); Guid NewNoteId = service.Create(NewNote); OutputAttachmentId.Set(executionContext, new EntityReference("annotation", NewNoteId)); if (Logging) { Log("Attachment Created Successfully", LogFilePath); } } }