Friday, July 1, 2011

Server Side Example - Run query expression

Hi,
Here is an example of run query QueryExpression in CRM 2011.
Example project for Console application.

Before run the application musat add reference to dlls:
1. Microsoft.Xrm.Sdk.dll ( get this file from SDK 2011)
2. Microsoft.Crm.Sdk.Proxy.dll ( get this file from SDK 2011)

Before run the console application add Service References to:

http://ServerName:5555/XRMServices/2011/Organization.svc

Console Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Description;
using System.Net;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk;

namespace crm2011Test1
{
    class query2
    {
        static void Main(string[] args)
        {
            Button1_Click();
        }
        static void Button1_Click()
        {
            //Authenticate using credentials of the logged in user;       
            ClientCredentials Credentials = new ClientCredentials();
            Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
            //This URL needs to be updated to match the servername and Organization for the environment.
            Uri OrganizationUri = new Uri("http://crmserver:5555/crm2011dev/XRMServices/2011/Organization.svc");
            Uri HomeRealmUri = null;
            using (Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy serviceProxy = new Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))
            {
                serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
                IOrganizationService service = (IOrganizationService)serviceProxy;
                // Create query in contact entity                Microsoft.Xrm.Sdk.Query.QueryExpression qe = new Microsoft.Xrm.Sdk.Query.QueryExpression();
                qe.EntityName = "contact";
                Microsoft.Xrm.Sdk.Query.FilterExpression filter1 = new Microsoft.Xrm.Sdk.Query.FilterExpression();
                filter1.FilterOperator = Microsoft.Xrm.Sdk.Query.LogicalOperator.And;
                filter1.Conditions.Add(new Microsoft.Xrm.Sdk.Query.ConditionExpression("firstname", Microsoft.Xrm.Sdk.Query.ConditionOperator.Equal, "amos"));
                filter1.FilterOperator = Microsoft.Xrm.Sdk.Query.LogicalOperator.Or;
                filter1.Conditions.Add(new Microsoft.Xrm.Sdk.Query.ConditionExpression("createdon", Microsoft.Xrm.Sdk.Query.ConditionOperator.NextXDays, 7));
                filter1.Conditions.Add(new Microsoft.Xrm.Sdk.Query.ConditionExpression("createdon", Microsoft.Xrm.Sdk.Query.ConditionOperator.NextXDays, 30));
                qe.Criteria = filter1;
                //first retrive
                EntityCollection ec = service.RetrieveMultiple(qe);
                //create loog on result
                foreach (Entity act in ec.Entities){}
                Console.ReadKey();            }
        }
    }
}

1 comment:

  1. http://social.microsoft.com/Forums/en/crmdevelopment/thread/dde428d0-4383-46d7-9a21-584462d45860

    ReplyDelete