Hi all,
I need to make this query in c#
select CreatedOn, * from Lead
where CreatedOn between '2017-01-31T00:00:00.000' AND '2017-08-31T00:00:00.000'
and OwnerIdName = 'Abdul Wahab'
I wrote the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query; //QueryExpression
using System.ServiceModel.Description;
namespace ConnectionWithCRM
{
class Program
{
static IOrganizationService _service;
static void Main(string[] args)
{
ConnectToMSCRM("XXXXXXXXX", "XXXXX", "XXXXXXXXXXXXXXXXXXXX");
Guid userid = ((WhoAmIResponse)_service.Execute(new WhoAmIRequest())).UserId;
if (userid != Guid.Empty)
{
//Console.WriteLine("Connection Established Successfully");
//Console.ReadKey();
EntityCollection ec = null;
string Id = Data.id.ToString();
ec = GetEntityCollection(service, EntityName.Template, TemplateAttributes.Id, Id, new ColumnSet(TemplateAttributes.Id));
if (ec.Entities.Count == 0) //Check for EntityCollection count
{
Entity Entity = new Entity(EntityName.Template);
Entity[TemplateAttributes.Id] = Id;
Entity[TemplateAttributes.Name] = Data.name;
service.Create(Entity);
}
}
}
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();
}
}
private static EntityCollection GetEntityCollection(IOrganizationService service, string entityName, string attributeName, string attributeValue, ColumnSet cols)
{
QueryExpression query = new QueryExpression
{
EntityName = entityName,
ColumnSet = cols,
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression
{
AttributeName = attributeName,
Operator = ConditionOperator.Equal,
Values = { attributeValue }
}
}
}
};
return service.RetrieveMultiple(query);
}
}
}
I am stuck on this condition logic. How do I make conditions as my sql query have, using this code.?
Thank You
*This post is locked for comments