Integrating Microsoft CRM with Microsoft Office 2003 InfoPath

 

Microsoft Corporation

February 2004

Applies to:
    Microsoft® Customer Relationship Management (CRM) version 1.2

Requires:
    Microsoft CRM version 1.2
    Microsoft Visual Studio® .NET
    Microsoft Office InfoPath™ 2003

Summary: You can use a Web service to integrate Microsoft Business Solutions Customer Relationship Management (CRM) with Office InfoPath 2003. This article demonstrates how to use a Web service to retrieve a summary of Microsoft CRM account information. This data is then sent to a form created in InfoPath 2003. The Web service shown in this article was developed with C# using Visual Studio .NET 2003. However, any .NET language can be used for this purpose. (8 printed pages)

Contents

Create the Web Service
Create the InfoPath Form
Test the InfoPath Form
Conclusion

Create the Web Service

To create a Web service to access the data in Microsoft® Customer Relationship Management (CRM), you will need to reference the assemblies for Microsoft CRM, Microsoft.Crm.Platform.Proxy.dll, and Microsoft.Crm.Platform.Types.dll. These are located on the Microsoft CRM server in the folder <root>\inetpub\wwwroot\bin.

The following code is used in this example, writing in C# using Microsoft Visual Studio® .NET.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Xml;
using Microsoft.Crm.Platform.Proxy;

namespace FetchAllAccounts
{
   public class FetchAllAccounts : System.Web.Services.WebService
   {
      // You must change this to the URL for your Microsoft CRM server.
      private const string _strCRMURL = "https://MyServerName/mscrmservices/";

      /// <summary>
      /// Retrieve a summary of all accounts in the database.
      /// </summary>
      [WebMethod]
      public XmlNode Fetch()
      {
         // Create BizUser proxy object.
         BizUser objBizUser = new BizUser();
         objBizUser.Credentials
              = System.Net.CredentialCache.DefaultCredentials;
         objBizUser.Url = _strCRMURL + "BizUser.srf";

         // Create CRMQuery proxy object.
         CRMQuery objQuery = new CRMQuery();
         objQuery.Credentials = objBizUser.Credentials;
         objQuery.Url = _strCRMURL + "CRMQuery.srf";

         // Create the XML to fetch a summary of all accounts.
         string strAllAccountsFetchXML = 
            @"<fetch>
               <entity name='Account'>
                  <attribute name='accountid'/>
                  <attribute name='name'/>
                  <attribute name='websiteurl'/>
               </entity>
            </fetch>";


         // Execute the query and return the result set. 
         // If an exception occurs, return the error message instead.
         XmlDocument xmlDoc = new XmlDocument();
         try
         {
            xmlDoc.LoadXml(objQuery.ExecuteQuery(objBizUser.WhoAmI(),
                           strAllAccountsFetchXML));
            return xmlDoc;   
         }
         catch (System.Web.Services.Protocols.SoapException err)
         {
            xmlDoc.LoadXml("<Error/>");
            xmlDoc.DocumentElement.InnerText = err.ToString();
            return xmlDoc.DocumentElement;
         }
      }

      #region Component Designer generated code

      public FetchAllAccounts()
      {
         //CODEGEN: This call is required by the ASP.NET Web Services Designer
         InitializeComponent();
      }

      
      //Required by the Web Services Designer 
      private IContainer components = null;
            
      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeComponent()
      {
      }

      /// <summary>
      /// Clean up any resources being used.
      /// </summary>
      protected override void Dispose( bool disposing )
      {
         if(disposing && components != null)
         {
            components.Dispose();
         }
         base.Dispose(disposing);      
      }
      
      #endregion
   }
}

Create the InfoPath Form

The following steps demonstrate how to create the Microsoft Office InfoPath™ form:

1. From the Start menu, run Office InfoPath 2003.

2. In the task pane, click Design a Form.

3. In the task pane, click New from Data Source to open the Data Source Setup Wizard.

4. Select Web service as your data source, and then click Next.

5. Select Receive data, and then click Next.

6. Enter the location of your Web service, and then click Next. For example, https://YourCRMServerName/FetchAllAccounts/FetchAllAccounts.asmx, where YourCRMServerName is replaced with the appropriate text. Click Next.

The Data Source Setup Wizard displays a list of methods available from the specified Web service, as shown in figure 1.

Figure 1. Data Source Setup Wizard

7. From the Web service in the FetchAllAccounts example, click the Fetch method, and then click Next.

8. Click Next for InfoPath to query the Web method and automatically resolve the data types returned.

9. The wizard displays the message "You have successfully set up your data source." Select Design query view first, and then click Finish.

You can now customize the text on your query form, as shown in figure 2.

Click here to see larger image

Figure 2. Customizing text on a query form (click image for a larger view)

10. In the task pane, click Views.

11. In the Actions section in the task pane, click Add a New View.

12. Type Display for the view name and then click OK.

13. In the Select a view section in the task pane, right-click your new view Display and then click Set as Default.

You can now customize the display form.

14. Type the label Accounts: in the view window.

15. To add the Accounts table to the Display form, in the task pane, click Data Source.

16. Expand the dataFields node, the s0:FetchResponse node, the FetchResult node, the :resultset node, and finally the result node.

Note   The result set will contain only fields exposed in your Web service that actually contain data.

17. Right-click result and then click Repeating Table.

The table is inserted into your display form, as shown in figure 3.

Click here to see larger image

Figure 3. Inserting an accounts table into a display form (click image for a larger view)

18. Right-click the Repeating Table tab beneath your new table and then click Repeating Table Properties.

19. On the Data tab of the Repeating Table Properties window, deselect Allow users to insert and delete rows.

20. If you want, use Change Binding to customize the rows and columns shown in the table.

21. Click OK.

Customize your display form as necessary. For this example, remove the Accountid column, resize the remaining columns and change the name of the Websiteurl column to Website. When you finish customizing your display form, publish the form to a Web service or SharePoint server. Follow the directions in the Publishing Wizard.

Test the InfoPath Form

You can now test your new InfoPath form by following this procedure:

  1. Open a browser window and enter the path for your new form.
  2. Once you have opened the form, click Run Query.

The results are shown in figure 4.

Click here to see larger image

Figure 4. Test results from running a query (click image for a larger view)

Note   The form viewer's credentials will be used to access the Microsoft CRM database. Therefore, the viewer must be a user in Microsoft CRM with a valid license. The data retrieved from Microsoft CRM will be only that for which the user has read privileges.

Conclusion

It is possible to create a form in Office InfoPath 2003 that links with Microsoft CRM SDK APIs by specifying a Web service for the data source in the InfoPath form. You can then code the Web service to provide wrappers around the SDK methods you need.