0

I add a new column to a SharePoint list (the list is built on a content type) like this:

        SPField textfield = list.Fields.CreateNewField(SPFieldType.Text.ToString(), "new column");
        list.Fields.Add(textfield);            
        list.Update();

        //the new field must be editable!
        SPField newField = list.Fields["new column"];
        newField.ShowInEditForm = true;
        newField.ShowInViewForms = true;
        newField.ShowInDisplayForm = true;
        newField.ShowInNewForm = true;
        newField.Update();

        //update the view
        SPView view = list.DefaultView;
        view.ViewFields.Add(language);
        view.Update(); 

The new column appears in the default view of the list and everything appears to work just fine. However, when I try to add a new item or edit an existing item, my new column doesn't appear in the form. A quick look in PowerShell reveals that "CanBeShowInEditForm" is set to true. What am I missing here?

1 Answer 1

1

Your missing adding the field as fieldlink to the Lists version for the content type

var ct = list.ContentTypes["NameOfCT"];
var fieldLink = new SPFieldLink(newField);
ct.FieldLinks.Add(fieldLink);
ct.Update()

When you're using content types in a list only the fields corresponding to the content type is shown in forms. But all fields can be shown in views.

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.