Quantcast
Channel: Danny Varghese's Blog » Dynamics CRM 2011
Viewing all articles
Browse latest Browse all 5

Sample CRM 2011 Retrieve and Retrieve Multiple

0
0

Thank you to all who have been reading my blog articles and have commented and sent personal messages.  It means a lot to me to know some of my content might be helping.  A common request I get is to post sample code on some basic operations using web services.  I’ll try to do that in the upcoming articles.  For those of you that are seasoned CRM developers, this might be redundant and I apologize!

The method GetCRMService() is a private helper method used to retrieve an instance of the CRM web service. This particular method authenticates both on-premise IFD and non-IFD deployments. Please note, authenticating to a CRM online environment is a different solution. Also note that this particular method does not provide early binding support. I will discuss all of these possible options in a later article.

private IOrganizationService GetCRMService()
{
     OrganizationServiceProxy serviceProxy;
     const string uri = "uri";
     const string userName = "username";
     const string password = "password";
     const string domain = "domain";

     IServiceConfiguration<IOrganizationService> config = ServiceConfigurationFactory.CreateConfiguration<IOrganizationService>(new Uri(uri));

     if (config.AuthenticationType == AuthenticationProviderType.Federation)
     {
          // ADFS (IFD) Authentication
          ClientCredentials clientCred = new ClientCredentials();
          clientCred.UserName.UserName = userName;
          clientCred.UserName.Password = password;
          serviceProxy = new OrganizationServiceProxy(config, clientCred);
     }
     else
     {
          // On-Premise, non-IFD authentication
          ClientCredentials credentials = new ClientCredentials();
          credentials.Windows.ClientCredential = new NetworkCredential(userName, password, domain);
          serviceProxy = new OrganizationServiceProxy(new Uri(uri), null, credentials, null);
     }

     return serviceProxy;
}

An example of retrieve a single record, given the record id is below:

public Entity RetrieveRecord(string entityName, Guid recordId, ColumnSet columns)
{
     return GetCRMService().Retrieve(entityName, recordId, columns);
}

An example of retrieving multiple records is below:

public IEnumerable RetrieveRecords(string entityName, ColumnSet columns)
{
     QueryExpression query = new QueryExpression
     {
          EntityName = entityName,
          ColumnSet = columns,
          Criteria = new FilterExpression
          {
               Conditions =
               {
                    new ConditionExpression
                    {
                         AttributeName = "attributeName",
                         Operator = ConditionOperator.Equal,
                         Values = {"attributeValue"}
                    }
               }
          };
       }

       return GetCRMService().RetrieveMultiple(query).Entities;
}


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images