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.
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) vbc
{
//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();
}
No comments:
Post a Comment