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 am trying to modify a view for a list in a Feature Event Receiver. The list is created in the same function; and the columns all added to it. When I look in the List Settings via the web interface, the Fields are there, but the following code doesn't seem to work:

SPView view = list.Views[0];
view.ViewFields.Add("Value");
view.Update();

Going from the MSDN Documentation on the subject I really can't see what I'm doing wrong. No exceptions are being thrown, the list only has the one view, so... any thoughts?

share|improve this question

1 Answer 1

I usually retreive the View via it's ID like this:

 var view = list.Views[list.DefaultView.ID];
 view.ViewFields.Add("Value");
 view.Update();

Or like this:

SPView view = list.DefaultView;
view.ViewFields.Add("Value");
view.Update();

But to get it by Views[0] should work to.

What comes to mind is that the Field name may possibly be incorrect. Have you tried debugging your code to see if it breaks?

share|improve this answer
    
The field name is correct; if the field name is incorrect it throws an error (I tested this by changing the field name to something incorrect). –  TZHX Apr 11 '13 at 9:14
    
Great, and I assume you also do a list.Update() before adding the viewfields? –  Robert Lindgren Apr 11 '13 at 9:15
    
Using the field object directly (it is created earlier in the function), also doesn't work. –  TZHX Apr 11 '13 at 9:15
    
Yes; list.Update() is called numerous times before this point, and after. –  TZHX Apr 11 '13 at 9:16
2  
Did you check if list.View[0] is actually a visible html view of your list? –  Elitza Atanasova Apr 11 '13 at 9:37

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.