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

so I have a custom webpart on both the DispForm.aspx and EditForm.aspx which, on page load gets a list of all the fields in a SharePoint list and loops through each field and sets its display property to be true for both the DispForm.aspx and EditForm.aspx pages. The problem is when I try to edit a list item and save the changes I get an error. The error and code sample is show below:

Error: [SPException: The settings for this list have been recently changed. Refresh your browser before editing this list.]

Code:

 protected void Page_Load(object sender, EventArgs e)
 {
   using (SPSite site = new SPSite("http://labsp001"))
   {
     if (site != null)
     {
       using (SPWeb web = site.OpenWeb())
       {
         var mainList = web.Lists["Main List"];
         web.AllowUnsafeUpdates = true;
         web.Update();

         var spFieldCol = mainList.Fields.Cast<SPField>().ToList();
         if (spFieldCol != null)
         {
           foreach (SPField fieldItem in spFieldCol)
           {
             if (field != null)
             {
               field.ShowInDisplayForm = true;
               field.ShowInEditForm = true;
               field.Update();
               field.ParentList.Update();
             }
           }
         }
         web.AllowUnsafeUpdates = false;
         web.Update();
       }

     }
   }
 }
share|improve this question

2 Answers 2

up vote 0 down vote accepted

Ok so from debugging the solution it seems that while the FormMode is in Edit mode you cannot call on the Update method for the list. The code works fine in display mode on the DispForm.aspx page and it will hide and show the correct fields in the EditForm.aspx page but when you click Save that is when the error is thrown. If the Update method is removed the error will not be thrown however you will not be able to see the changes to the fields. I actually found this out by trying another route which is actually hiding the rendering control on the page instead of the actual SP Field item. You can follow the steps on this link to see how to manipulate the actual controls that are rendered on the form: link text

share|improve this answer

I think the problem is you're calling List.Update multiple times.

you might be better having an "IsListUpdated" flag

ie.

bool IsListUpdated = false;

foreach (SPField fieldItem in spFieldCol)
           {
             if (field != null)
             {
               field.ShowInDisplayForm = true;
               field.ShowInEditForm = true;
               field.Update();
               isListUpdated=true;
             }
           }
if (IsListUpdated)
    mainList.Update();
share|improve this answer
    
Actually, just thinking about it, do you even need to update the list at all? It might be enough just to update the fields. –  Neil Richards Nov 18 '10 at 0:26
    
Well I had the list update outside the loop and it still doesn't work. Since I am changing the display settings on the list doesn't that warrant an update on the list itself? Well I tried without the list update and with the update outside the loop and still the same error. Shouldn't something like this be possible? –  Peter T Nov 18 '10 at 19:28
    
Have you tried without updating the list? I'm just not convinced you need the mainList.Update() –  Neil Richards Nov 19 '10 at 7:30
    
Hey Neil, yes I have tried without updating the list, mainList and it still gave the error. The problem seems to stem from the fact that you can only modify the list if you are not accessing the item at that point. If this code was to be used in an event reciever for item added or when the list is created it would work, however I need the functionality to show/hide the fields when a particular user views or edits the item. –  Peter T Nov 19 '10 at 19:10
    
Can you try something for me? Create a console app that updates the list in the way described above, in isolation of editing the specific list item. Just keen to understand whether the problem is related to your other activities. One other thing I might try is to control the display of fields via the UI, but that's clearly not appropriate if you have some security restriction (is this the case?) –  Neil Richards Nov 22 '10 at 7:25

Your Answer

 
discard

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