In this article we will be seeing about projected fields in SharePoint 2010. Introduction: SharePoint 2010 enables you to display additional columns from the target list in the view of the list that contains the lookup column. These additional generated columns are known as projected fields. I have created two lists "Departments" and "Employees to model the relationship between them. I have created a Department lookup column for the Employees list and I also want to display the name of the department manager in the list of employees, as shown in the following illustration.
Departments List: Employees List: In the "Employees" list I have created a Department lookup column as shown in the following You could be able to see the projected field "Department: Manager" as shown in the following Using SharePoint Object Model:
namespace ProjectedFields { class Program { static void Main(string[] args) { using (SPSite site = new SPSite("http://serverName:22222/sites/test")) { using (SPWeb web = site.RootWeb) { SPList lookupList = web.Lists.TryGetList("Departments"); SPList list = web.Lists.TryGetList("Employees");
if (lookupList != null && list != null) { // Create the lookup column. string strPrimaryColumn = list.Fields.AddLookup("Department", lookupList.ID, true); SPFieldLookup primaryColumn = (SPFieldLookup)list.Fields.GetFieldByInternalName(strPrimaryColumn); primaryColumn.LookupField = lookupList.Fields["Name"].InternalName; primaryColumn.Indexed = true; primaryColumn.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.Restrict; primaryColumn.Update();
// Projected Field string strProjectedCol = list.Fields.AddDependentLookup("LookUp:Manager", primaryColumn.Id); SPFieldLookup projectedCol = (SPFieldLookup)list.Fields.GetFieldByInternalName(strProjectedCol); projectedCol.LookupField = lookupList.Fields["Manager"].InternalName; projectedCol.Update();
// Add the columns to the "All Items" view SPView view = list.Views["All Items"]; view.ViewFields.Add(primaryColumn); view.ViewFields.Add(projectedCol); view.Update(); } } } } } }
Using powershell:
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: