Wednesday, June 20, 2012

Insert Account Record in Account Entity in MS CRM 2011


Introduction:

      Here I am going to show how to insert account into CRM 2011.
      First you have to Add the appropriate Reference Dll for CRM.Add Reference microsoft.crm.sdk.proxy,microsoft.xrm.sdk,System.Servicemodel,System.EnterpriceService,System.Web.Services etc

       Here I am insering the account in CRM.I am insering the name,parent account and Email Address to CRM.As we know that Parent account is the Lookup value and to set the Look up value we need top find the Id of it and then need to insert it.so to set the Parent Account I need to Find the accountid of Account and then have to set.so I have Created on Method here that will Find the account id of the parent account.Here i am just inserting three value name,parent account and Email Address.but you can insert more if you want just need to set it.

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk.Messages;
using System.Net;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Data;
using System.Collections;
using System.ServiceModel.Description;
using System.ServiceModel;

 private static IOrganizationService _service;
        private static OrganizationServiceProxy _serviceProxy;
        private static OrganizationServiceContext orgContext;


        static public void Main(string[] args)
        {
            SampleTest p = new SampleTest();
            p.InsertAccount();
            Console.WriteLine("Inserted");
            Console.ReadKey();
        }

        private void InsertAccount()
        {
            try
            {
                string file = "insertlead.txt";
                GetCRMService();
                Entity entity = new Entity("account");
                entity["name"] = "Manish";
                entity["parentaccountid"] = new EntityReference("account", GetParentaccountid("dinesh"));//GetParentaccountid("Kartik"));
                entity["emailaddress1"] = "manish@gmail.com";
                _service.Create(entity);
                File.AppendAllLines(file, new string[] { entity["name"].ToString() });
            }
            catch (FaultException ex)
            {
                File.AppendAllLines("error.txt", new string[] { (ex.InnerException == null ? ex.Message.ToString() : ex.InnerException.ToString()) });
            }
        }

        private Guid GetParentaccountid(string accountname)
        {
            Guid accountid = Guid.Empty;
            var querry = (from a in orgContext.CreateQuery("account") where (string)a["name"] == accountname select a["accountid"]).SingleOrDefault();
            accountid = new Guid(querry.ToString());
            //foreach (var k in querry)
            //{
            //    accountid = new Guid(querry.ToString());
            //}
            return accountid;
        }

        private static void GetCRMService()
        {
            try
            {

                string file = "connection.txt";
                Uri organizationuri = new Uri("https://kartik.crm5.dynamics.com/XRMServices/2011/Organization.svc");
                Uri homeRealmUri = null;

            
                ClientCredentials credential = new ClientCredentials();
                credential.Windows.ClientCredential = new NetworkCredential("username", "password", "Kartik");
          
            
                _serviceProxy = new OrganizationServiceProxy(organizationuri, homeRealmUri, credential, null);
                _serviceProxy.EnableProxyTypes();

                _service = (IOrganizationService)_serviceProxy;
                orgContext = new OrganizationServiceContext(_serviceProxy);
                File.AppendAllLines(file, new string[] { "connection tested" });
            }
            catch (FaultException ex)
            {
                File.AppendAllLines("error.txt", new string[] { (ex.InnerException == null ? ex.Message.ToString() : ex.InnerException.ToString()) });
            }
        }
    }
}

Download the Sample Code from Here
 Download

No comments:

Post a Comment