Thursday, May 10, 2012

CRM 2011-Retrieving data from crm 2011 using FetchXML

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;

namespace CrmTestApp
{
    public partial class MainWindow : Window
    {
        public ObservableCollection<contact> collect { get; set; }
        public static EntityCollection result;
        public static OrganizationServiceProxy _serviceProxy;
        public MainWindow()
        {
            InitializeComponent();          
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
           
            string fetch = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
  <entity name='contact'>
    <attribute name='fullname' />
    <attribute name='parentcustomerid' />
    <attribute name='telephone1' />
    <attribute name='emailaddress1' />
    <attribute name='contactid' />
    <order attribute='fullname' descending='false' />
    <filter type='and'>
      <condition attribute='ownerid' operator='eq-userid' />
      <condition attribute='statecode' operator='eq' value='0' />
   <condition attribute='mobilephone' operator='eq' value='" + textBox1.Text + @"' />
    </filter>
  </entity>
</fetch>";

            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))
            {

                result = _serviceProxy.RetrieveMultiple(new FetchExpression(fetch));

                collect = new ObservableCollection<contact>();
                foreach (var c in result.Entities)
                {

                    collect.Add(new contact()
                    {
                        Email = c.Attributes["emailaddress1"].ToString(),
                        Id = c.Attributes["contactid"].ToString(),
                        Name = c.Attributes["fullname"].ToString()
                    });
                }
            }
                      dataGrid1.ItemsSource = collect;
        }
    }
  public class contact
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public string Id { get; set; }

    }
}

No comments:

Post a Comment