Assign SharePoint Item Picker control to External Data / BCS Column

Recently, I need to create visual webpart to display the item picker control like External Data / BCS column in list new or edit form.
Let’s go straight on how to assign the item picker as External Data column.

  • 1. Add Item Picker control in ascx file and initialize OnLoad attributes.
    <SharePoint:ItemPicker ID="myPicker" runat="server" OnLoad="myPicker_Load" CssClass="ms-input" />
    
  • 2. In ascx.cs file and OnLoad item picker events.
    protected void myPicker_Load(object sender, EventArgs e)
    {
     try
     {
      // Set extended data
      myPicker.ExtendedData = GetExtendedData("SystemInstanceName", "EntityName", "PrimaryColumnName", "EntityNamespace");
      // Set other properties
      myPicker.AllowTypeIn = true;
      myPicker.AllowEmpty = false;
      myPicker.AutoPostBack = false;
      myPicker.MultiSelect = false;
     }
     catch (Exception ex)
     {
      // Your error handler
     }
    }
    
    private Microsoft.SharePoint.WebControls.ItemPickerExtendedData GetExtendedData(string systemInstanceName, string entityname, string primaryColumnName, string entityNameSpace)
    {
     ItemPickerExtendedData extendedData = null;
     try
     {
      // Associate Network Affiliate Item Picker
      extendedData = new ItemPickerExtendedData();
      extendedData.SystemInstanceName = systemInstanceName;
      extendedData.EntityName = entityname;
      extendedData.PrimaryColumnName = primaryColumnName;
      extendedData.EntityNamespace = entityNameSpace;
     }
     catch (Exception ex)
     {
      // Your error handler
     }
     return extendedData;
    }
    

The next question is, how are we going to get these values “SystemInstanceName”, “EntityName”, “PrimaryColumnName”, “EntityNamespace” ?

  • 1. Create SharePoint list “MyList”, add new External Data column “MyField” to the list.
  • 2. Create simple SharePoint console application project and populate Main method like below.
    private static void Main(string[] args)
    {
     try
     {
      using (SPSite site = new SPSite("YourSiteURL"))
      {
       using (SPWeb web = site.OpenWeb("YourWeb"))
       {
        SPList listResult = web.Lists.TryGetList("MyList");
        if (listResult == null)
         return null;
    
        foreach (SPField eachField in listResult.Fields)
        {
         if (eachField != null && eachField.Title == "MyField" && eachField is SPBusinessDataField)
         {
          //Read in the field schema so that we can obtain extra values to perform a query against the BCS.
          XmlDocument xmlData = new XmlDocument();
          xmlData.LoadXml(eachField.SchemaXml);
    
          //This get all attributes required for External Data column
          string systemInstanceName = xmlData.FirstChild.Attributes["SystemInstance"].Value;
          string primaryColumnName = xmlData.FirstChild.Attributes["BdcField"].Value;
          string entityNameSpace = xmlData.FirstChild.Attributes["EntityNamespace"].Value;
          string entityname = xmlData.FirstChild.Attributes["EntityName"].Value;
          Console.WriteLine(string.Format("SystemInstanceName:{0}; PrimaryColumnName:{1}; EntityNamespace:{2}; EntityName:{3}", systemInstanceName, primaryColumnName, entityNameSpace, entityname));
         }
        }
       }
      }
     }
     catch (Exception ex)
     {
      Console.WriteLine("ERROR: " + ex.Message);
     }
     Console.ReadLine();
    }
    
Advertisements

Tagged: , , , , ,

3 thoughts on “Assign SharePoint Item Picker control to External Data / BCS Column

  1. Suhail Akhtar 06/06/2015 at 2:13 PM Reply

    HI,
    very useful information,

    Thank You…

  2. Suhail Akhtar 06/06/2015 at 2:30 PM Reply

    Getting External Data Column attributes dynamically , instead of hard coding..

    /*
    * My Documents = Your Document Library Name or ListName
    * Customer = Your External Data Column Name
    */

    SPBusinessDataField customer = SPContext.Current.Site.RootWeb.Lists[“My Documents”].Fields[“Customer”] as SPBusinessDataField;
    string sysInstanceName = customer.SystemInstanceName;
    string namespace = customer.EntityNamespace;
    string entityName = customer.EntityName;
    string primaryColumnName = customer.BdcFieldName;

    —————————————————————–
    try
    {
    // Set extended data
    myPicker.ExtendedData = GetExtendedData(sysInstanceName, entityName, primaryColumnName, namespace);
    // Set other properties
    myPicker.AllowTypeIn = true;
    myPicker.AllowEmpty = false;
    myPicker.AutoPostBack = false;
    myPicker.MultiSelect = false;
    }
    catch (Exception ex)
    {
    // Your error handler
    }

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: