{"id":776,"date":"2012-03-06T20:25:07","date_gmt":"2012-03-06T20:25:07","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=776"},"modified":"2012-03-06T20:25:07","modified_gmt":"2012-03-06T20:25:07","slug":"validation-viewstate-mac-failed-addwrkfl","status":"publish","type":"post","link":"https:\/\/www.stuartroberts.net\/index.php\/2012\/03\/06\/validation-viewstate-mac-failed-addwrkfl\/","title":{"rendered":"Validation of viewstate MAC failed in AddWrkfl"},"content":{"rendered":"<p>I recently added a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.webcontrols.delegatecontrol.aspx\" title=\"delegate control\" target=\"_blank\">delegate control<\/a> 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.<\/p>\n<p>Everything worked perfectly until I tried to add or update a workflow against a list.  After clicking <em>Next<\/em> on the add workflow page, the following error was thrown:<\/p>\n<p><em>Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster<\/em><br \/>\n<!--more--><br \/>\nAs the delegate control was working perfectly everywhere else in the site I didn&#8217;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.<\/p>\n<p>Now, removing them wasn&#8217;t an option for me, as they were necessary for the control to function.<\/p>\n<p>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 <em>_spFormOnSubmit<\/em> function.<\/p>\n<p>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.<\/p>\n<p>The solution to the problem?<\/p>\n<p>Replace the server side control<\/p>\n<pre lang=\"html\">\r\n<asp:HiddenField ID=\"hiddenField\" runat=\"server\" \/>\r\n<\/pre>\n<p>with the equivalent client control<\/p>\n<pre lang=\"html\">\r\n<input type=\"hidden\" name=\"hiddenField\" id=\"hiddenField\" \/>\r\n<\/pre>\n<p>Previously, the hidden field&#8217;s value was set by server side code in the <em>OnLoad<\/em> method.  As I&#8217;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:<\/p>\n<pre lang=\"csharp\">\r\npublic string CustomValue { get { return \"avalue\"; } }\r\n<\/pre>\n<pre lang=\"html\">\r\n<input type=\"hidden\" name=\"hiddenField\" id=\"hiddenField\" value=\"<%= CustomValue %>\"`\/>\r\n<\/pre>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/www.stuartroberts.net\/index.php\/2012\/03\/06\/validation-viewstate-mac-failed-addwrkfl\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[3,82],"tags":[81,42,6],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-cw","_links":{"self":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/776"}],"collection":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=776"}],"version-history":[{"count":8,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/776\/revisions"}],"predecessor-version":[{"id":916,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/776\/revisions\/916"}],"wp:attachment":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}