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);
            }
        }
        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)
        {
            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;
            }
        }
 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);
     }
 }