My 2p about ERP Solutions, Information Worker Solutions and other software products (mainly Microsoft Dynamics AX and Microsoft SharePoint).

20 July 2009

Integration MOSS 2007 – Dynamics AX with BDC through a custom field type (part 2)

by Patrik Luca 14 comments



In this series of post, I’ll show how you can integrate Microsoft Office SharePoint Server 2007 with another line of business system, more specifically Dynamics AX.

The purpose is to create a custom field type, which can be added as a column to a SharePoint list. This SharePoint column renders a dropdown box with values from a Microsoft SQL Server table’s column. As such, in stead of duplicating the possible values on your SharePoint box each time new values are added in your line of business system, the available values for this SharePoint column are always in synch with your line of business system.

This first post describes how to create your connection between Microsoft Office SharePoint Server 2007 and Dynamics AX.

In this post, I’ll describe how to create the custom field type.

In a following post, I’ll describe how to make this custom field type more generic by using a dynamic where clause in the query.


  1. In Visual Studio, select External Tools from the Tools menu.
  2. In the External Tools dialog, click Add and enter Get Assembly Public Key for the Title.
  3. Fill the Command textbox by browsing to C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\sn.exe.
  4. In the Arguments textbox, type the following (case sensitive) –Tp $(TargetPath).
  5. Enable the Use Output window checkbox.
  6. Click OK. The new command is added to the Tools menu.

See also: Prepare for Development of Custom Field Rendering Controls.

Create a Visual Studio project

  1. Create a Class Library project with Microsoft Visual Studio 2005.

  2. Create a folder structure in your project.

  3. Add a reference to the System.Web dll (add it by selecting it in the .NET tab page) and the dlls: Microsoft.Office.Server, Microsoft.SharePoint and microsoft.sharepoint.portal (add them by browsing to C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI to find it).

  4. Right-click the project name in Solution Explorer and select Properties.
  5. On the Application tab of the Properties dialog, enter MyCompany.SharePoint.DAX.CustomFields as the Default namespace and MyCompany.SharePoint.DAX.CustomFields.DAXProjActivityTxt as the Assembly name.
  6. Open the Signing tab and the select Sign the assembly.
  7. Choose <New…> from the Choose a strong name key file drop-down list box.
  8. In the Create Strong Name Key dialog, type MyCompany.SharePoint.DAX.CustomFields.DAXProjActivityTxt in the Key file name box, and then be sure that the Protect … check box is not checked. Click OK.
  9. Open the Build Events tab and type the following in the Post-build event command line box.
    cd "$(ProjectDir)"
    "%programfiles%\microsoft visual studio 8\sdk\
    v2.0\bin\gacutil" /i "$(TargetPath)" /nologo /f
    %systemroot%\system32\iisapp.vbs /a
    "SharePoint_App_Pool" /r
    xcopy *.ascx
    "C:\Program Files\Common Files\Microsoft Shared\
    web server extensions\12\TEMPLATE\CONTROLTEMPLATES\"
    xcopy 12\Template\xml\fldtypes*.xml
    "C:\Program Files\Common Files\Microsoft Shared\
    web server extensions\12\TEMPLATE\XML\" /y

  10. SharePoint_App_Pool with the actual name of the Internet Information Server (IIS) Application Pool that is assigned to your Windows SharePoint Services Web Application.

See also: To set up the custom field project.

Create the Field Rendering Control

  1. Create the Field Rendering Control, to render your custom field in New mode or Edit mode (see Field Rendering Controls). Rename the Class1.cs file to DAXProjActivityTxtFieldControl.cs.

  2. Edit the DAXProjActivityTxtFieldControl.cs and change the namespace to MyCompany.SharePoint.DAX.CustomFields.

  3. Add the necessary using statements:

    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.Office.Server.ApplicationRegistry.MetadataModel;
    using Microsoft.Office.Server.ApplicationRegistry.SystemSpecific.Db;
  4. Replace the Class declaration with the following code:

    public class DAXProjActivityTxtFieldControl : TextField
  5. Add following code lines to define the controls:
    private DropDownList DAXProjActivityTxtDropDownList;
    private HtmlTable table;
  6. Add the following override of the CreateChildControls method.

    protected override void CreateChildControls()
    this.table = new HtmlTable();
    HtmlTableRow row = new HtmlTableRow();
    HtmlTableCell cell = null;
    = TemplateContainer.FindControl(
    as DropDownList;
    if (DAXProjActivityTxtDropDownList == null)
    throw new ArgumentException("Activity
    dropdown list not found.
    Please check if your control
    template is valid.");

    if (this.ControlMode == SPControlMode.Edit ||
    this.ControlMode == SPControlMode.New)
    row = new HtmlTableRow();
    cell = new HtmlTableCell();
    this.DAXProjActivityTxtDropDownList =
    new DropDownList();
    DAXProjActivityTxtDropDownList.ToolTip =
    "Enter Activity";
    DAXProjActivityTxtDropDownList.Width = 255;
    string currentValue = (string)this.ItemFieldValue;

    if (currentValue != null &
    currentValue != string.Empty)
    = currentValue;
    else if
    > 0)
    = "";



    LiteralControl literalControl = new LiteralControl();
    cell = new HtmlTableCell();
    cell.ColSpan = 4;

    if (this.ControlMode == SPControlMode.Display)
    = "<p id='SPFieldChoice'
    font-size:11; margin-bottom:-3;
    + this.ItemFieldValue + "</p>";

  7. Add the following code to update the underlying value of the field to the latest user-set value:

    public override void UpdateFieldValueInItem()

    this.Value =
    this.ItemFieldValue = this.Value;
  8. Add a method which uses the Business Data Catalog object model to populate the dropdown list with activities:

    private void PopulateDAXProjActivityTxts(string partName)
    //connect to the LobSystemInstance that represents
    //the DAX Activities business application in the BDC
    NamedLobSystemInstanceDictionary instances
    = ApplicationRegistry.GetLobSystemInstances();
    LobSystemInstance instance = instances["Your_Instance"];

    // Connect to the DAX Activity entity
    Entity entity
    = instance.GetEntities()

    Method method
    = entity.GetMethods()

    MethodInstance methodinstance
    = method.GetMethodInstances()
    Object[] args
    = methodinstance.GetMethod().
    args[0] = “your projid”;
    args[1] = “your dataareaid”;
    DbEntityInstanceEnumerator daxprojactivitytxts
    = (DbEntityInstanceEnumerator)entity.Execute(
    methodinstance, instance, ref args);

    // loop through the activities
    // and populate the activities dropdown
    while (daxprojactivitytxts.MoveNext())
    DbEntityInstance daxprojactivitytxt
    = (DbEntityInstance)daxprojactivitytxts.Current;
    new ListItem(
  9. Add the following override of the Render method.
    protected override void Render(HtmlTextWriter output)

Create the Custom Field Class

  1. Add a new class to the root of the project called DAXProjActivityTxtField.cs.

  2. Edit the DAXProjActivityTxtField.cs and change the namespace to MyCompany.SharePoint.DAX.CustomFields.
  3. Add the necessary using statements:

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
  4. Let the class derive from SPFieldText.

    public class DAXProjActivityTxtField : 
  5. Implement two required constructors:

    public DAXProjActivityTxtField(
    SPFieldCollection fields, string fieldName)
    : base(fields, fieldName)

    public DAXProjActivityTxtField(
    SPFieldCollection fields, string typeName,
    string displayName)
    : base(fields, typeName, displayName)
  6. Override the FieldRenderingControl as follows:

    public override BaseFieldControl FieldRenderingControl
    BaseFieldControl control =
    new DAXProjActivityTxtFieldControl();
    control.FieldName = this.InternalName;
    return control;

Create the Field Type definition

  1. Add a new XML File in the folder XML of your project folder structure, called fldtypes_daxprojactivitytxt.xml.

  2. Edit the file by entering following lines:

    <?xml version="1.0" encoding="utf-8" ?>
    <field name="TypeName">DAXProjActivityTxt</field>
    <field name="ParentType">Text</field>
    <field name="TypeDisplayName">DAX ProjActivityTxt</field>
    <field name="TypeShortDescription">DAX project activity cust fld.</field>
    <field name="UserCreatable">TRUE</field>
    <field name="FieldTypeClass">
    Version=, Culture=neutral, PublicKeyToken=cf2f0bd24cc4b888</field>
  3. Replace MyCompany with the same value as done in the replacement of the namespaces. Replace the value for the PublicKeyToken with the value you receive by executing the previously created External Tool in Visual Studio (Tools > Get Assembly Public Key)

Build the Visual Studio project

Build your Visual Studio project: the post-build events added previously will install everything needed to your SharePoint environment.

Add column to list

Add a SharePoint column to one of your SharePoint lists or create a site column: you’ll have a new type of information option. Choose it for the column you are creating.

Set Permission

You need to give the appropriate permissions to the SharePoint users who need to be able to edit the values in this column. Open SharePoint Central Administration, go to your Shared Service Provider and choose View applications in the  Business Data Catalog section. Select your previously created Application and choose Manage Permissions. Give the SharePoint users Execute Right on the Application.


When you create or edit an item in the list, your drop down column will be loaded with values from your Microsoft Dynamics AX source and should look like below:

Interesting resources

Comments 14 comments
BWL said...

Hello Patrik,

I used your code and I did everything like you did, but in my sharepoint website it wont show the type of column. As I build my project it wasnt any errors and it was successfully builed. Can you please help me!!!

Patrik Luca said...

To be sure everything got installed correctly: can you check the following:

* Go to the 12 hive on your SharePoint server and go to the directory \TEMPLATE\XML: you should find a fldtypes_daxprojactivitytxt.xml file there.

* Go to your global assembly cache folder (normally it should be c:\Windows\assembly: you should find two assemblies there named <yourcompany>.SharePoint.DAX.CustomFields.DAXProjActivityTxt and <yourcompany>.SharePoint.DAX.DAXProjActivityTxt.CustomFields

Anonymous said...

Great solution, but when editing the meta data in Word (from the Document Information Panel), the field is greyed out (and it says it must be edited from server). Any idea how to make it work in Word?


Patrik Luca said...

Hi Jens,

I don't have immediately an idea, and haven't run (yet) into the problem. Does your lookup to the database has over 255 entries? Else I would suggest to let t point to a column with less values, just to see if the problem is related to it or not.

Carl said...

Great post Patrik thanks for the hard work. I've been looking for this for a long time, I'll check it out.


Office doc panels won't show custom field types by default other than as a greyed out text box. Simply rebuild the panel using MS InfoPath and create a secondary data source to access the BDC data using a drop down.


Patrik Luca said...

Thanks Carl: glad to hear this post is valuable info for you!

Carl said...

Hi Patrik.

I've tried this with what you have provided on the page but am receiving quite a bit of errors.

Do you by chance hapen to have the code available for download SVP?



Patrik Luca said...

I am sorry Carl: I don't have this code anymore: I started with it as described in these 3 posts, but I elaborated it further a lot, so my project contains lots of other stuff which makes it probably difficult to understand. Moreover, it is not in a stable state for the moment: I am in the middle of further development.

You receive quite a bit of errors: is it still upon compilation that you receive some stuff? Or after deployment (so during the tests)? Maybe you can clarify more about those errors you're receiving?

Anonymous said...

Hi Patrick,

Thanks for the great post.
I followed the steps mentioned in the post but i seem to get the following error when i select the option in the Document Library add columns page.

Any idea where im going wrong,

The file '/_layouts/CRMAccountControl' does not exist. at System.Web.UI.Util.CheckVirtualFileExists(VirtualPath virtualPath)
at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile)
at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile)
at System.Web.Compilation.BuildManager.GetVPathBuildResult(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile)
at System.Web.UI.TemplateControl.LoadControl(VirtualPath virtualPath)
at System.Web.UI.TemplateControl.LoadControl(String virtualPath)
at Microsoft.SharePoint.ApplicationPages.FieldCustomizationPage.CreateChildControls()
at Microsoft.SharePoint.ApplicationPages.FieldNewPage.CreateChildControls()
at System.Web.UI.Control.EnsureChildControls()
at Microsoft.SharePoint.ApplicationPages.FieldCustomizationPage.OnLoad(EventArgs e)
at Microsoft.SharePoint.ApplicationPages.FieldNewPage.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Patrik Luca said...

CRMAccountControl: is this the name of your Field Rendering Control?
If so, it seems as it is not correctly installed on your SharePoint server, as SharePoint seems not be able to find it.

Check the following:

* Go to the 12 hive on your SharePoint server and go to the directory \TEMPLATE\XML: you should find a fldtypes_daxprojactivitytxt.xml file there.

* Go to your global assembly cache folder (normally it should be c:\Windows\assembly: you should find two assemblies there named <yourcompany>.SharePoint.DAX.CustomFields.DAXProjActivityTxt and <yourcompany>.SharePoint.DAX.DAXProjActivityTxt.CustomFields

Anonymous said...

Thanks Patrik,

Now i get the error Unable to cast object of type 'ASP._controltemplates_crmaccountcontrol_ascx' to type 'Microsoft.SharePoint.WebControls.IFieldEditor'

Appreciate your help

Patrik Luca said...


it seems as you've created your own Field Rendering Template which is typically a .ascx file: in my example, I didn't. So I guess there is something wrong with your template...

You should have installed it in C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES\ on your SharePoint server.

Syed said...

Really Very nice post Patrik. I was basically looking for a similar scenario and this post has shown me a direction.I want to discuss with you my problem.
I need to add a field in EPM which is also integrated with Dynamics Ax. Just like we have Project Id, Project name, DAX_Task_Description etc. The field will be of a lookup of Project categories.
Please Patrik reply on this comment. Its really very urgent and important for me.Any type of help ans suggestion will be highly appreciated. I will try the code of this code and then will try to map it for my case. Just posting this comment so that you can guide me the best way.
looking forward for your response.

Patrik Luca said...

Hi Syed,

just following the guidelines in my 3 posts and you should get the result you want. Making a lookup on a project activity or on a project category is the same: it is only another table and SQL mapping you should apply: but that's the easiest part in the development of such functionality :-)

Patrik Luca, Ieper, BELGIUM
Feel free to use or spread all of the content on my blog. In return, linking back to my blog would be greatly appreciated. All my posts and articles are provided "AS IS" with no warranties.

Subscribe feeds via e-mail
Subscribe in your preferred RSS reader

Subscribe feeds rss Most Read Entries

Subscribe feeds rss Recent Entries


Recommended Books

Subscribe feeds rss Recent Comments

This Blog is part of the U Comment I Follow movement in blogosphere. Means the comment field of this blog is made DOFOLLOW. Spam wont be tolerated.

Blog Archive

My Blog List