Peterson Yap

Peterson Yap

  • NA
  • 35
  • 66.9k

Creating Default View and Adding Default columns to Default View

Oct 5 2010 2:11 AM
Hi, as mentioned, i am already able to create a default view using the below code: but i would like to add default columns like  <> and <ID> to this view. How can i do it? i can only add columns i have added programmatically in my codes, what about system maintained codes like ID? and Name (File Name)??? Please Help!

Thanks!

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();


Answers (2)