2

I'm creating an SPList item programatically. Everything goes fine, I add a list, I add spfields to it.

SPList list = (SPList)oWeb.Lists[listName];

SPField libraryNameField = new SPField(list.Fields, "Library", Names.LIBRARY_NAME);
libraryNameField.Type = SPFieldType.Text;
libraryNameField.Required = true;
list.Fields.Add(libraryNameField);

But I encounter a problem. The fields are created, but not viewed on default view. What am I missing? How can I achieve that programatically?

1

You need to specifically add the column to the list view. For example,

//Get list object
SPList list = oWeb.Lists[listName];
//Get the view 
SPView defaultView = list.DefaultView;
//Add the desired fields
defaultView.ViewFields.Add(libraryNameField.Name);    
//Update the view
defaultView.Update();    
//Update the List -- Not entirely sure this step is necessary.
list.Update();

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.