Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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();
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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