Thursday, March 18, 2010

Creating Lookup, Multi-Value Fields in SharePoint

Because lookup fields are not included when you save a list as template in SharePoint, you might need to create custom code to programmatically add these fields to a list during site provisioning. Here's how:

SPSite site = new SPSite (siteURL);
SPWeb web = site.OpenWeb();
SPList departmentList = web.Lists["Department"];
SPList employeeList = web.Lists["Employees"];
employeeList.Fields.AddLookup("Department", departmentList.ID, false);

Lookup field in this case is the Department field which was added to the Employee list. Afterwards, you may set this field as a multi-value field:

SPFieldLookup categoryField = (SPFieldLookup)eLibrary.Fields[fldName];
categoryField.AllowMultipleValues = true;
categoryField.Update();

No comments:

Post a Comment