, , ,

Hello Everyone,

Today I’m going to discuss about accessing CRM 2011 from an external Website.

Lets take a scenario; when a webpage is hit or accessed along with the Querystring then a BusinessUnit should be created automatically.

To start with, we’ll create a Website first.

1> Open Visual Studio 2010-> File -> New -> WebSite
2> Under Installed Templates -> Visual C# -> click ASP .NET Empty WebSite
3> Properly name your project and choose a better destination folder. (I would recommend to choose the Drives other than default C:/ as the Documents Section have lot of Security related different access credentials for different users.)
4> Right click on Website and choose Add New Item
5> Under Installed Templates -> Visual C# -> click WebForm
6> Expand the Webform and click on *.aspx.cs. (We’ll write our code here)


7>Add the CRM assembly references(Microsoft.Xrm.Sdk and Microsoft.crm.sdk.proxy)


8>Add the following directives to your codes.


9> Now under the Page_Load method write your code to access CRM using IOrganization Service.
The Final code will be:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Client;
using System.Net;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        //string a= Request.QueryString[""].ToString();

        //Authenticate using credentials of the logged in user;       
        ClientCredentials Credentials = new ClientCredentials();

        Credentials.Windows.ClientCredential = new System.Net.NetworkCredential("Administrator","password") ;

        //This URL needs to be updated to match the servername and Organization for the environment.

        Uri OrganizationUri = new Uri("http://<server-name>/<Organization Name>/XRMServices/2011/Organization.svc");

        Uri HomeRealmUri = null;

        //OrganizationServiceProxy serviceProxy;       

        using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))

            IOrganizationService service = (IOrganizationService)serviceProxy;

            Guid buid = new Guid("H693W265-D22Q-E787-B1KG-S46F13637529"); // guid for Parent-BU

            Entity business = new Entity("businessunit");
            business.Attributes["name"] = Request.QueryString["PN"].ToString();
            business.Attributes["parentbusinessunitid"] = new EntityReference("businessunit", buid);
            Guid newbusiness = service.Create(business);




10> Build the Solution.
11> Open IIS to add a Website


12> Once finished adding the website, make sure that the Application pool to which the website was added is .NET Framework Version 4.0


13> By default its 2.0 so if that is the case then go to the Application pool and change the specific to 4.0.


14> Now browse Your website along with Querystring value like /localhost:8086/?PN=test

15> You are lucky if you see no errors and once the blank form is loaded, it means the page_load Method has successfully been executed.
16> Go to your organization in CRM and check for the new BusinessUnit with name test.

Hope this would make somebody task much easier.
Thanks !!!