Code Example: Contact Creation from an Account Workspace
This example demonstrates how to populate a contact creation form with information associated with the parent account with which the contact is associated.
Applies to: Duet Enterprise for Microsoft SharePoint and SAP Server 2.0 | Office 2010 | SharePoint Server 2010
In SAP systems, an account can be associated with multiple sales contacts. In most cases, each contact that you create will share information with the associated account. The code example described in this topic demonstrates how to create a custom control that populates a contact creation form with information from the associated account. You can download a Microsoft Visual Studio 2010 solution that contains this example at Duet Enterprise: Contact Creation from Account Workspace on Code Gallery (https://code.msdn.microsoft.com/duetfieldcontrol).
Creating Contacts from an Account Workspace
When you create a contact from an account workspace, the contact creation form does not by default populate its fields with information from the account. The contact creation form in this example populates the Account field so that the user who is creating the contact will not need to type this information into the form.
You can add this convenience for users by creating a custom field definition and associating it with a custom control that populates the contact entity with information from the account.
Use the following steps to create the solution.
Create the custom field definition.
Create the custom field class.
Create the custom field control class.
Associate the custom field with the custom control.
Creating the Custom Field Definition
Use the following XML to create a custom SharePoint field definition.
<?xml version="1.0" encoding="utf-8" ?>
<FieldTypes>
<FieldType>
<Field Name="TypeName">PopulateAccountField</Field>
<Field Name="ParentType">Text</Field>
<Field Name="TypeDisplayName">Populate Account Field</Field>
<Field Name="TypeShortDescription">Auto-populates the associated account field while creating contacts for the account from the account site.</Field>
<Field Name="UserCreatable">FALSE</Field>
<Field Name="ShowInListCreate">FALSE</Field>
<Field Name="ShowInDocumentLibraryCreate">FALSE</Field>
<Field Name="ShowInColumnTemplateCreate">FALSE</Field>
<Field Name="Filterable">FALSE</Field>
<Field Name="Sortable">FALSE</Field>
<Field Name="FieldTypeClass">Fully Qualified Assembly Name</Field>
</FieldType>
</FieldTypes>
Be sure to specify a TypeName and to provide the fully qualified name of the type that implements the FieldTypeClass. For more information about creating custom field definitions, see How to: Create a Custom Field Type Definition (https://msdn.microsoft.com/en-us/library/ms415141.aspx).
Creating the Custom Field Class
The code in the following sample creates the custom Field class.
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace Sample
{
public class PopulateAccountfield : SPFieldText
{
public PopulateAccountfield(SPFieldCollection fields, string fieldName)
: base(fields, fieldName)
{
}
public override BaseFieldControl FieldRenderingControl
{
get
{
BaseFieldControl fieldControl = new PopulateAccountfieldControl();
fieldControl.FieldName = this.InternalName;
return fieldControl;
}
}
}
}
Creating the Custom Field Control Class
The code in the following example creates a custom control that inherits from the TextField class. This custom control reads the associated account information and populates the account field for the contact.
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace Sample
{
public class PopulateAccountfieldControl : TextField
{
protected override void CreateChildControls()
{
base.CreateChildControls();
if (!this.Page.IsPostBack)
{
// Check if the form is in 'create' mode.
if (this.ControlMode == SPControlMode.New)
{
// Duet workspace stores the EntityInstanceId in serialized form.
// in the SPWeb's AllProperties dictionary with the key 'EntityInstanceID'
string serializedEntityInstanceId = this.Web.AllProperties["EntityInstanceID"] as string;
// If the site is not a Duet Workspace, it is possible that the property does not exist.
if (string.IsNullOrEmpty(serializedEntityInstanceId))
return;
// Deserialize the id to get the 'identity'.
Microsoft.BusinessData.Runtime.Identity id = Microsoft.BusinessData.Runtime.Identity.Deserialize(serializedEntityInstanceId);
// Get the EntityName from the parent SPWeb’s AllProperties dictionary with the key, 'Key'.
// The stored key is of the form EntityNamespace||EntityName. Extract the entity name from this.
string key = this.Web.ParentWeb.AllProperties["Key"] as string;
string entityName = key.Substring(key.IndexOf("||") + 2);
// Loop through the field collection and find the associated field by checking if the field name contains entityName.
string fkFieldName = string.Empty;
foreach (SPField fld in this.Fields)
{
if (!string.IsNullOrEmpty(fld.RelatedField) &&
fld.RelatedField.Contains(entityName))
{
fkFieldName = fld.RelatedField;
break;
}
}
// Set the field with the read account instance.
this.Item[fkFieldName] =
Microsoft.SharePoint.BusinessData.Infrastructure.EntityInstanceIdEncoder.EncodeEntityInstanceId(id.GetIdentifierValues());
}
}
}
}
}
Associating the Custom Field with the Custom Control
You can associate the custom field with the custom control by adding the following XML to the Business Data Connectivity (BDC) service model.
<TypeDescriptor TypeName="System.String" Name="Extenconta3extValue">
<Properties>
<Property Name="SPCustomFieldType" Type="System.String">PopulateAccountField</Property>
</Properties>
</TypeDescriptor>