Using Microsoft CRM 4 Services (IFD)
Before you can do anything with Microsoft CRM you must first be authenticated. Here are some prerequisites that you must have in place before you can get started.
- Download the Microsoft CRM 4 SDK
- Have the CrmDiscoveryService.wsdl file from your IFD site
Microsoft CRM 4 uses Claim based (aka token) authentication and it communicates using a request/response format. Now that you know that you have crossed the hardest bridge. Authentication will take place using known credentials like domain\username and password. In the example below we will be creating an Account inside of CRM to show the interaction. Lets get started.
- This code is simply supplying the credentials to our custom method to authenticate and initialize our CRMService.
- Then we new up an instance up an account setting the name field on the account and creating it in CRM.
string username = "username";
string password = "password";
string domain = "domain";
CrmService myCrm = this.IFDConnection("OrgName",
"server.com",
domain,
username,
password);
account newAccount = new account();
newAccount.name = string.Concat(username, " - Testing account service");
myCrm.Create(newAccount);Next we dig into the IFDConnection method
- First we setup some variables and fix the server string
- Then we initialize our CrmDiscoveryService and set the url based on the server name. This is important because you are specifying the authentication type you are working with.
private CrmService IFDConnection(string organization, string server, string domain, string username, string password)
{
// A CrmService reference.
CrmService CrmService = null;
// URL of the Web application.
string WebApplicationUrl = String.Empty;
// GUID of the user's organization.
Guid OrganizationId = Guid.Empty;
//Remove any trailing forward slash from the end of the server URL.
server = server.TrimEnd(new char[] { '/' });
// Initialize an instance of the CrmDiscoveryService Web service proxy.
CrmDiscoveryService disco = new CrmDiscoveryService();
disco.Url = "http://" + server + "/MSCRMServices/2007/SPLA/CrmDiscoveryService.asmx";- Next we are going to make a request using our known credentials
- We then get a response with a possible list of Organizations
- We then enumerate the list looking for the supplied organization
//Retrieve a list of available organizations.
RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
orgRequest.UserId = domain + "\\" + username;
orgRequest.Password = password;
RetrieveOrganizationsResponse orgResponse = (RetrieveOrganizationsResponse)disco.Execute(orgRequest);
//Find the desired organization.
foreach (OrganizationDetail orgdetail in orgResponse.OrganizationDetails)
{
if (orgdetail.OrganizationName.ToLower() == organization.ToLower())
{- Now that we found our organization we request an authentication ticket using our supplied credentials
- Then we receive a response with our authentication ticket
//Retrieve the ticket.
RetrieveCrmTicketRequest ticketRequest = new RetrieveCrmTicketRequest();
ticketRequest.OrganizationName = organization;
ticketRequest.UserId = domain + "\\" + username;
ticketRequest.Password = password;
RetrieveCrmTicketResponse ticketResponse = (RetrieveCrmTicketResponse)disco.Execute(ticketRequest);- Now we take our authentication ticket and create an authentication token. We specifiy the AuthenticationType = 2 which mean IFD authentication.
- Now that our token is created we can start working with CRM.
- So we new up a CRMService and assign our token and the organization’s url and other details.
- Then we return the created CrmService
//Create the CrmService Web service proxy.
CrmAuthenticationToken sdktoken = new CrmAuthenticationToken();
sdktoken.AuthenticationType = 2;
sdktoken.OrganizationName = organization;
sdktoken.CrmTicket = ticketResponse.CrmTicket;
CrmService = new CrmService();
CrmService.CrmAuthenticationTokenValue = sdktoken;
CrmService.Url = orgdetail.CrmServiceUrl;
WebApplicationUrl = orgdetail.WebApplicationUrl;
OrganizationId = orgdetail.OrganizationId;
break;
}
}
return CrmService;Complete code Here
protected void Page_Load(object sender, EventArgs e)
{
string username = "username";
string password = "password";
string domain = "domain";
CrmService myCrm = this.IFDConnection("OrgName",
"server.com",
domain,
username,
password);
account newAccount = new account();
newAccount.name = string.Concat(username, " - Testing account service");
myCrm.Create(newAccount);
}
private CrmService IFDConnection(string organization, string server, string domain, string username, string password)
{
// A CrmService reference.
CrmService CrmService = null;
// URL of the Web application.
string WebApplicationUrl = String.Empty;
// GUID of the user's organization.
Guid OrganizationId = Guid.Empty;
//Remove any trailing forward slash from the end of the server URL.
server = server.TrimEnd(new char[] { '/' });
// Initialize an instance of the CrmDiscoveryService Web service proxy.
CrmDiscoveryService disco = new CrmDiscoveryService();
disco.Url = "http://" + server + "/MSCRMServices/2007/SPLA/CrmDiscoveryService.asmx";
//Retrieve a list of available organizations.
RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
orgRequest.UserId = domain + "\\" + username;
orgRequest.Password = password;
RetrieveOrganizationsResponse orgResponse = (RetrieveOrganizationsResponse)disco.Execute(orgRequest);
//Find the desired organization.
foreach (OrganizationDetail orgdetail in orgResponse.OrganizationDetails)
{
if (orgdetail.OrganizationName.ToLower() == organization.ToLower())
{
//Retrieve the ticket.
RetrieveCrmTicketRequest ticketRequest = new RetrieveCrmTicketRequest();
ticketRequest.OrganizationName = organization;
ticketRequest.UserId = domain + "\\" + username;
ticketRequest.Password = password;
RetrieveCrmTicketResponse ticketResponse = (RetrieveCrmTicketResponse)disco.Execute(ticketRequest);
//Create the CrmService Web service proxy.
CrmAuthenticationToken sdktoken = new CrmAuthenticationToken();
sdktoken.AuthenticationType = 2;
sdktoken.OrganizationName = organization;
sdktoken.CrmTicket = ticketResponse.CrmTicket;
CrmService = new CrmService();
CrmService.CrmAuthenticationTokenValue = sdktoken;
CrmService.Url = orgdetail.CrmServiceUrl;
WebApplicationUrl = orgdetail.WebApplicationUrl;
OrganizationId = orgdetail.OrganizationId;
break;
}
}
return CrmService;
}



“The significant problems we face cannot be solved at the same level of thinking we were at when we created them.”
Now is the time to look at what you’re doing within your organization see what you can streamline, optimize and simplify. What we used to know as far as how technology and software used to work is no longer the case. Take a look back at the quote for this entry and note that what you thought a year ago to be a fact may need to be revisited. Pick up a book, read a new blog, or get a new General Ledger statement and demand change.
Facebook
RSS
Recent Comments