private void UploadFileOnStorage(CloudAppConfig Config, MemoryStream fileStream)
        {
            string URIRequest = Config.ProductUri + "/storage/file/" + Config.FileName;

            string URISigned = Sign(URIRequest, Config.AppSID, Config.AppKey);

            try
            {
                System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(URISigned);
                req.Method      = "PUT";
                req.ContentType = "application/x-www-form-urlencoded";
                req.AllowWriteStreamBuffering = true;

                using (System.IO.Stream reqStream = req.GetRequestStream())
                {
                    reqStream.Write(fileStream.ToArray(), 0, (int)fileStream.Length);
                }
                string statusCode = null;

                using (System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)req.GetResponse())
                {
                    statusCode = response.StatusCode.ToString();
                }
            }
            catch (System.Net.WebException webex)
            {
                throw new Exception(webex.Message);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        private void DeleteDocumentFromStorage(CloudAppConfig Config, string FileName)
        {
            string URIRequest = Config.ProductUri + "/storage/file/" + FileName;
            string URISigned  = Sign(URIRequest, Config.AppSID, Config.AppKey);

            ProcessCommand(URISigned, "DELETE");
        }
 private MemoryStream DownloadFile(CloudAppConfig Config, string OutputFileName)
 {
     try
     {
         string URIRequest = Config.ProductUri + "/words/" + OutputFileName + "?format=" + "Docx";
         string URISigned  = Sign(URIRequest, Config.AppSID, Config.AppKey);
         using (Stream responseStream = ProcessCommand(URISigned, "GET"))
         {
             using (MemoryStream memoryStream = new MemoryStream())
             {
                 responseStream.CopyTo(memoryStream);
                 return(memoryStream);
             }
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
 private List <string> GetFieldsName(CloudAppConfig Config)
 {
     try
     {
         List <string>          FieldsName = new List <string>();
         string                 URIRequest = Config.ProductUri + "/words/" + Config.FileName + "/mailMergeFieldNames";
         string                 URISigned  = Sign(URIRequest, Config.AppSID, Config.AppKey);
         StreamReader           reader     = new StreamReader(ProcessCommand(URISigned, "GET"));
         string                 strJSON    = reader.ReadToEnd();
         var                    jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(strJSON), new System.Xml.XmlDictionaryReaderQuotas());
         var                    root       = XElement.Load(jsonReader);
         IEnumerable <XElement> MainList   = root.XPathSelectElements("//FieldNames/Names/item");
         foreach (XElement el in MainList)
         {
             FieldsName.Add(el.Value);
         }
         return(FieldsName);
     }
     catch (Exception Ex)
     {
         throw new Exception(Ex.Message);
     }
 }
        private string ExecuteMailMerge(CloudAppConfig Config, string Xml)
        {
            try
            {
                string URIRequest = Config.ProductUri + "/words/" + Config.FileName + "/executeMailMerge";
                string URISigned  = Sign(URIRequest, Config.AppSID, Config.AppKey);

                string outputFileName = null;

                using (Stream responseStream = ProcessCommand(URISigned, "POST", Xml, "xml"))
                {
                    string strResponse = null;

                    using (StreamReader reader = new StreamReader(responseStream))
                    {
                        strResponse = reader.ReadToEnd();
                    }

                    using (MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(strResponse)))
                    {
                        XPathDocument  xPathDoc  = new XPathDocument(ms);
                        XPathNavigator navigator = xPathDoc.CreateNavigator();

                        //get File Name
                        XPathNodeIterator nodes = navigator.Select("/SaaSposeResponse/Document/FileName");
                        nodes.MoveNext();
                        outputFileName = nodes.Current.InnerXml;
                        return(outputFileName);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void UploadFileOnStorage(CloudAppConfig Config, MemoryStream fileStream)
        {
            string URIRequest = Config.ProductUri + "/storage/file/" + Config.FileName;

            string URISigned = Sign(URIRequest, Config.AppSID, Config.AppKey);

            try
            {
                System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(URISigned);
                req.Method = "PUT";
                req.ContentType = "application/x-www-form-urlencoded";
                req.AllowWriteStreamBuffering = true;

                using (System.IO.Stream reqStream = req.GetRequestStream())
                {
                    reqStream.Write(fileStream.ToArray(), 0, (int)fileStream.Length);
                }
                string statusCode = null;

                using (System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)req.GetResponse())
                {
                    statusCode = response.StatusCode.ToString();
                }
            }
            catch (System.Net.WebException webex)
            {
                throw new Exception(webex.Message);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        private List<string> GetFieldsName(CloudAppConfig Config)
        {
            try
            {

                List<string> FieldsName = new List<string>();
                string URIRequest = Config.ProductUri + "/words/" + Config.FileName + "/mailMergeFieldNames";
                string URISigned = Sign(URIRequest, Config.AppSID, Config.AppKey);
                StreamReader reader = new StreamReader(ProcessCommand(URISigned, "GET"));
                string strJSON = reader.ReadToEnd();
                var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(strJSON), new System.Xml.XmlDictionaryReaderQuotas());
                var root = XElement.Load(jsonReader);
                IEnumerable<XElement> MainList = root.XPathSelectElements("//FieldNames/Names/item");
                foreach (XElement el in MainList)
                    FieldsName.Add(el.Value);
                return FieldsName;
            }
            catch (Exception Ex)
            {
                throw new Exception(Ex.Message);
            }
        }
        private string ExecuteMailMerge(CloudAppConfig Config, string Xml)
        {
            try
            {
                string URIRequest = Config.ProductUri + "/words/" + Config.FileName + "/executeMailMerge";
                string URISigned = Sign(URIRequest, Config.AppSID, Config.AppKey);

                string outputFileName = null;

                using (Stream responseStream = ProcessCommand(URISigned, "POST", Xml, "xml"))
                {
                    string strResponse = null;

                    using (StreamReader reader = new StreamReader(responseStream))
                    {
                        strResponse = reader.ReadToEnd();
                    }

                    using (MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(strResponse)))
                    {
                        XPathDocument xPathDoc = new XPathDocument(ms);
                        XPathNavigator navigator = xPathDoc.CreateNavigator();

                        //get File Name
                        XPathNodeIterator nodes = navigator.Select("/SaaSposeResponse/Document/FileName");
                        nodes.MoveNext();
                        outputFileName = nodes.Current.InnerXml;
                        return outputFileName;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 private MemoryStream DownloadFile(CloudAppConfig Config, string OutputFileName)
 {
     try
     {
         string URIRequest = Config.ProductUri + "/words/" + OutputFileName + "?format=" + "Docx";
         string URISigned = Sign(URIRequest, Config.AppSID, Config.AppKey);
         using (Stream responseStream = ProcessCommand(URISigned, "GET"))
         {
             using (MemoryStream memoryStream = new MemoryStream())
             {
                 responseStream.CopyTo(memoryStream);
                 return memoryStream;
             }
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
 private void DeleteDocumentFromStorage(CloudAppConfig Config, string FileName)
 {
     string URIRequest = Config.ProductUri + "/storage/file/" + FileName;
     string URISigned = Sign(URIRequest, Config.AppSID, Config.AppKey);
     ProcessCommand(URISigned, "DELETE");
 }
        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;
            }
        }
        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;
            }
        }