Add lookup field for our newly created list.
This is a sample code.
public void
createList()
{
// choose your
site
SPSite site = new
SPSite("http://merdev-moss:5050");
SPWeb web = site.OpenWeb();
SPListCollection lists = web.Lists;
// create new Generic list called "My List"
lists.Add("My List", "My list Description", SPListTemplateType.GenericList);
SPList newList = web.Lists["My List"];
// create Text
type new column called "My Column"
newList.Fields.Add("My
Column", SPFieldType.Text, true);
/*create lookup
type new column called "Lookup Column"
* Here I am going to get the information
from the "Title"
* column of a list called "User
Roles"
*/
SPList targetList = web.Lists["User Roles"];
newList.Fields.AddLookup("Lookup Column", targetList.ID, false);
SPFieldLookup lkp =
(SPFieldLookup)newList.Fields["Lookup
Column"];
lkp.LookupField = targetList.Fields["Title"].InternalName;
lkp.Update();
// make new
column visible in default view
SPView view = newList.DefaultView;
view.ViewFields.Add("My Column");
view.ViewFields.Add("Lookup Column");
view.Update();
}
diego sanchezPosted Dec 14, 2017, 2:07 AM
I have a created list in Infopath. How to import progammmatically?
sai saranPosted Sep 7, 2017, 6:52 AM
Can u specify the namespaces for above code
Shandeep BaalajiPosted Mar 22, 2016, 2:37 AM
Thanks for sharing....
Mahesh ChandPosted Oct 12, 2010, 8:01 AM
Good start Suresh. Thank you for sharing.