using web services for microsoft dynamics gp in business ......4 using web services for microsoft...

10
Microsoft Dynamics GP Using Web Services for Microsoft Dynamics GP in Business Portal White Paper How to use Visual Studio™ 2005 and Web Services for Microsoft Dynamics GP 10.0 to create a new Business Portal web part. Date: September 26, 2007 INTEGRATE

Upload: others

Post on 20-Apr-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

  • Microsoft Dynamics™ GP

    Using Web Services for Microsoft Dynamics GP in Business Portal

    White Paper

    How to use Visual Studio™ 2005 and Web Services for Microsoft Dynamics GP 10.0 to create a new Business Portal web part.

    Date: September 26, 2007

    INTEGRATE

  • 2

    USING WEB SERVICES FOR MICROSOFT DYNAMICS GP IN BUSINESS PORTAL

    Table of Contents

    Introduction .......................................................................................................................3

    Internet Information Services (IIS) configuration .........................................................3 Uninstall Web Services for Microsoft Dynamics GP:...........................................................................................................3 Create a new web site: ...................................................................................................................................................................4 Reinstall Web Services for Microsoft Dynamics GP:............................................................................................................4 Add the Microsoft® Windows® user to the BDC Administrator role:...........................................................................5

    Visual Studio 2005 Web Site custom application ..........................................................6

    Exposing the custom application in Business Portal .....................................................8

    How to use Company Database ID in custom application............................................9 Microsoft Business Framework Method: .................................................................................................................................9 Office SharePoint Server Method: .............................................................................................................................................9

  • 3 USING WEB SERVICES FOR MICROSOFT DYNAMICS GP IN BUSINESS PORTAL

    Introduction This document provides the developer a sample solution that demonstrates how to add new functionality into Business Portal for Microsoft Dynamics GP 10.0. In this scenario, we use Visual Studio 2005 and Web Services for Microsoft Dynamics GP 10.0 to create a web part that creates Sales Quotes from the Sales Quote List page in Business Portal.

    The custom Enter Sales Quotes web part is shown below in the Sales Quote List page:

    Internet Information Services (IIS) configuration If Business Portal and Web Services for Microsoft Dynamics GP will be installed on the same web server, these programs need to be installed in their own web sites. The following steps describe how to move the web site for Web Services for Microsoft Dynamics GP to a new site. (For more information, refer to Knowledge Base article 920903).

    Uninstall Web Services for Microsoft Dynamics GP:

    1. From the Start menu, choose Control Panel, and then choose Add or Remove Programs.

    2. Select Web Services for Microsoft Dynamics GP, and then click Change.

    3. Click Remove. Do not mark the Remove SQL Objects and Data check box.

    4. Click Next.

  • 4

    USING WEB SERVICES FOR MICROSOFT DYNAMICS GP IN BUSINESS PORTAL

    Create a new web site:

    1. From the Start menu, choose Administrative Tools, and then choose Internet Information Services (IIS) Manager.

    2. Expand the local computer name in the left pane, and then right-click Web Sites. Choose New, and then choose Web Site.

    3. In the Web Site Creation wizard, click Next.

    4. On the Web Site Description page, enter a description for the site, such as "Web services."

    5. Click Next.

    6. On the IP Address and Port Settings page, enter a port number other than port 80. Alternatively, enter a host header, such as “Web services.”

    Note: Port 80 is the default port number for the Default Web Site. Only one web site can use a specific port number unless you specify a host header for the new site. That host header lets IIS correctly route requests to the appropriate sites. See the IIS documentation for more information about port numbers and host headers.

    7. Click Next.

    8. On the Web Site Home Directory page, enter the path of the folder in which you want to create the new web site. This folder can be in any location, or you can create a new folder.

    9. Clear the Allow anonymous access to this web site check box.

    10. Click Next.

    11. On the Web Site Access Permissions page, mark the Read check box, the Run scripts check box, and the Execute check box.

    12. Click Next to complete the wizard and to create the new web site.

    Reinstall Web Services for Microsoft Dynamics GP:

    1. Reinstall Web Services for Microsoft Dynamics GP to the site that you created in the previous procedure. Use the installation procedure described in the Microsoft Dynamics GP Web Services Installation and Administration Guide.

    2. Verify the new URL in a web browser. You can access the web site for Web Services for Microsoft Dynamics GP by using one of the following URLs, depending on whether you specified a port or a host header when you created the site:

    • http:///DynamicsGPWebServices

    • http:///DynamicsGPWebServices

  • 5 USING WEB SERVICES FOR MICROSOFT DYNAMICS GP IN BUSINESS PORTAL

    Add the Microsoft® Windows® user to the BDC Administrator role:

    The Business Data Catalog web parts in Business Portal use Web Services for Microsoft Dynamics GP. It is necessary to use the Dynamics Security Console to add the Windows user running the Application pool for the Business Portal web site into the BDC Administrator role.

    To determine the Application Pool for the Business Portal web site, right click on the web site and choose Properties, and then display the Home Directory tab. If you are not sure which web site is the Business Portal web site, click each web site to look for a virtual directory called BusinessPortal.

    To add the Windows user to the BDC Administrator role, complete the following steps:

    1. Open the Dynamics Security Console on a computer where the console is installed. From the Start menu, choose Administrative Tools, and then choose Dynamics Security Console.

    2. Select the Role Assignments node in the left pane of the Dynamics Security Console.

    3. From the Action menu, choose Add. The Add Role Assignments window will appear.

    4. In the Role list, select the BDC Administrator role.

    5. Click Add Windows Users to add individual windows users to the role assignment.

    6. Select the desired company or all companies and click OK to save the new role assignment.

  • 6

    USING WEB SERVICES FOR MICROSOFT DYNAMICS GP IN BUSINESS PORTAL

    Visual Studio 2005 Web Site custom application The custom application is an ASP.NET web site application created with Visual Studio 2005. A web reference in the project is made to the DynamicsGPService.asmx page to access the WSDL (Web Services Description Language) of the Dynamics GP Web Service.

    The web site application is made up of a simple Default.aspx page that has four text box controls, two button controls, and some labels. The Default.aspx page references a DataMgr class that handles calling the web service. The attached code is for the Default.aspx.cs and DataMgr.cs files.

    Note: To view attachments, you must be viewing this document in Adobe Reader 6.0 or later, or in the full version of Acrobat. Right-click the paperclip icon to the left of the desired file, and choose to open or save the file.

    • Default.aspx.cs.txt

    • DataMgr.cs.txt

    When it is time to publish the web site, make sure the following options are marked:

    • Allow this precompiled site to be updatable

    • Enable strong naming on precompiled assemblies

    • Mark assemblies with AllowPartiallyTrustedCallerAttribute

    The following illustration shows the Publish Web Site window in Visual Studio 2005. (To view this window, choose Build from the Publish Web Site menu.)

    Steps for using the Strong Name tool to create a key file are located at http://msdn2.microsoft.com/en-us/library/6f05ezxy(vs.80).aspx. It is necessary to follow these steps when publishing the web site so the web part will display correctly. Windows SharePoint® Services 3.0 will need to trust the new assemblies that are generated from the Publish Web Site process.

    In addition to the Publish Web Site steps, you will need to add a new code group to the WSS_minimaltrust.config file, using the following steps:

    1. From the Start menu, choose Administrative Tools, and then choose Microsoft .NET 2.0 Configuration.

    2. Expand Runtime Security Policy, expand Machine, expand Code Groups, expand All Code, and then expand My_Computer_Zone.

    3. Right-click My_Computer_Zone, and then choose New.

    http://msdn2.microsoft.com/en-us/library/6f05ezxy(vs.80).aspxhttp://msdn2.microsoft.com/en-us/library/6f05ezxy(vs.80).aspx

    //Code for default.aspx.cs (default.aspx):

    using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Services.Protocols;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using DynamicsGPService;using Microsoft.BusinessFramework;using Microsoft.BusinessFramework.Entity;using Microsoft.BusinessFramework.Security;using Microsoft.Dynamics.Common.Company;using Microsoft.Dynamics.GP.Containment;using Microsoft.Dynamics.Common.Containment;using Microsoft.Office.Server.UserProfiles;

    public partial class _Default : System.Web.UI.Page { //Use MBF to grab company database information from BP session public string GetDBNameMBF() { string companyDB; EntityKey eK;

    eK = (Microsoft.Dynamics.Common.Company.Company.Key)EnterpriseSession.DefaultParentKey;

    Microsoft.Dynamics.Common.Company.Company company = (Microsoft.Dynamics.Common.Company.Company)eK.GetEntity();

    companyDB = company.Name;

    return companyDB; }

    // Use Office SharePoint Server UserProfile class for company database ID

    public string GetDataBaseOSS() { string companyDB; UserProfile userProfile = ProfileLoader.GetProfileLoader().GetUserProfile(); companyDB = userProfile["CompanyKeyID"].ToString();

    return companyDB; } protected void Page_Load(object sender, EventArgs e) { statusMessageLabel.Text = "System ready...";

    // Set Company TextBox to method using MBF to retrieve company database name companyTextBox.Text = GetDBNameMBF();

    // Set Company TextBox to method using UserProfile from Microsoft.Office.SharePoint companyTextBox2.Text = GetDataBaseOSS();

    } protected void clearButton_Click(object sender, EventArgs e) { statusMessageLabel.Text = "System ready..."; customerTextBox.Text = ""; itemTextBox.Text = ""; } protected void createButton_Click(object sender, EventArgs e) { //Create an instance of DataMgr class DataMgr dataMgr = new DataMgr();

    //Set customerID to Customer ID from textbox in UI string customerID = customerTextBox.Text;

    //Set itemID to Item ID from textbox in UI string itemID = itemTextBox.Text;

    //set Qty string to Qty value from textbox in UI string qtyAmount = qtyTextBox.Text;

    try { dataMgr.CreateSalesQuoteAction(customerID, itemID, qtyAmount); statusMessageLabel.Text = "Quote successfully created. Document number is auto-generated."; } catch (SoapException soapException) { statusMessageLabel.Text = "Error: " + soapException.Message + "Stack Trace: " +soapException.StackTrace; }

    }}

    MBSFile AttachmentDefault.aspx.cs.txt

    //Code for DataMgr.cs (DataMgr class):

    using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using DynamicsGPService;using Microsoft.BusinessFramework;using Microsoft.BusinessFramework.Entity;using Microsoft.Dynamics.Common.Company;

    /// /// Summary description for DataMgr/// public class DataMgr{ private DynamicsGP wsDynamicsGP;private Context context; private CompanyKey companyKey; BatchKey batchKey; SalesDocumentTypeKey documentTypeKey;

    public DataMgr(){try { // Instantiate the web service proxy class wsDynamicsGP = new DynamicsGP();

    // Be sure the default credentials are used wsDynamicsGP.UseDefaultCredentials = true;

    // Create the context object context = new Context();

    //Create CompanyKey companyKey = new CompanyKey(); companyKey.Id = GetDBID();//Method from MBF to grab company database ID from BP session // Set up the context object // To retrieve from the system database set the organization key to null context.OrganizationKey = (OrganizationKey)companyKey; context.CultureName = "en-US"; }

    catch (Exception err) { // Throw the error to allow it to be diplayed by the UI throw err; } }

    //Use MBF to grab company database ID from BP session public int GetDBID() { int dbID; EntityKey eK;

    eK = (Microsoft.Dynamics.Common.Company.Company.Key)EnterpriseSession.DefaultParentKey;

    Microsoft.Dynamics.Common.Company.Company company = (Microsoft.Dynamics.Common.Company.Company)eK.GetEntity();

    dbID = company.ID;

    return dbID; }

    public void CreateSalesQuoteAction(string customerId, string itemID, string qtyAmount) { // Create a customer key CustomerKey customerKey = new CustomerKey(); customerKey.Id = customerId;

    SalesQuote salesQuote; //DocumentTypeKey documentTypeKey;

    try { // Create a sales quote object salesQuote = new SalesQuote();

    // Create a sales document type key documentTypeKey = new SalesDocumentTypeKey(); documentTypeKey.Type = SalesDocumentType.Quote; documentTypeKey.Id = "STDQTE";

    // Set the document type key of the sales quote object salesQuote.DocumentTypeKey = documentTypeKey;

    // Set the customer key property of the sales quote object salesQuote.CustomerKey = customerKey;

    // Create a batch key batchKey = new BatchKey(); batchKey.Id = "WS SALES QUOTES";

    // Set the batch key property of the sales quote object salesQuote.BatchKey = batchKey;

    // Create a sales quote line to specify the quote items SalesQuoteLine salesQuoteLine = new SalesQuoteLine();

    // Create an item key ItemKey quoteItem = new ItemKey(); quoteItem.Id = itemID;

    // Set the item key property of the sales quote line object salesQuoteLine.ItemKey = quoteItem;

    // Create a quantity object DynamicsGPService.Quantity itemQuantity = new DynamicsGPService.Quantity(); itemQuantity.Value = 0;

    // Initialize the quantity to invoice and quantity to order properties salesQuoteLine.QuantityToInvoice = itemQuantity; salesQuoteLine.QuantityToOrder = itemQuantity;

    // Create a quantity object DynamicsGPService.Quantity quoteItemQuantity = new DynamicsGPService.Quantity(); //quoteItemQuantity.Value = (decimal)qtyAmount.; quoteItemQuantity.Value = Convert.ToDecimal(qtyAmount);

    // Set the quantity property of the sales quote line object salesQuoteLine.Quantity = quoteItemQuantity;

    // Create an array of sales quote lines // Initialize the array with the sales quote line object SalesQuoteLine[] quotes = { salesQuoteLine };

    // Add the sales quote line to the sales quote object salesQuote.Lines = quotes; // Get the create SalesQuote policy Policy createSalesQuotePolicy = wsDynamicsGP.GetPolicyByOperation("CreateSalesQuote", context);

    // Create Sales Quote wsDynamicsGP.CreateSalesQuote(salesQuote, context, createSalesQuotePolicy); } catch (Exception err) { // Throw the exception to allow it to be diplayed by the UI throw err; } } }

    MBSFile AttachmentDataMgr.cs.txt

  • 7 USING WEB SERVICES FOR MICROSOFT DYNAMICS GP IN BUSINESS PORTAL

    4. In the Create Code Group window, give the group a name such as “WSStrongName.”

    5. Click Next.

    6. In the Choose Condition Type window, click Strong Name, click Import, and then locate the .dll file that contains the digitally signed assembly. A public key appears on the window.

    7. Click Next, click Existing Permission Set, and then click Full Trust.

    8. Click Next.

    9. Click Finish. This step creates a code group for that key.

    10. Locate the C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG folder on the computer where you created the code group.

    11. Open the Security.config file, and then copy the code group nodes that contain the new group. For example, a code group node resembles the following:

    version="1"

    PermissionSetName="FullTrust"

    Name="BPSDKStrongName"

    Description="">

    version="1"

    PublicKeyBlob="00240000048000009400000006020000002400005253413100040000010001004DC871C3C1549B921CA9417CF9FE423F0FBF8B98720EF7C8C1988945FF5B45D4934594201C9FE886F36B20C0976AFF63DCAC1961E7287616AA7C79B97273F14CD659436A2FD3AEEBA048D3E01AE3A834F31615CA97B8FFEF03726C2D09F00014692D63824E7328EBDD92C4E63FDB8F9A35B90AACBD6E2A1FC2D87B41B228F9BB"/>

    12. Locate the C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\Config folder, open the WSS_minimaltrust.config file, and then locate the following code group.

    version="1"

    PermissionSetName="FullTrust"

    Name="MBFStrongName"

    Description="">

    version="1"

    PublicKeyBlob="0024000004800000940000000602000000240000525341310004000001000100B5FC90E7027F67871E773A8FDE8938C81DD402BA65B9201D60593E96C492651E889CC13F1415EBB53FAC1131AE0BD333C5EE6021672D9718EA31A8AEBD0DA0072F25D87DBA6FC90FFD598ED4DA35E44C398C454307E8E33B8426143DAEC9F596836F97C8F74750E5975C64E2189F45DEF46B2A2B1247ADC3652BF5C308055DA9"/>

    13. Paste the code group nodes that you copied in step 11 into the WSS_minimaltrust.config file immediately after the code group that you located in step 12. This is a critical step. If the code group is not put in the correct location, the code and the Business Portal assemblies will not be trusted.

    14. Save the WSS_minimaltrust.config file.

    15. Copy the strong named assemblies to the Bin folder of the Business Portal web site and the aspx pages to the desired location.

  • 8

    USING WEB SERVICES FOR MICROSOFT DYNAMICS GP IN BUSINESS PORTAL

    Exposing the custom application in Business Portal Since the custom application allows a user to enter Sales Quotes, the location of the custom application will be on the Sales Quote List page under the Sales Center site.

    The custom application will be rendered in a Page Viewer web part. The default.aspx page will be located in the BusinessPortal virtual directory. The default location is at C:\Program Files\Microsoft Dynamics\Business Portal\Applications. The following screen shot illustrates the Page Viewer web part points to a new folder called SalesQuoteMgmt. The path is /BusinessPortal/Applications/SalesQuoteMgmt:

    A benefit of storing the custom application pages in the BusinessPortal virtual directory is the ability to leverage the Business Portal session information. In this example, the custom application is leveraging the user’s Business Portal session to attain the company database ID and set the OrganizationKey of the Context object in the Dynamics GP Web Service call.

    The code files attached below provide examples of using the Microsoft Business Framework and Office SharePoint Server assemblies. The Office SharePoint Server assembly method can be used when Business Portal is installed in a Microsoft Office SharePoint Server environment.

    Note: To view attachments, you must be viewing this document in Adobe Reader 6.0 or later, or in the full version of Acrobat. Right-click the paperclip icon to the left of the desired file, and choose to open or save the file.

    • MBFMethod.txt

    • OSSMethod.txt

    //Microsoft Business Framework Method:

    //Add the below using directives

    using Microsoft.BusinessFrameworkusing Microsoft.BusinessFramework.Entity;

    //Method to use company database ID from BP session

    public int GetDBID() { int dbID; EntityKey eK;

    eK = (Microsoft.Dynamics.Common.Company.Company.Key)EnterpriseSession.DefaultParentKey;

    Microsoft.Dynamics.Common.Company.Company company = (Microsoft.Dynamics.Common.Company.Company)eK.GetEntity();

    dbID = company.ID;

    return dbID; }

    //Set the CompanyKey of the Web Services to the value returned by the GetDBID() method.

    // GP Web Service Context object snippet context = new Context();

    // Create CompanyKey and Set OrganizationKey companyKey = new CompanyKey(); companyKey.Id = GetDBID(); context.OrganizationKey = (OrganizationKey)companyKey;

    MBSFile AttachmentMBFMethod.txt

    //Office SharePoint Server method

    //Add the following using directive:

    using Microsoft.Office.Server.UserProfiles;

    //Method to use company database ID from BP session

    public int GetDataBaseOSS() { int companyDB;

    UserProfile userProfile = ProfileLoader.GetProfileLoader().GetUserProfile(); companyDB = Convert.ToInt32(userProfile["CompanyKeyID"].ToString()); return companyDB; }

    //Set the CompanyKey of the Web Services to the value returned by the GetDataBaseOSS() method.

    // GP Web Service Context object snippet context = new Context();

    // Create CompanyKey and Set OrganizationKey companyKey = new CompanyKey(); companyKey.Id = GetDataBaseOSS(); context.OrganizationKey = (OrganizationKey)companyKey;

    MBSFile AttachmentOSSMethod.txt

  • 9 USING WEB SERVICES FOR MICROSOFT DYNAMICS GP IN BUSINESS PORTAL

    How to use Company Database ID in custom application Use the method appropriate for your installation type.

    Microsoft Business Framework Method:

    1. Open the project using Visual Studio 2005.

    2. In Solution Explorer, right-click on the project and choose Add Reference.

    3. View the Browse tab and locate the Microsoft.BusinessFramework.dll. Click OK. (This assembly is located in the Bin folder of the Business Portal web site.)

    4. Add the following using directives:

    using Microsoft.BusinessFramework

    using Microsoft.BusinessFramework.Entity;

    5. Add the code from the MBFMethod.txt file to the class in the project. Notice that the CompanyKey of the Web Services is set to the value returned by the GetDBID() method.

    6. Build and publish the web site application, following the recommendations in this document.

    Office SharePoint Server Method:

    1. Open the project using Visual Studio 2005.

    2. In Solution Explorer, right-click on the project and choose Add Reference.

    3. View the Browse tab and locate the Microsoft.Office.Server.dll. Click OK. (The default location is C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI)

    4. Add the following using directive:

    using Microsoft.Office.Server.UserProfiles;

    5. Add the code from the OSSMethod.txt file to the class in the project. Notice that the CompanyKey of the Web Services is set to the value returned by the GetDataBaseOSS() method.

    6. Build and publish the web site application, following the recommendations in this document.

  • 10

    USING WEB SERVICES FOR MICROSOFT DYNAMICS GP IN BUSINESS PORTAL

    The information contained in this document represents the current view of Microsoft Corporation on the issues discussed as of the date of publication. Because Microsoft must respond to changing market conditions, this document should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information presented after the date of publication.

    This White Paper is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED, OR STATUTORY, AS TO THE INFORMATION IN THIS DOCUMENT.

    Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation.

    Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property.

    © 2007 Microsoft Corporation. All rights reserved.

    Microsoft, the Microsoft Dynamics Logo, Microsoft Dynamics, SharePoint, Visual Studio, and Windows are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries.

    Table of ContentsIntroductionInternet Information Services (IIS) configurationUninstall Web Services for Microsoft Dynamics GP: Create a new web site:Reinstall Web Services for Microsoft Dynamics GP: Add the Microsoft® Windows® user to the BDC Administrator role:

    Visual Studio 2005 Web Site custom applicationExposing the custom application in Business PortalHow to use Company Database ID in custom applicationMicrosoft Business Framework Method:Office SharePoint Server Method: