简体   繁体   中英

Console Application - Send/Write JSON files to Azure Blob Storage

I have a Console Application created in C# that extracts data from Dynamics 365 Customer Voice. The following code extracts data of Projects, Surveys, Questions and Question Responses respectivelly. The data extracted are written in JSON files that are saved on my HD.

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UmbracoMVC.App_Code.Infrastructure.CRMIntegration.Business;
using System.IO;
using Microsoft.Crm.Sdk.Messages;
using System.Globalization;
using E2BWorkflow.Classes;
using System.Web.Management;
using System.ServiceModel;
using System.Drawing;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;  

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            GetCustomerVoice();
        }

        public static void GetCustomerVoice()
        {
            IOrganizationService crmServiceTo;

            crmServiceTo = Connect.Crm("myemail@e-mail.com", "mypassword", "https://mydynamics.crm4.dynamics.com/XRMServices/2011/Organization.svc");

            // Get Projects
            QueryExpression qP = new QueryExpression("msfp_project");
            qP.ColumnSet = new ColumnSet(true);
            var projects = crmServiceTo.RetrieveMultiple(qP);
            foreach (var p in projects.Entities)
            {
                msfp_project project = p.ToEntity<msfp_project>();
            }
            var projectsList = projects.Entities.Select(
                s => new {
                    msfp_projectId = s.Attributes["msfp_projectid"],
                    msfp_name = s.Attributes["msfp_name"]
                }
            ).ToList();

            var jsonSerialiser = new JavaScriptSerializer();
            var json = jsonSerialiser.Serialize(projectsList);
            System.IO.File.WriteAllText(@"C:\MyDirectory\projects.json", json);

            // Get Surveys
            QueryExpression qS = new QueryExpression("msfp_survey");
            qS.ColumnSet = new ColumnSet(true);
            var surveys = crmServiceTo.RetrieveMultiple(qS);
            foreach (var s in surveys.Entities)
            {
                msfp_survey survey = s.ToEntity<msfp_survey>();
            }
            var surveysList = surveys.Entities.Select(
                s => new {
                    msfp_surveyId = s.Attributes["msfp_surveyid"],
                    msfp_name = s.Attributes.Contains("msfp_name") ? s.GetAttributeValue<string>("msfp_questiontext") : "",
                    msfp_anonymousurl = s.Attributes.Contains("msfp_anonymousurl") ? s.GetAttributeValue<string>("msfp_anonymousurl") : "",
                    msfp_friendlyname = s.Attributes.Contains("msfp_friendlyname") ? s.GetAttributeValue<string>("msfp_friendlyname") : "",
                    msfp_surveyurl = s.Attributes.Contains("msfp_surveyurl") ? s.GetAttributeValue<string>("msfp_surveyurl") : "",
                    msfp_projectId = s.Attributes.Contains("msfp_project") && s.GetAttributeValue<EntityReference>("msfp_project").Id != null ? s.GetAttributeValue<EntityReference>("msfp_project").Id : Guid.Empty
                }
            ).ToList();

            jsonSerialiser = new JavaScriptSerializer();
            json = jsonSerialiser.Serialize(surveysList);
            System.IO.File.WriteAllText(@"C:\MyDirectory\surveys.json", json);

            // Get Questions
            QueryExpression qQ = new QueryExpression("msfp_question");
            qQ.ColumnSet = new ColumnSet(true);
            var questions = crmServiceTo.RetrieveMultiple(qQ);
            foreach (var q in questions.Entities)
            {
                msfp_question question = q.ToEntity<msfp_question>();
            }
            var questionsList = questions.Entities.Select(
                s => new {
                    msfp_questionId = s.Attributes["msfp_questionid"],
                    msfp_questionText = s.Attributes.Contains("msfp_questiontext") ? s.GetAttributeValue<string>("msfp_questiontext") : "",
                    msfp_surveyId = s.Attributes.Contains("msfp_survey") && s.GetAttributeValue<EntityReference>("msfp_survey").Id != null ? s.GetAttributeValue<EntityReference>("msfp_survey").Id : Guid.Empty
                }
            ).ToList();

            jsonSerialiser = new JavaScriptSerializer();
            json = jsonSerialiser.Serialize(questionsList);
            System.IO.File.WriteAllText(@"C:\MyDirectory\questions.json", json);

            //Get Question Responses
            QueryExpression qR = new QueryExpression("msfp_questionresponse");
            qR.ColumnSet = new ColumnSet(true);
            var responses = crmServiceTo.RetrieveMultiple(qR);
            foreach (var r in responses.Entities)
            {
                msfp_questionresponse response = r.ToEntity<msfp_questionresponse>();
            }

            var responsesList = responses.Entities.Select(
                s => new {
                    msfp_questionresponseId = s.Attributes["msfp_questionresponseid"],
                    msfp_questionresponse = s.Attributes["msfp_name"],
                    msfp_questionId = s.GetAttributeValue<EntityReference>("msfp_questionid").Id
                }
            ).ToList();

            jsonSerialiser = new JavaScriptSerializer();
            json = jsonSerialiser.Serialize(responsesList);
            System.IO.File.WriteAllText(@"C:\MyDirectory\responses.json", json);

            Console.WriteLine("END");
            Console.ReadKey();
        }
    }
}

Now what I need is to have the JSONs be saved on a Azure Blob Storage account. I searched many solutions on the internet, but not sure what should I use for Console Application.

I have a Console Application created in C# that extracts data from Dynamics 365 Customer Voice. The following code extracts data of Projects, Surveys, Questions and Question Responses respectivelly. The data extracted are written in JSON files that are saved on my HD.

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UmbracoMVC.App_Code.Infrastructure.CRMIntegration.Business;
using System.IO;
using Microsoft.Crm.Sdk.Messages;
using System.Globalization;
using E2BWorkflow.Classes;
using System.Web.Management;
using System.ServiceModel;
using System.Drawing;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;  

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            GetCustomerVoice();
        }

        public static void GetCustomerVoice()
        {
            IOrganizationService crmServiceTo;

            crmServiceTo = Connect.Crm("myemail@e-mail.com", "mypassword", "https://mydynamics.crm4.dynamics.com/XRMServices/2011/Organization.svc");

            // Get Projects
            QueryExpression qP = new QueryExpression("msfp_project");
            qP.ColumnSet = new ColumnSet(true);
            var projects = crmServiceTo.RetrieveMultiple(qP);
            foreach (var p in projects.Entities)
            {
                msfp_project project = p.ToEntity<msfp_project>();
            }
            var projectsList = projects.Entities.Select(
                s => new {
                    msfp_projectId = s.Attributes["msfp_projectid"],
                    msfp_name = s.Attributes["msfp_name"]
                }
            ).ToList();

            var jsonSerialiser = new JavaScriptSerializer();
            var json = jsonSerialiser.Serialize(projectsList);
            System.IO.File.WriteAllText(@"C:\MyDirectory\projects.json", json);

            // Get Surveys
            QueryExpression qS = new QueryExpression("msfp_survey");
            qS.ColumnSet = new ColumnSet(true);
            var surveys = crmServiceTo.RetrieveMultiple(qS);
            foreach (var s in surveys.Entities)
            {
                msfp_survey survey = s.ToEntity<msfp_survey>();
            }
            var surveysList = surveys.Entities.Select(
                s => new {
                    msfp_surveyId = s.Attributes["msfp_surveyid"],
                    msfp_name = s.Attributes.Contains("msfp_name") ? s.GetAttributeValue<string>("msfp_questiontext") : "",
                    msfp_anonymousurl = s.Attributes.Contains("msfp_anonymousurl") ? s.GetAttributeValue<string>("msfp_anonymousurl") : "",
                    msfp_friendlyname = s.Attributes.Contains("msfp_friendlyname") ? s.GetAttributeValue<string>("msfp_friendlyname") : "",
                    msfp_surveyurl = s.Attributes.Contains("msfp_surveyurl") ? s.GetAttributeValue<string>("msfp_surveyurl") : "",
                    msfp_projectId = s.Attributes.Contains("msfp_project") && s.GetAttributeValue<EntityReference>("msfp_project").Id != null ? s.GetAttributeValue<EntityReference>("msfp_project").Id : Guid.Empty
                }
            ).ToList();

            jsonSerialiser = new JavaScriptSerializer();
            json = jsonSerialiser.Serialize(surveysList);
            System.IO.File.WriteAllText(@"C:\MyDirectory\surveys.json", json);

            // Get Questions
            QueryExpression qQ = new QueryExpression("msfp_question");
            qQ.ColumnSet = new ColumnSet(true);
            var questions = crmServiceTo.RetrieveMultiple(qQ);
            foreach (var q in questions.Entities)
            {
                msfp_question question = q.ToEntity<msfp_question>();
            }
            var questionsList = questions.Entities.Select(
                s => new {
                    msfp_questionId = s.Attributes["msfp_questionid"],
                    msfp_questionText = s.Attributes.Contains("msfp_questiontext") ? s.GetAttributeValue<string>("msfp_questiontext") : "",
                    msfp_surveyId = s.Attributes.Contains("msfp_survey") && s.GetAttributeValue<EntityReference>("msfp_survey").Id != null ? s.GetAttributeValue<EntityReference>("msfp_survey").Id : Guid.Empty
                }
            ).ToList();

            jsonSerialiser = new JavaScriptSerializer();
            json = jsonSerialiser.Serialize(questionsList);
            System.IO.File.WriteAllText(@"C:\MyDirectory\questions.json", json);

            //Get Question Responses
            QueryExpression qR = new QueryExpression("msfp_questionresponse");
            qR.ColumnSet = new ColumnSet(true);
            var responses = crmServiceTo.RetrieveMultiple(qR);
            foreach (var r in responses.Entities)
            {
                msfp_questionresponse response = r.ToEntity<msfp_questionresponse>();
            }

            var responsesList = responses.Entities.Select(
                s => new {
                    msfp_questionresponseId = s.Attributes["msfp_questionresponseid"],
                    msfp_questionresponse = s.Attributes["msfp_name"],
                    msfp_questionId = s.GetAttributeValue<EntityReference>("msfp_questionid").Id
                }
            ).ToList();

            jsonSerialiser = new JavaScriptSerializer();
            json = jsonSerialiser.Serialize(responsesList);
            System.IO.File.WriteAllText(@"C:\MyDirectory\responses.json", json);

            Console.WriteLine("END");
            Console.ReadKey();
        }
    }
}

Now what I need is to have the JSONs be saved on a Azure Blob Storage account. I searched many solutions on the internet, but not sure what should I use for Console Application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM