private void CreateList(SPWeb web)
{
SPListCollection lists = web.Lists;
SPList list = lists.TryGetList("Child List");
if (list == null)
{
lists.Add("Child List", "Example child list " , SPListTemplateType.GenericList);
list = web.Lists["Child List"];
list.OnQuickLaunch = true;
#region CreatingListFields
if (list != null)
{
SPFieldText txtField = null;
// Rename Title Field
txtField = (SPFieldText)list.Fields["Title"];
if (txtField != null)
{
txtField.Title = "Text Field";
txtField.Update();
}
// Creating a lookup field
SPList targetList= web.Lists.TryGetList("Parent List");
if (targetList != null)
{
list.Fields.AddLookup("Child Lookup Field Name", targetList.ID, false);
SPFieldLookup lookup = (SPFieldLookup)list.Fields["Child Lookup Field Name"];
lookup.LookupField = targetList.Fields["ParentListFieldName"].InternalName;
lookup.AllowMultipleValues = false;
lookup.Update();
}
#endregion
// Add a default View to the List
#region DefaultView
SPView view = list.DefaultView;
view.RowLimit = 30;
view.Title = "All Items";
view.Update();
list.Update();
#endregion
}
}
No comments:
Post a Comment