Bhoopathi

"Be Somebody Nobody Thought You could Be"

Wednesday, July 27

Filter Expression for MS Dynamics CRM using console application

/** Filter Expression - MS Dynamics CRM **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;

namespace mailsend
{
    class Program
    {
        
        static IOrganizationService _service;

        static void Main(string[] args)
        {
            Guid userid = ((WhoAmIResponse)_service.Execute(new WhoAmIRequest())).UserId;
            if (userid != Guid.Empty)
            {
                Console.WriteLine("connection successfuly established");
                Console.ReadKey();

            }
            
            Entity account = new Entity();
            account.LogicalName = "account";
            account.Attributes["name"] = "11finally you are really super";
            account.Attributes["emailaddress1"] = "super@keka.com";
            account.Attributes["websiteurl"] = "crmb.crm8.dynamics.com";
            account.Attributes["new_total"] = "1000001";
            account.Attributes["new_credit"] = "11111";
            account.Attributes["int_facebook"] = "Bhoopathi Technologies";
            //account.Attributes["parentaccountid"] = "BD2B06F1-2036-E611-80FC-5065F38CE4F1";
            account.Attributes["telephone1"] = "88888888";
            Console.Write("{0} {1} created, ", account.LogicalName, account.Attributes["name"], account.Attributes["int_facebook"]);
            Guid accountId = _service.Create(account);

            ColumnSet cols = new ColumnSet(new String[] { "name", "new_credit", "int_facebook", "telephone1", "emailaddress1", "websiteurl", "new_total" });

            account= _service.Retrieve("account", accountId, cols);
            Console.Write("retrieved ");

            account.Attributes["tickersymbol"] = "KBG";
            account.Attributes["address1_county"] = "india";
            account.Attributes["address1_city"] = "hyderabad";
            Console.WriteLine("and updated.");
            _service.Update(account);

            QueryExpression qe = new QueryExpression("account");
            qe.ColumnSet = new ColumnSet("name", "address1_city", "address1_country", "int_facebook", "telephone1", "emailaddress1", "websiteurl");
            qe.Distinct=false;
            
            ConditionExpression ce = new ConditionExpression();
            ce.AttributeName = "int_facebook";
            ce.Operator = ConditionOperator.BeginsWith;
            ce.Values.Add("bh");

            Console.WriteLine("NO of columns with fb starts with 'Bh' \n" + ce.Values.Count);

            ConditionExpression ce1 = new ConditionExpression();
            ce1.AttributeName = "address1_city";
            ce1.Operator = ConditionOperator.BeginsWith;
            ce1.Values.Add("hyd");

            Console.WriteLine("NO of columns with Addr starts with 'HYD' \n" + ce1.Values.Count);

            FilterExpression fe = new FilterExpression(LogicalOperator.Or);
            fe.Conditions.Add(ce);
            fe.Conditions.Add(ce1);

            Console.WriteLine("NO of columns with Addr starts with 'HYD' && FB with 'BH' \n");

            //FilterExpression fe1 = new FilterExpression(LogicalOperator.Or);

            //ConditionExpression ce2 = new ConditionExpression();
            //ce2.AttributeName = "address1_country";
            //ce2.Operator = ConditionOperator.BeginsWith;
            //ce2.Values.Add("ind");

            //fe1.Conditions.Add(ce2);

            qe.Criteria.AddFilter(fe);
            //qe.Criteria.AddFilter(fe1);

            EntityCollection ec = _service.RetrieveMultiple(qe);
            

            foreach(var c in ec.Entities)
            {
                Console.WriteLine("name: {0} ", c.GetAttributeValue<string>("name"));
                Console.WriteLine("facebook: {0}", c.GetAttributeValue<string>("int_facebook"));
                Console.WriteLine("City: {0}", c.GetAttributeValue<string>("address1_city"));
                Console.ReadKey();
            }
        }
        
        public static void ConnectToMSCRM(string UserName, string Password, string SoapOrgServiceUri)
        {
            try
            {
                ClientCredentials credentials = new ClientCredentials();
                credentials.UserName.UserName = UserName;
                credentials.UserName.Password = Password;
                Uri serviceUri = new Uri(SoapOrgServiceUri);
                OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
                proxy.EnableProxyTypes();
                _service = (IOrganizationService)proxy;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error while connecting to CRM " + ex.Message);
                Console.ReadKey();
            }
        }
    }
}