Friday 6 December 2013

Create a WPF appication and display Account record in grid Ms Crm

1. Create a Grid in WPF application in window.Xaml


  <Grid>
        <Grid Height="287" HorizontalAlignment="Left" Margin="0,12,0,0" Name="grid1" VerticalAlignment="Top" Width="503">
{ create a Label  Create Account } 

            <Label Content="Create Account" Height="28" HorizontalAlignment="Left" Margin="6,33,0,0" Name="label1" VerticalAlignment="Top" Width="109" />
{ Create a Button and on click display account connecting to mscrm online }

            <Button Content="Display Account" Height="23" HorizontalAlignment="Left" Margin="179,34,0,0" Name="button1" VerticalAlignment="Top" Width="266" Click="button1_Click" />
{Create a colom in Grid }
            <DataGrid Height="171" AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="4,109,0,0" Name="account" VerticalAlignment="Top" Width="497"  >
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Path= Name}" Header="Name" />
                    <DataGridTextColumn Binding="{Binding Path= FirstName}" Header="fName" />
                    <DataGridTextColumn Binding="{Binding Path= emailAddress}" Header="lName" />
                    <DataGridTextColumn Binding="{Binding Path= lName}" Header="emailAdrress" />
                    <DataGridTextColumn Binding="{Binding Path= fName}" Header="First Name" />
                    <DataGridTextColumn Binding="{Binding Path= phnNum}" Header="Phone Number" />
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </Grid>


2.) In window.xaml.cs

public partial class MainWindow : Window
    {
        IOrganizationService org;   // add a microsoft.xrm.sdk.dll
        EntityCollection ent = new EntityCollection();
        public MainWindow()
        {
            InitializeComponent();
            org =  GetOrganizationSerivice();
           
        }

        #region GetProxy
        public OrganizationServiceProxy GetProxy()
        {
            string strUrl = string.Empty;
            strUrl = "Organization url mscrm online" ;

            Uri organizationUri = new Uri(strUrl);
            Uri homeRealUri = null;
            ClientCredentials credentials = new ClientCredentials();
            credentials.UserName.UserName = "username" ;
            credentials.UserName.Password = "password";
           // credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
            OrganizationServiceProxy proxyService = new OrganizationServiceProxy(organizationUri, homeRealUri, credentials, null);
            proxyService.EnableProxyTypes();
            return proxyService;
        }

        static IOrganizationService GetOrganizationSerivice()
        {
           
            string strUrl = string.Empty;
            strUrl = "mscrm online";
            ClientCredentials credential = new ClientCredentials();
            credential.UserName.UserName = "user name";
            credential.UserName.Password = "password";
           // credential.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
            OrganizationServiceProxy proxy = new OrganizationServiceProxy(new Uri(strUrl), null, credential, null);
            return proxy as IOrganizationService;
        }


        #endregion

        public class AccountList
        {
create property to bind data in grid .

            public int id{get; set;}
            public string Name { get; set; }
            public string FirstName{ get; set; }
            public string emailAddress { get; set; }
            public string lName { get; set; }
            public string fName { get; set; }
            public string phnNum { get; set; }
        }


        private void button1_Click(object sender, RoutedEventArgs e)
        {
           
        
            QueryExpression query = new QueryExpression();
            query.EntityName = "account";
            ColumnSet columnSet = new ColumnSet();
            columnSet.AllColumns = true;
            //columnSet.Columns.Add("name");
            query.ColumnSet = columnSet;
            query.Distinct = true;
           ent = org.RetrieveMultiple(query);
            List<AccountList> AllParts = new List<AccountList>();
            AccountList rr = new AccountList();
          
            foreach (Entity en in ent.Entities)
            {
                rr = new AccountList { Name = en.Attributes["name"].ToString(), lName = "Apple" };
//using above property to bind data to CRM.
           AllParts.Add(rr);
             }
            account.ItemsSource = AllParts;

            account.Items.Refresh();
          
        }

       
    }