Here’s what I have for you guys a simple technique to connect to your CRM office 365 based organizations. I have created a sample to add a new contact. This will help you use the Create function of CRM Service
- Create an empty solution in VS 2010
- Right click, to add new class library project
- Choose the Target Framework as .NET 4.0
- Name the project as “<<your preferred name>>”
- Add the required references to your project
- Add the following code:
Note: The example is based out from MSDN article. For complete details on how to create a helperclass, please refer to
http://msdn.microsoft.com/en-us/library/gg309393.aspx.
Walkthrough
- Create an empty solution in VS 2010
- Right click, to add new console
- Choose the Target Framework as .NET 4.0
- Name the project as “CrmProxy”
- Create a class called
- Add the required references to your project
- Add the following code:
public class Program
{
//Define global variables
private String discoveryServiceAddress = "https://disco.crm5.dynamics.com/XRMServices/2011/Discovery.svc"; //Use this discovery URL
private String organizationUniqueName = "UniqueName"; //Provide the unique name of the organization
private String userName = "user@yourname.onmicrosoft.com"; //Use your office 365 user Id
private String password = "*******"; //Provide your password here
private String domain = "domainName";
static void Main(string[] args)
{
Program programObject = new Program();
programObject.ReturnCRMServiceInstance();
}
/// <summary>
/// This function returns the instance for CRM Service using OrganizationServiceProxy
/// </summary>
/// <returns></returns>
public OrganizationServiceProxy ReturnCRMServiceInstance()
{
String organizationUri = "https://<yourorgName>.api.crm5.dynamics.com/XRMServices/2011/Organization.svc";
IServiceManagement<IOrganizationService> OrganizationServiceManagement = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri(organizationUri));
AuthenticationProviderType OrgAuthType = OrganizationServiceManagement.AuthenticationType;
AuthenticationCredentials authCredentials = GetCredentials(OrgAuthType);
AuthenticationCredentials tokenCredentials = OrganizationServiceManagement.Authenticate(authCredentials);
OrganizationServiceProxy organizationProxy;
SecurityTokenResponse responseToken = tokenCredentials.SecurityTokenResponse;
using (organizationProxy = new OrganizationServiceProxy(OrganizationServiceManagement, responseToken))
{
organizationProxy.EnableProxyTypes();
}
return organizationProxy;
}
/// <summary>
/// Use this function for creating
/// </summary>
/// <param name="lastName"></param>
/// <returns></returns>
public Guid createContact(string lastName)
{
OrganizationServiceProxy organizationProxy = this.ReturnCRMServiceInstance();
Entity contactEntity = new Entity("contact");
contactEntity.Attributes["lastname"] = lastName;
return organizationProxy.Create(contactEntity);
}
//Note: Please use your own authentication mechanism. Refer to this link for more types: http://msdn.microsoft.com/en-us/library/gg309393.aspx
/// <summary>
/// This function retuns the authentication instance for different type of modes.
/// </summary>
/// <param name="endpointType"></param>
/// <returns></returns>
private AuthenticationCredentials GetCredentials(AuthenticationProviderType endpointType)
{
AuthenticationCredentials authCredentials = new AuthenticationCredentials();
switch (endpointType)
{
case AuthenticationProviderType.ActiveDirectory:
authCredentials.ClientCredentials.Windows.ClientCredential =
new System.Net.NetworkCredential(userName, password, domain);
break;
case AuthenticationProviderType.LiveId:
authCredentials.ClientCredentials.UserName.UserName = userName;
authCredentials.ClientCredentials.UserName.Password = password;
authCredentials.SupportingCredentials = new AuthenticationCredentials();
authCredentials.SupportingCredentials.ClientCredentials =
Microsoft.Crm.Services.Utility.DeviceIdManager.LoadOrRegisterDevice();
break;
default: // For Federated and OnlineFederated environments.
authCredentials.ClientCredentials.UserName.UserName = userName;
authCredentials.ClientCredentials.UserName.Password = password;
// For OnlineFederated single-sign on, you could just use current UserPrincipalName instead of passing user name and password.
// authCredentials.UserPrincipalName = UserPrincipal.Current.UserPrincipalName; //Windows/Kerberos
break;
}
return authCredentials;
}
}
- Now, we’re all set.
- Build the complete solution and now you have your assembly ready.
- You can use this in any of your projects.
Happy Integration with Office 365!
Cheers,
Apurv