Monday, August 03, 2009

Create a Lookup field in SharePoint list

/// <summary>
/// Adds the lookup field to list.
/// </summary>
/// <param name="web">The web.</param>
/// <param name="list">The list.</param>
/// <param name="fieldName">Name of the field.</param>
/// <param name="lookupListName">Name of the lookup list.</param>
/// <param name="lookupFieldName">Name of the lookup field.</param>

SPField AddLookupFieldToList(SPWeb web, SPList list, string fieldName, string lookupListName, string lookupFieldName)
        {
            SPList destList = web.Lists[lookupListName];
            list.Fields.AddLookup(fieldName, destList.ID, required);
            SPFieldLookup field = list.Fields[fieldName] as SPFieldLookup;
            field.LookupField = destList.Fields[lookupFieldName].InternalName;
            field.Update(true);
            return field;
        }

0 comments: