0
votes

I am new in Dynamics CRM and I want to create a console application that can create a new record for account entity and can display a list of all account names from account entity from Dynamics CRM online.

This is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Tooling.Connector;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int choice;
            CrmServiceClient crmConn = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CRM"].ConnectionString);
            IOrganizationService crmService = crmConn.OrganizationServiceProxy;

            Entity acc = new Entity("account");
            String account_name;
            Console.WriteLine("Press 1 to Create a new account or Press 2 to view list of available accounts.");
            choice = Convert.ToInt32(Console.ReadLine());
            switch (choice)
            {
                case 1:
                    Console.WriteLine("Enter Name of Account to Create ?");
                    account_name = Console.ReadLine();
                    acc["name"] = account_name;
                    crmService.Create(acc);
                    Console.WriteLine("*****An account with name {0} is created successfully*****", account_name);
                    Console.WriteLine();
                    Console.WriteLine("Press any key to exit..");
                    Console.ReadKey();
                    break;
                case 2:
                    //code to display list of all account names in CRM.
                    Console.ReadKey();
                    break;
                default:
                    Console.WriteLine("Wrong input...");
                    Console.ReadKey();
                    break;

            }
        }

    }
}
2
if you want to retrieve data you need to do a QueryExpressionGuido Preite
QueryExpression query = new QueryExpression { EntityName = "account", ColumnSet = new ColumnSet(new string[] { "name" }) }; EntityCollection account = service.RetrieveMultiple(query); like this?Dev
yes, like that.Guido Preite
Thanks @GuidoPreite, I've done it.Dev
If this is resolved, please add an answer. It'd be great also if you could read this community discussion about urgent begging, thanks.halfer

2 Answers

1
votes

Here is my answer

Inside Case 2: I've used following code:

QueryExpression query = new QueryExpression { EntityName = "account", ColumnSet = new ColumnSet(new string[] { "name" }) };
EntityCollection account = crmService.RetrieveMultiple(query);
string name = "";
foreach (var count in account.Entities)
{
    name = count.GetAttributeValue<string>("name");
    Console.WriteLine(name);
}
Console.ReadKey();
0
votes

For case 2 I would better go with approach of using FetchXML. Go to advanced find and get your filter criteria and then download the xml file.

Then incorporate the fetchxml in your code and retrieve data like this

var objCollection = crmService.RetrieveMultiple(new FetchExpression(fetchXMLString));

Hope it helps