I recently added a delegate control to a SharePoint solution. The delegate control was used to dynamically insert a custom user control into the master page. Part of the functionality of this control required the use of hidden fields.
Everything worked perfectly until I tried to add or update a workflow against a list. After clicking Next on the add workflow page, the following error was thrown:
Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that
As the delegate control was working perfectly everywhere else in the site I didn’t immediately think to look at that control for a solution. However, I did eventually discover that the HiddenField controls I was using were part of the problem. Removing them allowed the page to progress to the next one without error.
Now, removing them wasn’t an option for me, as they were necessary for the control to function.
Looking at the SharePoint page AddWrkfl.aspx (located in the layouts folder), I noticed that it was making changes to the form and setting the __viewstate field to an empty string within the _spFormOnSubmit function.
This made me think that the AddWrkfl page must be causing the server side HiddenField controls I was using to generate the viewstate MAC error.
The solution to the problem?
Replace the server side control
<asp:HiddenField ID="hiddenField" runat="server" /> |
with the equivalent client control
<input type="hidden" name="hiddenField" id="hiddenField" /> |
Previously, the hidden field’s value was set by server side code in the OnLoad method. As I’d changed them to client controls this was no longer possible, so I added public properties to the user control which would provide the required values to the client controls:
public string CustomValue { get { return "avalue"; } } |
<input type="hidden" name="hiddenField" id="hiddenField" value="<%= CustomValue %>"`/> |
This will not be a solution for every scenario but hopefully it helps some of you if you encounter this. The server side code in my control did not require to read values from the hidden fields, so having them not present in the viewstate on postback was not an issue for me.