private void button1_Click_1(object sender, EventArgs e)
 {
     if (!String.IsNullOrEmpty(outputpath.Text))
     {
         System.Diagnostics.Process.Start(outputpath.Text);
     }
     else
     {
         MessageText.AppendText("First select a download location.");
         MessageText.AppendText(Environment.NewLine);
     }
 }
Beispiel #2
0
 private void AddMessage(string text)
 {
     if (this.InvokeRequired)
     {
         var d = new AddMessageDelegate(AddMessage);
         this.BeginInvoke(d, new object[] { text });
     }
     else
     {
         MessageText.AppendText(text.Replace("\n", Environment.NewLine));
         MessageText.AppendText(Environment.NewLine);
     }
 }
        private void button1_Click(object sender, EventArgs e)
        {
            DownloadButton.Enabled = false;
            BackButton.Enabled     = false;

            MessageText.Text = "";


            if (String.IsNullOrEmpty(outputpath.Text))
            {
                MessageText.Text = "Please select a folder to download."; DownloadButton.Enabled = true; BackButton.Enabled = true; return;
            }
            if (String.IsNullOrEmpty(FetchXMLData.Text))
            {
                MessageText.Text = "Please provide a valid fetch xml."; DownloadButton.Enabled = true; BackButton.Enabled = true; return;
            }



            String           Location = @"" + outputpath.Text;
            String           fetch    = FetchXMLData.Text.Replace("\"", "\'");
            EntityCollection enColl   = _service.RetrieveMultiple(new FetchExpression(fetch));
            int i = 0;

            MessageText.AppendText("Total records found: " + enColl.Entities.Count);
            MessageText.AppendText(Environment.NewLine);
            if (enColl.Entities.Count == 0)
            {
                MessageText.AppendText("No records found for this fetch xml. Please try again");
                MessageText.AppendText(Environment.NewLine);

                DownloadButton.Enabled = true;
                BackButton.Enabled     = false;
                return;
            }
            else if (enColl.Entities.Count == 5000)
            {
                MessageText.AppendText("More than 4999 records can't be processed in one go for downloading. Please provide smaller set xml.");
                MessageText.AppendText(Environment.NewLine);
                DownloadButton.Enabled = true;
                BackButton.Enabled     = false;
                return;
            }

            foreach (Entity en in enColl.Entities)
            {
                try
                {
                    QueryExpression qe = new QueryExpression("annotation");
                    qe.Criteria.AddCondition("objectid", ConditionOperator.Equal, en.Id);
                    qe.ColumnSet = new ColumnSet(true);
                    EntityCollection AnnotationColl = _service.RetrieveMultiple(qe);

                    foreach (Entity annotationRec in AnnotationColl.Entities)
                    {
                        if (annotationRec.Contains("filename"))
                        {
                            // String Location = @"C:\test\";
                            String filename       = annotationRec.GetAttributeValue <String>("filename");
                            String noteBody       = annotationRec.GetAttributeValue <String>("documentbody");
                            string outputFileName = @"" + Location + "\\" + filename;

                            if (!string.IsNullOrEmpty(noteBody))
                            {
                                // Download the attachment in the current execution folder.
                                byte[] fileContent = Convert.FromBase64String(noteBody);
                                System.IO.File.WriteAllBytes(outputFileName, fileContent);
                                MessageText.AppendText(++i + " files downloaded. File name: " + filename);
                                MessageText.AppendText(Environment.NewLine);
                            }
                            else
                            {
                                MessageText.AppendText("File content is empty or cannot be retrieved");
                                MessageText.AppendText(Environment.NewLine);
                            }
                        }
                    }
                } catch (Exception ex)
                {
                    MessageText.AppendText("Error in downloading attachments.Details:" + ex.Message);
                    MessageText.AppendText(Environment.NewLine);
                    DownloadButton.Enabled = true;
                    BackButton.Enabled     = true;
                    return;
                }
            }
            MessageText.AppendText("Downloading completed.");
            MessageText.AppendText(Environment.NewLine);

            DownloadButton.Enabled = true;
            BackButton.Enabled     = true;
        }
        private void Connect_Click(object sender, EventArgs e)
        {
            MessageText.Text = "";
            Connect.Enabled  = false;

            if (String.IsNullOrEmpty(orgurl.Text))
            {
                MessageText.Text = "Org url is empty."; Connect.Enabled = true; return;
            }
            if (String.IsNullOrEmpty(domainname.Text))
            {
                MessageText.Text = "Domain Name is empty."; Connect.Enabled = true; return;
            }
            if (String.IsNullOrEmpty(username.Text))
            {
                MessageText.Text = "User Name is empty."; Connect.Enabled = true; return;
            }
            if (String.IsNullOrEmpty(password.Text))
            {
                MessageText.Text = "Password is empty."; Connect.Enabled = true; return;
            }


            else
            {
                MessageText.Text = "";
            }


            #region ConnecttoCRM
            IOrganizationService service = null;
            try
            {
                ClientCredentials cre = new ClientCredentials();
                cre.Windows.ClientCredential.Domain   = domainname.Text;
                cre.Windows.ClientCredential.UserName = username.Text;
                cre.Windows.ClientCredential.Password = password.Text;

                Uri serviceUri = new Uri(orgurl.Text);
                OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, cre, null);
                proxy.EnableProxyTypes();
                service = (IOrganizationService)proxy;
            }
            catch (Exception ex)
            {
                MessageText.AppendText("Error in connecting to crm instance.");
                MessageText.AppendText(Environment.NewLine);
                MessageText.AppendText(ex.Message);
                MessageText.AppendText(Environment.NewLine);
                Connect.Enabled = true;
                return;
            }
            #endregion

            if (service == null)
            {
                MessageText.Text = "Organisation service object is null. Contact administrator.";
                Connect.Enabled  = true;
                return;
            }

            // DownloadAttachment obj1 = new DownloadAttachment();
            //this.Hide();
            //obj1.Show();


            DownloadAttachment obj1 = new DownloadAttachment(service);
            obj1.Show();
            this.Hide();
        }