Friday, May 11, 2012

CRM 2011-Retreiving data from crm using LINQ with early binding and OrganisationServiceContext

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using System.ServiceModel.Description;
using System.ComponentModel;
using System.ServiceModel;

namespace CrmTestApp
{
   
    public partial class MainWindow : Window
    {
      
        public ObservableCollection<Contact> collect { get; set; }
        public static EntityCollection result;
        public static OrganizationServiceProxy _serviceProxy;
       public static OrganizationServiceContext context;
  
        public MainWindow()
        {
            InitializeComponent();
         
        }
   
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {  
             
         
            IServiceConfiguration<IOrganizationService> orgConfigInfo =
                ServiceConfigurationFactory.CreateConfiguration<IOrganizationService>(new Uri("http://ntiercrm/cca/XRMServices/2011/Organization.svc"));
           
            var creds = new ClientCredentials();
            using (_serviceProxy = new OrganizationServiceProxy(orgConfigInfo, creds))
            {
                _serviceProxy.EnableProxyTypes();  //To support early binding
                context = new OrganizationServiceContext(_serviceProxy);
                var query = from c in context.CreateQuery<Contact>()
                            where c.MobilePhone == textBox1.Text
                             select c;
                collect = new ObservableCollection<Contact>(query.ToList()); //Converting IQueriable to Observable collection
                dataGrid1.ItemsSource = collect;
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {

            IServiceConfiguration<IOrganizationService> orgConfigInfo =
                ServiceConfigurationFactory.CreateConfiguration<IOrganizationService>(new Uri("http://ntiercrm/cca/XRMServices/2011/Organization.svc"));

            var creds = new ClientCredentials();
            using (_serviceProxy = new OrganizationServiceProxy(orgConfigInfo, creds))
            {
                _serviceProxy.EnableProxyTypes();  //To support early binding
                context = new OrganizationServiceContext(_serviceProxy);
                var query = from c in context.CreateQuery<Contact>()
                            select c;
                collect = new ObservableCollection<Contact>(query.ToList()); //Converting IQueriable to Observable collection
                dataGrid1.ItemsSource = collect;
            }
        }

     }
}

No comments:

Post a Comment