Click here to Skip to main content
Click here to Skip to main content

Access a Web page in Dynamics CRM with JavaScript Code

, 21 Mar 2008
Rate this:
Please Sign up or sign in to vote.
In this article, I will show you how to access a Web page with JavaScript code in Microsoft Dynamics CRM. You can process CRM entities or different actions in that page

Introduction

In this article, I will show you how to access a Web page with JavaScript code in Microsoft Dynamics CRM. You can process CRM entities or perform different actions on that page.

In sample JavaScript code; we will take Guid of a Product and we will pass this Guid to /QuoteCalcs/Calcs.aspx page. This page will process something and will return a result in <baris> XML tags. If the result is null or false JavaScript code will show an error alert to the user.

If you want to cancel the save process of CRM after this error, you must add the following code block after the error code:

event.returnValue = false;

If there is a value in the object, pass an area for showing to the user.
You will find the C# code after JavaScript code.

Using the JavaScript Code

This code will show you how to access a Web page with JavaScript (I will continue the article in the JavaScript code):

var oProduct = document.crmForm.all.productid; 
// we passed the Lookup object for access to Guid
var aProduct = new Array(); 
//The lookup is an array on CRM Form. So we must access 
//this objects of array with an array object.
aProduct = oProduct .DataValue; 
//Yeah, we found the Guid of Product
var sProductID = aProduct [0].id;sProductID = encodeURIComponent(sProductID);
if (sProductID!=null)
{
    //We create an Microsoft.XMLDOM object
    var oXmlDoc = new ActiveXObject('Microsoft.XMLDOM');
    oXmlDoc.async = false; // we don't want asenkron code process
    var path = '/QuoteCalcs/Calcs.aspx?productid=' + sProductID ;
    //we passed the path of our web page to XMLDOM object
    oXmlDoc.load(path);
    //we look for <baris> xml tag - Baris is my name:-) 
    var oNode = oXmlDoc.selectSingleNode('baris'); 
    if (oNode != null && oNode.text == 'false')
    {
        alert('an error');
    }
    if (oNode != null && oNode.text != 'false')
    { 
        //we set value to price area in CRM Form
        crmForm.all.price.value = oNode.text;
    }
}

Using the C# Script Code

The real process will run on this ASPX page. We takes the parameters that are passed from our JavaScript code. We are looking for parameters that are empty. Later, we access the CRM Web Services with default credentials. If you aren't in the same domain with Microsoft Dynamics CRM you didn't access CRM Web Service with default credentials, so you must access Web Services with a username and password. We will send a query like...

select * from filteredproduct where productid=ProductId

... while using Web services. The response from Web services is a BussinessEntityCollection that is the basis of all entities in Microsoft Dynamics CRM. We convert the BussinessEntityCollection class to a product class and we find the value that we want and we return this value in the <baris> XML tag:

<%@ Page Language="'c#'%">
<%@ Import Namespace='CrmSdk' %>
<script runat="'server'">
protected override void Render(HtmlTextWriter writer) 
{
    Response.Clear();
    Response.ContentType = 'text/xml';
    string ProductID = Request.QueryString['ProductID'];
    string TYPE = Request.QueryString['Type'];
    if (ProductID != null &&
    ProductID !='null')
    {
        CrmService service = new CrmService();
        service.Credentials = System.Net.CredentialCache.DefaultCredentials;
        QueryByAttribute attributeQuery = new QueryByAttribute(); 
        attributeQuery.ColumnSet = new AllColumns();
        attributeQuery.Attributes = new string [] {'productid'};
        attributeQuery.Values = new string [] {ProductID};
        attributeQuery.EntityName = EntityName.product.ToString();
        RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();
        retrieve.Query = attributeQuery;
        RetrieveMultipleResponse retrieved = 
		(RetrieveMultipleResponse)service.Execute(retrieve);
        if (retrieved.BusinessEntityCollection.BusinessEntities.Length > 0)
        {
            product prd = (product)
			retrieved.BusinessEntityCollection.BusinessEntities[0];
            if (prd.price!=null)
            {
                Response.Write('<baris>'+prd.price.Value.ToString()+'</baris>');
            }
            else
            {
                Response.Write('<baris>false</baris>');
            } 
        }
        else
        {
            Response.Write('<baris>false</baris>');
        }
    }
} 
</script>

In all of this process, we have a JavaScript and a C# script code. In C# script code we import the CrmSdk DLL to the page with the import method. You can make your CrmSdk (Microsoft.Crm.Sdk.Wsdl.dll) DLL. Below, you can find how to make your CrmSdk DLL. In all of those processes, we must put our code to a server and we must create a virtual directory in CRM Web Site in IIS.

Creating a DLL for the Microsoft CRM Web Service

When developing your solution, you first need to generate a WSDL that will provide you with all the classes and methods in Microsoft CRM 3.0. You should always start with a clean installation of Microsoft CRM 3.0, one that has had no customizations made. This way your code will not rely on information not found on a customer installation.

The steps below demonstrate how to generate this reference file for the SDK. You can use the same procedure to generate a reference file for the metadata Web service.

  1. Click Start, point to All Programs, point to Microsoft Visual Studio .NET 2003, point to Visual Studio .NET Tools, and then click Visual Studio .NET 2003 Command Prompt.

  2. At the command prompt, create the reference file, Microsoft.Crm.Sdk.Wsdl.cs, by typing the following command, using the URL of your server running Microsoft CRM:

    wsdl.exe /out: Microsoft.Crm.Sdk.Wsdl.cs 
        /namespace:CrmSdk http://<yourserver>/mscrmservices/2006/crmservice.asmx
  3. Generate a WSDL DLL that will be packaged with your solution using the reference created in step 2 using this command:

    csc /t:library Microsoft.Crm.Sdk.Wsdl.cs

This DLL can now be packaged with your add-on.

Baris KANLICA
Software Specialist
brsk@e-kolay.net
www.cub-e.net

forum.cub-e.net

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

About the Author

Baris KANLICA
Software Developer (Senior) Omerd Business Solutions
Turkey Turkey
Since 2005, I have experience in consulting, software development and project management for Microsoft Dynamics CRM 1.2, 3.0, 4.0 and 2011 versions.
Since 2008, I have experience in the software department manager.
I got the MVP (Most Valuable Professional) award from Microsoft since 2009. This award is given to exceptional technical community leaders who actively share their high quality, real world expertise with others. You can find more information about this award on this address : http://mvp.microsoft.com/en-us/mvp/Baris%20Kanlica-4025099
Digital Life Coach title is an award given by Bilkom that general distributor of Apple, Adobe, Graphisoft brands in Turkey. This award is given to people who are the highest level of knowledge on a certain proven leader and pioneer in the industry. My expertise as a Digital Life Coach, explain, teach and give necessary advice to the end user and companies how can be used effectively Microsoft products on Apple's hardware and operating systems (MacOS X and IOS)
http://www.cub-e.net is my technical website. My website is site has been approved as a Microsoft Dynamics Related Community. As such they have added link to my site on their Related Communities page. You can look at in http://community.dynamics.com/p/relatedcommunities.aspx#MsDynCRM
I given lectures about CRM topics at many universities and seminars in Turkey.
I'm an editor of the following Web Sites, www.yazgelistir.com, www.yazilimgunlugu.com and www.nedirtv.com
 
Specialties:
 
Develop Architecture in .NET (extend CRM and connect CRM with other systems)
Cloud Services (Windows Azure) Implementation and Support
Sales, Marketing and Service management
Loyalty programs develop and management
CRM consultancy and training
Social Networks for B2B and B2C
 
My Programing Skills:
 
C#, ASP.NET, Windows Forms, Web Services, Windows Services
SQL Server, SQL Reporting Services,
Web-based software development technologies (JavaScript, HTML, CSS, etc.)
Multi-tier architecture, obje

Comments and Discussions

 
GeneralOpenSource CRM & ERP : http://www.codeplex.com/workflowmagic Pinmemberworkf16-Oct-08 4:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 21 Mar 2008
Article Copyright 2007 by Baris KANLICA
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid