I want to create a custom List programmatically from scratch:

  1. First I add:

    Guid listID = web.Lists.Add(title, description, SPListTemplateType.GenericList);
    list = web.Lists[listID];
  2. Then I want to create custom Fields specific to that List.

    I look at the constructors for SPField, and what do I see?

    public SPField(
        SPFieldCollection fields,
        string fieldName

    It's already awkward to know that I need to specify which SPFieldCollection will my SPField be in. Nevertheless I try the obvious way:

    SPFieldCollection fieldCollection = list.Fields;
    SPField newField = new SPField(fieldCollection, "Name");

And guess what happens then? An error occurs: Field with name "Name" was not found.

So basically if I want to create a Field, I need to have SPFieldCollection which will have such Field already within?!

So I'm asking: Is it actually possible to create (instantiate) SPField? (Apart from the AddFieldAsXml method)

1 Answer 1


There is another way to create a field without using AddFieldAsXml.

SPList lst = oSPWeb.Lists["EmpList"];
 /* create a Text field for Name */
 SPFieldText fldName = (SPFieldText)lst.Fields.CreateNewField(
                        SPFieldType.Text.ToString(), "Name");
 fldName.Required = true;
 fldName.MaxLength = 50;
/* add the new fields to the list */

Add Columns / Fields Programmatically to a SharePoint List

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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