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.

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;
}
0 Comments

Shrtn ur cd

I believe there are always places in which you can improve efficiency. Some things stand out more than other, and usually the easy things get worked on last. I wanted to share with you a little trick to help out with managing data from the V (view) to the C (Controller).

My primary pattern for architecture is using MVC but not the ASP.Net MVC. Our company also does iPhone apps so we get exposed to other coding languages. The great thing about this is that you get to pick and choose what you like about each language. The MVC pattern that I follow is derived from that on the iPhone and this little trick comes in very handy in both objective-c and .net.

The point in time in which you are moving data from the user interface (View) into your Controller class a significant amount of time savings that can be gained. By implementing some nice extension methods (categories in the iPhone world) that can reduce the amount of code you put out.

Let me show you:

Extend the TextBox object to include one these so you don’t have to look for nulls

public static Decimal TextAsDecimal(this TextBox txt)
{
	decimal value = 0;
 
	if (!string.IsNullOrEmpty(txt.Text.Trim()))
	{ decimal.TryParse(txt.Text.Trim(), out value); }
 
	return value;
}

public static Decimal? TextAsDecimalNull(this TextBox txt)
{
	decimal value = 0;
	decimal? emptyValue = null;
 
	if (!string.IsNullOrEmpty(txt.Text.Trim()))
	{
	    decimal.TryParse(txt.Text.Trim(), out value);
 
	    return value;
	}
	else
	{ return emptyValue; }
 
}

Implement this so you don’t have to worry about setting the value from your storage to your TextBox

public static void TextSetFromDecimalNumber(this TextBox txt, Decimal? number)
{
	if (number.HasValue)
	{ txt.Text = number.Value.ToString(PageBase.DecimalFormatString); }
}

This is just a few examples that I usually implement, but I also do this for more data type handling and for more controls.  I usually have an extensions.cs file in each one of my projects and carrying them with me to whatever project I do. That way I hit the ground running. So take some of these concepts and implement your own extensions and start reducing the amount of code that you write. Let me know if you have any questions and I try and help you out.

Part 2

0 Comments

Technology, Money & Life

Einstein“The significant problems we face cannot be solved at the same level of thinking we were at when we created them.”

Albert Einstein





This quote is very empowering and enlightening. You can’t read a blog, watch the news, or read a paper (if your over 50 ;) ) without reading something that talks about the economy and being upside down. When I read this quote it said to me that there are things you can do about it and it is an exciting time. Things are getting mixed up within technology because of how the economy is. Apple, Google and Microsoft are pushing each other to become better. Big technology companies like the big 3 were becoming fat and happy on the economy being so good that they didn’t have to do anything to make money.


Now the big 3 are having to get back to what put them at the top by pushing each other to innovate. There are a ton of cool really innovative products that can make your company run better for a fraction of what you used to spend.


Lemonade StandNow 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.


Right now is not about cutting people but refocusing. Take a negative and make it a positive. No matter what the industry your are in the lines are getting blurred. Things are becoming more viral and flexible within every week.


Take a look at a cool piece of technology like the iPad. The iPad is changing computing how we know it today. The iPad is based on storing your data in the cloud and doing simple focused tasks on the device. The iPhone is a mini portable computer in your pocket. Manage your business with these devices from wherever you with a few flicks of your finger. Use Google docs with all these devices and then when you get to a computer you can continue to work on the same spreadsheets.


Mix up your business and become more social, and try to have more FUN while doing so. List your business on Facebook and create some thought provoking entries of your own. Business is no longer about a lot of hours and a few smiles. This a great time to take those things and tell them to kiss your gritz and start having fun. If you notice someone around you stuck in a slump by them a nice treat and give them a smile.


Get out there and provoke change in your life and in your business. Think back on what your thoughts were a year ago and kick the tires to see what happens. Find more time to smile and help someone around find their smile also.

Smile Baby

0 Comments