Hi Just wanted to get some tips on SharePoint List created with the following code?
I created a helper method (UpdateDisplayName())to update the display name of the columns created is there anything wrong with passing the SPList by ref? I know this implies that you can alter the list.
But I thought it would be "faster" by reference since it's object type.
If anyone can suggest a better way of doing this, I would love to know?
Thanks.
private void btnCreateProductList_Click(object sender, EventArgs e)
{
using (SPSite siteCollection = new SPSite(siteUrl))
{
SPWeb web = siteCollection.RootWeb;
Guid listId = web.Lists.Add("Products", "A list of products", SPListTemplateType.GenericList);
SPList list = web.Lists[listId];
list.OnQuickLaunch = true;
list.Update();
// Modify display name of the Title field.
SPField titleField = list.Fields.GetFieldByInternalName("Title");
titleField.Title = "Category";
titleField.Update();
// add custom fields.
var productDescription = list.Fields.Add("ProductDescription", SPFieldType.Note, false);
var inventoryLevel = list.Fields.Add("InventoryLevel", SPFieldType.Integer, true);
var listPrice = list.Fields.Add("ListPrice", SPFieldType.Currency, true);
UpdateDisplayName(ref list, productDescription, "Product Description");
UpdateDisplayName(ref list, inventoryLevel, "Inventory Level");
UpdateDisplayName(ref list, listPrice, "List Price");
SPView defaultView = list.DefaultView;
defaultView.ViewFields.Add(productDescription);
defaultView.ViewFields.Add(inventoryLevel);
defaultView.ViewFields.Add(listPrice);
defaultView.Update();
lstResults.Items.Add("Products list created");
}
}
private void UpdateDisplayName(ref SPList myList,string fieldName, string newName)
{
SPField field = myList.Fields.GetFieldByInternalName(fieldName);
field.Title = newName;
field.Update();
}