Share via


Retrieving extension data

The ExtensionList collection is available for every service object that inherits from the BusinessObject class. When an application retrieves an object from the Dynamics GP service, it should examine the ExtensionList collection to find whether any additional data is available in the Extension objects contained in the collection. The application should examine the ExtensionId parameter of each Extension object to find out what data is included in the extension.

For example, assume the Customer object was extended to include contact history information. An Extension object with the ExtensionId value "ContactHistory" could be included with the customer object when the object was retrieved. The application retrieving the customer object would look in the ExtensionList collection for this additional data.

Once the Extension object is found, the application will use the DocExtension property to retrieve the XML element that contains the additional data. In this example, the contact history XML element has the following format:

<ContactHistory>
    <FirstContactDate>6/6/1999 12:00:00 AM</FirstContactDate>
    <FirstContactSalesperson>NANCY B.</FirstContactSalesperson>
    <LastContactDate>3/1/2006 12:00:00 AM</LastContactDate>
    <LastContactSalesperson>ERIN J.</LastContactSalesperson>
</ContactHistory>

Hint: It is the responsibility of developer who extended the object to provide information about the format of the XML element containing the extended data.

The following C# example demonstrates retrieving the contact history information that is being included with the Customer object in the ExtensionList collection. Note how the ExtensionList collection is examined to determine whether the contact history information has been included. The XML element containing the contact history data is processed to retrieve the individual items.

Cc508782.LegacyEndpoint(en-us,MSDN.10).gif** Legacy endpoint**

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using DynamicsGPWebServiceSample.DynamicsGPService;

namespace DynamicsGPWebServiceSample
{
    class Program
    {
        static void Main(string[] args)
        {
            CompanyKey companyKey;
            Context context;
            Customer customer;
            CustomerKey customerKey;
            string firstContactDate;
            string firstContactSalesperson;
            string lastContactDate;
            string lastContactSalesperson;

            // Create an instance of the web service
            DynamicsGP wsDynamicsGP = new DynamicsGP();

            // Make sure that default credentials are being used
            wsDynamicsGP.UseDefaultCredentials = true;

            // Create a context with which to call the service
            context = new Context();

            // Specify which company to use (lesson company)
            companyKey = new CompanyKey();
            companyKey.Id = (-1);

            // Set up the context
            context.OrganizationKey = (OrganizationKey)companyKey;

            // Create a customer key
            customerKey = new CustomerKey();
            customerKey.Id = "WORLDENT0001";

            // Retrieve the customer object
            customer = wsDynamicsGP.GetCustomerByKey(customerKey, context);

            // Look for the contact history extension
            foreach (Extension ext in customer.Extensions)
            {
                if (ext.ExtensionId == "ContactHistory")
                {
                    XmlElement contactHistory;
                    contactHistory = customer.Extensions[0].DocExtension;

                    XmlNodeList nodeList;
                    nodeList = contactHistory.ChildNodes;

                    //First contact date
                    firstContactDate = nodeList[0].InnerText.ToString();

                    //First contact salesperson
                    firstContactSalesperson= nodeList[1].InnerText.ToString();

                    //Last contact date
                    lastContactDate = nodeList[2].InnerText.ToString();

                    //Last contact salesperson
                    lastContactSalesperson = nodeList[3].InnerText.ToString();
                }
            }
        }
    }
}

Cc508782.NativeEndpoint(en-us,MSDN.10).gif** Native endpoint **

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using WebServiceSample.DynamicsGPService;

namespace DynamicsGPWebServiceSample
{
    class Program
    {
        static void Main(string[] args)
        {
            CompanyKey companyKey;
            Context context;
            Customer customer;
            CustomerKey customerKey;
            string firstContactDate;
            string firstContactSalesperson;
            string lastContactDate;
            string lastContactSalesperson;

            // Create an instance of the web service
            DynamicsGPClient wsDynamicsGP = new DynamicsGPClient();

            // Create a context with which to call the service
            context = new Context();

            // Specify which company to use (lesson company)
            companyKey = new CompanyKey();
            companyKey.Id = (-1);

            // Set up the context
            context.OrganizationKey = (OrganizationKey)companyKey;

            // Create a customer key
            customerKey = new CustomerKey();
            customerKey.Id = "AARONFIT0001";

            // Retrieve the customer object
            customer = wsDynamicsGP.GetCustomerByKey(customerKey, context);

            // Look for the contact history extension
            foreach (Extension ext in customer.Extensions)
            {
                if (ext.ExtensionId == "ContactHistory")
                {
                    // The extension is a LINQ element type
                    XElement contactHistory;
                    contactHistory = customer.Extensions[0].DocExtension;

                    // Use LINQ queries to retrieve the values
                    //First contact date
                    firstContactDate = (string)contactHistory
                        .Element("FirstContactDate");

                    //First contact salesperson
                    firstContactSalesperson = (string)contactHistory
                        .Element("FirstContactSalesperson");

                    //Last contact date
                    lastContactDate = (string)contactHistory
                        .Element("LastContactDate");

                    //Last contact salesperson
                    lastContactSalesperson = (string)contactHistory
                        .Element("LastContactSalesperson");
                }
            }
        }
    }
}