SPSite siteCol = new SPSite(@"http://vm-moss2007/test/"); SPWeb site = siteCol.OpenWeb(); site.Lists.Add("New Calendar", "This calendar was created programmatically", site.ListTemplates["Calendar"]);
site.Lists.Add("My Custom List", "This list was created programmatically", site.ListTemplates["Custom List"]); SPList newList = site.Lists["My Custom List"]; newList.OnQuickLaunch = true; newList.Update();
newList.Fields.Add("NewText", SPFieldType.Text, true);
SPList targetList = site.Lists["My Lookup Target"]; SPField targetField = targetList.Fields["My Target Lookup Column"]; newList.Fields.AddLookup("NewLookup", targetList.ID, false); SPFieldLookup lkp = (SPFieldLookup)newList.Fields["NewLookup"]; lkp.LookupField = targetField.InternalName; lkp.Update();
//*** Doesn't work for some obscure reason *** newList.DefaultView.ViewFields.Add("NewText"); newList.DefaultView.ViewFields.Add("NewLookup"); newList.DefaultView.Update();
PView view = newList.DefaultView; view.ViewFields.Add("NewText"); view.ViewFields.Add("NewLookup"); view.Update();