2

I have list name students with 2 columns First Name and Last Name and has data in the list. I want to add a new column "class" to the students list. what to do in using visual studio.

Is it the right process or is there any better procedure to deal with the lists in SharePoint ??

1

You can add that column directly from the Sharepoint list itself enter image description here

However to add it using Visual Studio refer following code

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["student"];


      /* create a Class field */
        SPFieldText fldName = (SPFieldText)lst.Fields.CreateNewField(
                        SPFieldType.Text.ToString(), "Class");
        fldName.Required = true;
        fldName.MaxLength = 50;


       /* add the new fields to the list */
       lst.Fields.Add(fldName);

      /* finally update list */
        lst.Update();

        oSPWeb.AllowUnsafeUpdates = false;
    }

    oSPsite.AllowUnsafeUpdates = false;
}
0

You can create column in List without Visual Studio.

from GUI you can follow these steps:

  1. In the ribbon, click the List or Library tab.
  2. In the Manage Views group, click Create Column.
  3. In the Name and Type section, type the name that you want for the column in the Column name box.
  4. Under The type of information in this column is, select the type of information that you want to appear in the column.
  5. In the Additional Column Settings section, type a description in the Description box to help people understand the purpose of the column and what data it should contain. This description is optional.

Depending on the type of column that you selected, more options may appear in the Additional Column Settings section. Select the additional settings that you want.

  1. If you want the data in the column validated, click Column Validation to expand, and type the Formula that you want to use to validate the data, and type the User message that you want to provide users to help them type valid data. The Column Validation section is not available for all types of columns.
  2. Click OK.

https://support.office.com/en-us/article/Create-change-or-delete-a-column-in-a-list-or-library-2b0361ae-1bd3-41a3-8329-269e5f81cfa2?CorrelationId=2e73150a-13df-4a43-b0bc-f3dff0623168&ui=en-US&rs=en-US&ad=US

0

You can use SharePoint designer to quickly and easily add columns to a list, if you have it installed.

  1. Open your site in SharePoint Designer
  2. From the left click Lists and Libraries
  3. Select the relevant list
  4. Click Edit List Columns under the Customization section
  5. From the ribbon at the top choose Add New Column

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.