Sunday, July 17, 2011

Using CRM 2011 Service to Create Record from Code C# - IOrganizationService

// sample to test the method   
private void CreateAccount(string AccountName)
    {
        IOrganizationService _service = GetCRMService("http://<ServerName>:<PortNumber>", "<UserName>", "<Password>", "<Domain>");
        try
        {
            Entity account = new Entity("account");
            account["name"] = AccountName;
            _service.Create(account);
        }
        catch (Exception ex)
        {
            throw;
        }
    }

// GetCRMService   
    public static IOrganizationService GetCRMService(string ServerURL, string UserName, string Password, string DomainName)
    {
        ClientCredentials credentials = new ClientCredentials();
        credentials.Windows.ClientCredential = new System.Net.NetworkCredential(UserName, Password, DomainName);
        Uri organizationUri = new Uri(ServerURL + "/<OrgName>/XRMServices/2011/Organization.svc");
        Uri homeRealmUri = null;
        OrganizationServiceProxy orgService = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
        IOrganizationService _service = (IOrganizationService)orgService;
        return _service;
    }

1 comment:

  1. How to Assign Custom GUid to Entity when creating entity in MS CRM 2011 using service

    ReplyDelete