Del via


Defining the key

Web Services for Microsoft Dynamics GP uses key objects to uniquely identify documents. To add a class that defines the key for a new document type, complete the following steps:

  1. Add a class for the key.

    Add a new public class for your document key. Add the key class to the same file and namespace as your document type class.

    The following sample code adds the LeadKey class. Notice how the LeadKey class is added to the Microsoft.Dynamics.GP.Samples.Leads namespace.

    namespace Microsoft.Dynamics.GP.Samples.Leads
    

{ public class LeadKey {

}

}

  1. Mark the class as serializable.

    Add the Serializable and DataContract attributes.

    [Serializable, DataContract]
    

public class LeadKey {

}

  1. Inherit from the ReferenceKey class.

    The key class must inherit from the Microsoft.Dynamics.GP.ReferenceKey class.

    public class LeadKey : ReferenceKey
    

{

}

  1. Add a private method to initialize data fields.

    The Reference Key class uses a list object to store the document ID value. To initialize the list, create an empty Property object and add it to the properties collection of the base class.

    The following sample code shows the Initialize method for the LeadKey class. Notice how a Property object is created, initialized, and added to the ReferenceKey properties collection.

    private void Initialize()
    

{ Property id = String.Empty; properties.Add(id); }

  1. Add a constructor and OnDeserializing method.

    Create a class constructor and OnDeserializing method. The OnDeserializing method enables your class to be serialized by the Dynamics GP Service framework. Call the Initialize method from both of these methods.

    The following sample code shows the constructor and OnDeserializing method for the LeadKey class. Notice how the OnDeserializing attribute is added to the OnDeserializing method.

    public LeadKey()
    

{ Initialize(); } [OnDeserializing] public new void OnDeserializing(StreamingContext context) { Initialize(); }

  1. Add a property for the key ID.

    Implement a class property to set or retrieve the value of the ID in the properties collection of the base class. Add the DataMember attribute to the property. Set the EmitDefaultValue property of the attribute to false.

    Hint: If your document requires a multi-segment key, add a property for each segment of the key.

    The following sample code shows the ID property of the LeadKey class. Notice how the property sets or retrieves the value of the first element in the properties collection of the base class.

    [DataMember(EmitDefaultValue = false)]
    

public string Id { get { return properties[0]; } set { properties[0] = value; } }

  1. Add a GetInstance method.

    Add a static GetInstance method that returns an instance of your key class.

    public static LeadKey GetInstance()
    

{ return new LeadKey(); }

  1. Save the class.

    From the File menu, choose Save.