Friday, July 22, 2011

Extended CRM


Add the Reference to our project in Visual Studio from SDK kit

Microsoft.Crm.SdkTypeProxy;
Microsoft.Crm.Sdk;

Add the follwing code means Namespaces

using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Sdk;


Creating a crmservice using the c# code

class CrmServiceClass
    {
        public CrmService createcrmservice()
        {
            // Set up the CRM Service.
            CrmAuthenticationToken token = new CrmAuthenticationToken();
            token.AuthenticationType = 0;
            token.OrganizationName = "Organization Name";

            CrmService service = new CrmService();
            service.Url = "http://localhost:5555/mscrmservices/2007/crmservice.asmx";
            service.CrmAuthenticationTokenValue = token;
            service.Credentials = new System.Net.NetworkCredential("user", "password", "Domain Name");
             //The Commented line is used to get the default login details means it will take the user currently logined
            // service.Credentials = System.Net.CredentialCache.DefaultCredentials;
            return service;

        }
    }


Method used to Create a Entity in CRM 


 class createEntity
    {
        public void createentity()
        {
            CrmServiceClass obj=new CrmServiceClass();
            CrmService  service = (CrmService)obj.createcrmservice();
            // Create an account entity and assign data to some attributes.
            account newAccount = new account();
            newAccount.name = "Athul";
            newAccount.accountnumber = "123456";
            newAccount.address1_postalcode = "98052";
            newAccount.address1_city = "Redmond";

            contact newcontact = new contact();
            newcontact.firstname = "athul";
            newcontact.fullname = "Athul Mt";
            newcontact.emailaddress1 = "athulmt@gmail.com";
            // Call the Create method to create an account.
            Guid accountId = service.Create(newAccount);
            Guid conatctId = service.Create(newcontact);
        }
    }


Method to update a Entity


class updateEntity
    {
        public void updateentity()
        {
            CrmServiceClass obj = new CrmServiceClass();
            CrmService service = (CrmService)obj.createcrmservice();
            DynamicEntity dynAccount = new DynamicEntity("account");
            dynAccount.Properties.Add(CrmTypes.CreateKeyProperty("accountid", new Key(new Guid("10AA7D61-25B4-E011-B20B-000C29B9B349")))); //give the record guid within quotes
            dynAccount.Properties.Add(CrmTypes.CreateStringProperty("name", "Test Account 2"));

            TargetUpdateDynamic target = new TargetUpdateDynamic();
            target.Entity = dynAccount;

            UpdateRequest request = new UpdateRequest();
            request.Target = target;
            UpdateResponse response = (UpdateResponse)service.Execute(request);
            Console.WriteLine("record updated");
            Console.Read();
        }
    }

No comments:

Post a Comment