Thursday, March 4, 2010

Adding Columns to a List Programatically

Add Columns / Fields Programmatically to a SharePoint List You can think of a SharePoint List somewhat similar to a table present within a Database. As tables consist of different columns, a SharePoint List also comprises of different columns / fields and these fields can indeed have different attributes associated to it (like "Required field check", "Max Length", "Display Formats", etc). We can use SharePoint API to create these fields and associate the corresponding attributes programmatically within an existing List. Provided below is a code snippet in C#.Net describing the same. using (SPSite oSPsite = new SPSite("http://Web-URL")) { oSPsite.AllowUnsafeUpdates = true; using (SPWeb oSPWeb = oSPsite.OpenWeb()) { oSPWeb.AllowUnsafeUpdates = true; /* get the SPList object by list name*/ SPList lst = oSPWeb.Lists["EmpList"]; /* create a Numeric field for EmpID */ SPFieldNumber fldEmpID = (SPFieldNumber)lst.Fields.CreateNewField( SPFieldType.Number.ToString(), "EmpID"); fldEmpID.Required = true; fldEmpID.DisplayFormat = SPNumberFormatTypes.NoDecimal; /* create a Text field for Name */ SPFieldText fldName = (SPFieldText)lst.Fields.CreateNewField( SPFieldType.Text.ToString(), "Name"); fldName.Required = true; fldName.MaxLength = 50; /* create a Date field for Dob*/ SPFieldDateTime fldDob = (SPFieldDateTime)lst.Fields.CreateNewField( SPFieldType.DateTime.ToString(), "Dob"); fldDob.DisplayFormat = SPDateTimeFieldFormatType.DateOnly; /* create a Currency field for Salary */ SPFieldCurrency fldSal = (SPFieldCurrency)lst.Fields.CreateNewField( SPFieldType.Currency.ToString(), "Salary"); fldSal.Currency = SPCurrencyFieldFormats.UnitedStates; fldSal.DisplayFormat = SPNumberFormatTypes.TwoDecimals; /* add the new fields to the list */ lst.Fields.Add(fldEmpID); lst.Fields.Add(fldName); lst.Fields.Add(fldDob); lst.Fields.Add(fldSal); /* finally update list */ lst.Update(); oSPWeb.AllowUnsafeUpdates = false; } oSPsite.AllowUnsafeUpdates = false; } Adding Hyperlink Column to a SharePoint List Example 1: Set the url field of a link Use the SPFieldUrlValue class to create an object that holds the url to link to, and the title to display: SPList list = web.Lists["Links"]; SPListItem newLink = list.Items.Add(); SPFieldUrlValue value = new SPFieldUrlValue(); value.Description = "Click Me"; value.Url = ""; newLink["URLColumn"] = value; //URLColumn that we have added on the list newLink.Update(); Example 2: Get the url field of a link Use the SPFieldUrlValue class to create an object that gets the url and description: SPList list = web.Lists["Links"]; SPListItem existingLink = list.Items[0]; SPFieldUrlValue value = new SPFieldUrlValue(existingLink["URL"].ToString()); string linkTitle = value.Description; string linkURL = value.Url; Example 3: Add Choice Column to a List or Document Library programatically Considering lstCustomList is your list object and you have set AllowUnsafeUpdate() to true for the web object. We are adding two choices in the choice column. First we will add choice column itself to the list and then we will add the choices in that column and update the field. The main point to taken in to consideration is that it is SPFieldChoice not the SPField. lstCustomList.Fields.Add("ABC", SPFieldType.Choice, false); lstCustomList.Update(); SPFieldChoice objChoiceCol = (SPFieldChoice)lstCustomList.Fields["ABC"]; string[] strdata = new string[2]; strdata[0] = "Open"; strdata[1] = "Close"; objChoiceCol.Choices.Add(strdata[0]); objChoiceCol.Choices.Add(strdata[1]); objChoiceCol.Update(); lstCustomList.Update();

No comments: