Friday, July 1, 2011

Server Side Example - Fetch

Hi,
Here is an example of update entity 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 Fetch
    {
        static void Main(string[] args)
        {
            Button1_Click();
        }

        static void Button1_Click()
        {
            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;
                //fetch example
         
       string opportunity_count = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                                              <entity name='contact'>
                                                <attribute name='fullname' />
                                                <attribute name='new_customernumber' />
                                                <attribute name='donotemail' />
                                                <attribute name='birthdate' />
                                                <order attribute='new_customernumber' descending='true' />
                                                <filter type='and'>
                                                  <filter type='or'>
                                                    <condition attribute='birthdate' operator='next-seven-days' />
                                                    <condition attribute='birthdate' operator='last-x-days' value='30' />
                                                  </filter>
                                                </filter>
                                              </entity>
                                            </fetch>";

                EntityCollection contact_count_result = service.RetrieveMultiple(new Microsoft.Xrm.Sdk.Query.FetchExpression(opportunity_count));
                //Create loop of the result                 foreach (var c in contact_count_result.Entities){}
                //end fetch example
                Console.ReadKey();
                //This code will clear the textboxes after the contact is created.
            }
        }
    }
}

Thanks,
Rami Heleg

No comments:

Post a Comment