Walkthrough: Create and publish custom aspx pages to Dynamics CRM

  • Digg
  • Facebook
  • LinkedIn
  • Twitter

This walkthrough will show you how to create a custom aspx page, use the Dynamics CRM SDK to execute a simple query and to display the results within an iframe inside a crm entity.

Requirements

  • Access to Dynamics CRM (CRM 4, IIS7)
  • Visual Studio 2008 and .NET Framework v3.5
  • CRM SDK 4.0.11 or above

Creating an asp.net web application

To begin, you need to create a project for your code. For this example we will be creating an asp.net web application and executing the WhoAmIRequest request then displaying the results.

  1. Open Visual Studio
  2. Create a new ASP.NET Web Application
  3. Right click on the project then click on Add Reference and add reference to the following Dynamics CRM SDK dlls
    1. Microsoft.Crm.Sdk
    2. Microsoft.Crm.SdkTypeProxy
  4. Switch to the code-behind of Default.aspx page (Default.aspx.cs file)
  5. Copy paste the following code into the Page_Load method
    // connection to dynamics crm
    CrmService sdk = new CrmService
    {
        Url = "http://localhost:5555/mscrmservices/2007/crmservice.asmx",
        Credentials = CredentialCache.DefaultNetworkCredentials,
        CrmAuthenticationTokenValue = new Microsoft.Crm.Sdk.CrmAuthenticationToken
        {
            AuthenticationType = 0,
            OrganizationName = "Example"
        }
    };
     
    // simple request to find out who we are logged in as
    WhoAmIResponse whoAmI = sdk.Execute(new WhoAmIRequest()) as WhoAmIResponse;
    if (whoAmI != null)
    {
        Response.Write(
            string.Format("I am {0} from the {1} business unit.",
                whoAmI.UserId, whoAmI.BusinessUnitId));
    }
  6. Make sure to change the Url and the OrganizationName property values
  7. Build the project (CTRL + Shift + B)

Configuring the crm web server

We need to configure the web server before we deploy our custom application.

  1. Login to the Dynamics CRM server
  2. Find where Dynamics CRM is installed, usually in c:\inetpub\wwwroot\ or c:\Program Files\Microsoft Dynamics CRM\CRMWeb\
  3. Locate the ISV folder, then create a new directory with your company name. For example \ISV\XrmLinq\
  4. Go inside the new directory then create another directory for your application. For example \ISV\XrmLinq\SampleApp
  5. Open Internet Information Services (IIS) Manager from the Administration Tools
  6. Right click on Application Pools -> Add Application Pool -> Select Classic from the Managed pipeline mode optionIt’s good practice to isolate applications
  7. Expand your server -> Sites -> Microsoft Dynamics CRM -> ISV -> <CompanyName>Dynamics CRM IIS configuration for custom asp.net applications
  8. Right click on the SampleApp folder -> Convert to Application -> Select the newly created application pool

The crm server is now ready for us to deploy the sample application.

Deploying the sample asp.net application

Before we deploy our application we need to make some changes to our web.config file. We need to remove the crm specific httpmodules and enable viewstate/sessionstate.

  1. Open the web.config file
  2. Inside the system.web element we need to specify the authentication mode and impersonation
    <authentication mode="Windows"/>
    <identity impersonate="true"/>
  3. Next we need to make sure viewstate and sessionstate is enabled, this is of course optional, if your application uses viewstate or session state you’ll need to enable them, otherwise skip this step. The reason we need to explicitly enable this is because in the crm web.config file these options are turned off.
    <pages enableSessionState="true" enableViewState="true" enableEventValidation="false"
           enableViewStateMac="true" validateRequest="false">
  4. Next we need to clear Dynamics CRM httpmodules to avoid our aspx files being processed through them. Locate the httpModules element and replace it with the following
    <httpModules>
      <clear />
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions,
                                     Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </httpModules>

    Click here to download a sample web.config file

  5. To test open Internet Explorer and browse to the page. For example http://localhost:5555/isv/xrmlinq/sampleapp/default.aspxYou will see your current user guid and business unit guid

Adding the page as an iframe into Dynamics CRM

Now that we’ve tested and verified the sample app works we can configure an iframe. We will add this page to the account entity.

  1. Open Dynamics CRM
  2. Go into Settings -> Customization -> Customize Entities -> Double click on Account to open the account entity
  3. Click on Forms and Views on the left hand side
  4. Double click on Form to configure the account form
  5. Click on Add a Tab
  6. After adding the tab, click on Add a Section
  7. After adding the section, click on Add an IFRAME
  8. Specify a Name for this iframe and specify the Url, the url will be a relative url, which means it will look like this: /isv/xrmlinq/sampleapp/default.aspx
  9. Click on OK
  10. Save and Close then Publish the Account entity
  11. Open up any Account in Dynamics CRM, click on the new tab, you will see the same result you saw in step #4 above

The Result

Custom asp.net page added as an iframe to an entity

18 Feedbacks

  • By Gary, January 21, 2010 @ 2:33 am

    I like your article but I never see where you explain how to deply the web application to the new folder created in ISV? My test application has a whole mess of pages and referenced projects. What al;l do I need to copy (or deploy) to the folder to make it work? Just what is in the bin folder? Thanks!

  • By XrmLinq, January 23, 2010 @ 5:40 pm

    Hi Gary

    Once you have completed all the steps under the “Deploying the sample asp.net application” section, simply copy your application into the new folder. eg: \ISV\XrmLinq\SampleApp\

  • By Gary, January 24, 2010 @ 12:02 am

    Great thanks! I did that and when I try to launch one of the aspx pages from IIS by clicking browse, I get a 401 Unauthorized error even though all my pages tested out in VS 2008. I have been reading dozens of posts about the 401 error with a custom web page in CRM and keep making the suggested changes but nothing seems to work.

    Do I need to restart IIS or domething like that? I am using Windows authentication. Thanka again!

  • By XrmLinq, January 24, 2010 @ 9:50 am

    Hi again
    Step #5 of “Creating an asp.net web application” and step #1-4 of “Deploying the sample asp.net application” is pretty much what controls the 401 error.

    I can get one of my team to look at this for you, email support@xrmlinq.com and I will have one of them assist you remotely.

  • By Gary, January 24, 2010 @ 9:51 pm

    That would be awsome! Does the fact that it does not error with 401 when ran from my machine in a local website help?

    I noticed that in your example, you are referencing the SDK but in my code, I am referencing the web service. Could that be the problem?

    Also, in my website, I reference a project that contains CRM helper functions. It is from a class in my helper project that I do all my connecting and wonder if that isn’t the problem? For instance in my helper class, I have …

    private CrmService crmSvc;
    public CrmService ConnectCrmService()
    {
    CrmSvc.CrmAuthenticationToken token = new CrmSvc.CrmAuthenticationToken();
    token.OrganizationName = ConfigurationSettings.AppSettings["crmOrg"];
    token.AuthenticationType = 0; //AD

    //Create the Service
    crmSvc = new CrmService();
    crmSvc.Credentials = System.Net.CredentialCache.DefaultCredentials;
    crmSvc.CrmAuthenticationTokenValue = token;
    crmSvc.Url = ConfigurationSettings.AppSettings["crmServerUrl"];
    return crmSvc;
    }

    Then whenever I need to connect an aspx page to CRM, I just …

    CrmHelper crmHelp = new CrmHelper();
    CrmService crmSvc = crmHelp.ConnectCrmService();

    Would that not work when ran from the CRM website but run from my local computer? Thanks!

  • By XrmLinq, January 25, 2010 @ 12:06 am

    Depends, if you are using IFD authentication then it’s a whole different method, but if you’re not then try “Credentials = CredentialCache.DefaultNetworkCredentials” instead of DefaultCredentials when depyloed to the server.

  • By Gary, January 25, 2010 @ 2:47 pm

    I am not using IFD. We are on premise with AD. I don’t understand your suggestion of “try “Credentials = CredentialCache.DefaultNetworkCredentials” instead of DefaultCredentials”

    What are you suggesting this line should read?
    crmSvc.Credentials = System.Net.CredentialCache.DefaultCredentials

    Thanks!

  • By Gary, January 29, 2010 @ 3:38 am

    Well I am screwed. I have tried everything I can find and nothing makes any difference. I thought the hard part would be making the website talk to the CRM Webservice but that part was easy. My website works perfectly when ran from a local website on my laptop but dies horribly with a 401 error when ran from the ISV folder inside the CRM website. Seems like if there was going to be a permission error, it would happen from my local machine, not from the CRM server.

    I am totally stuck. Why doesn’t this work?

  • By Tim, January 29, 2010 @ 4:25 pm

    Gary, check the permissions on the folder you created in IIS. As a test, turn on asp.net anonymous access and see if the 401 goes away. This will at least help you narrow down where this is coming from.

  • By Gary, February 16, 2010 @ 6:28 pm

    Ok, I checked the permissions on the folder I created in IIS. What am I looking for? I see Administrators, Creater Owner, System, and Users. BTW this is IIS 6.0

    How/where do I turn on asp.net anonymous access? The annonymous access is already enabled under Directory Security.

    Thanks,

  • By crm40, March 5, 2010 @ 1:01 pm

    i would like to deploy a web page under isv that should work for ifd deployment.
    i used crmSvc.Credentials = System.Net.CredentialCache.DefaultCredentials
    but its raising 401 : Unauthorised error

  • By XrmLinq, March 8, 2010 @ 4:14 am

    Hi

    For IFD authentication you need to use /spla/crmdiscoveryservice.asmx instead of the crmservice.asmx. Download a copy of our trial and use Connection.Create(…) method which does everything for you. Take a look at http://www.xrmlinq.com/getting-started-with-xrmlinq/ for the url to use.

  • By Rahul Lohar, March 25, 2010 @ 5:44 am

    Hello,
    I am myself trying to add a custom page in appointment entity,
    in which i want to send emails from my custom page to multiple users.
    can i build on the above lines that you have written.
    How can i make the custom page to pop out rather than opening it in a tab/iframe?
    Can you suggest please.

    Thanks.

  • By Chris, April 15, 2010 @ 4:45 pm

    Great post, and very well done walk-through, however I have one correction for you. If you plan on using Session you also have to add the following line to the section:

    Then, beneath the section add the following:

    Otherwise you will get errors on the pages which are trying to use Session.

  • By JK, April 21, 2010 @ 8:15 am

    Hi

    Very great post, thanks!! I have a question for you. Since I can obtain the account id that I selected in the aspx form? I need it because i have to make some operations with it.

    Thank you very much!

  • By Diego, April 24, 2010 @ 8:44 am

    Hello,

    I did your sample deployment and work fine if i try to access directly. When i try to use a iframe ou isv button i have ask user/password again. I tried localhost and IFD.

    Chris, you said something about session, but i can’t see what you said “Then, beneath the section add the following:” and .. ?

    Thank you

  • By TC Tham, May 25, 2010 @ 10:07 am

    Hope this is useful for you.

    I encountered error when trying to do this example.

    The error message was like “could not load file or assembly ‘microsoft.crm.sdk’ or one of its dependencies”.

    After searching very long for it, I realised that the dll that I downloaded from Microsoft Crm Sdk was for 32-bit environment. Hence the error.

    To fix this, can refer to http://allfaq.org/forums/t/136204.aspx

    To summarise it, just download the dll from http://www.dynamicsexchange.com/download/64%20Bit%20CRM%20SDK%20DLL.zip

    Replace the dll and it should work fine.

  • By Domenic, July 29, 2010 @ 2:53 am

    Gary,
    Did you ever find a solution for this. I have the same problem and have not been able to get this to work for over a month. Thanks,
    Domenic

Other Links to this Post

RSS feed for feedback on this post. TrackBack URI

Leave your feedback

© XrmLinq 2009.