{"id":1602,"date":"2013-06-22T17:08:12","date_gmt":"2013-06-22T15:08:12","guid":{"rendered":"http:\/\/www.itidea.nl\/?p=1602"},"modified":"2013-06-22T17:08:12","modified_gmt":"2013-06-22T15:08:12","slug":"content-by-query-webpart","status":"publish","type":"post","link":"https:\/\/www.itidea.nl\/index.php\/content-by-query-webpart\/","title":{"rendered":"Content By Query webpart"},"content":{"rendered":"<p>Recently I was fixing a bug which occurred at a custom webpart derived from the Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart.<\/p>\n<p>The custom webpart has an EditorPart where the user is able to set some properties and after configuration the webpart shows an item from a list with custom formatting. Not too exciting.<\/p>\n<p>The customer reported an error occurred every time the properties where set and he selected &#8216;Apply&#8217; and &#8216;Save&#8217; or &#8216;Publish&#8217;. After returning from the error page to the page with the webpart on it he pressed &#8216;Save&#8217; or &#8216;Publish&#8217; again and the page was saved or published successfully.<\/p>\n<p>The first thing is to start analyzing the ULS log messages belonging to the correlation id which belongs to the exception. The first unexpected exception:<\/p>\n<blockquote><p>&#8216;Creating object wrapper, expecting major version, but no major version given.\u00a0 URL is \/xxxx&#8217;<\/p><\/blockquote>\n<p>followed closely by the message<\/p>\n<blockquote><p>&#8216;Trying to store a checked out item (\/xxx.ASPX) in the object cache.\u00a0 This may be because the checked out user is accessing the page, or it could be that the SharePoint system account has the item checked out.\u00a0 To improve performance, you should set the portalsuperuseraccount property on the web application&#8217;<\/p><\/blockquote>\n<p>Three things to check:<\/p>\n<ol>\n<li>the checked out user is      accessing the page &#8211; well yes, that will be me configuring the page<\/li>\n<li>the SharePoint system account      has the item checked out &#8211; no<\/li>\n<li>set the      portalsuperuseraccount property on the web application &#8211; I already did, so      no<\/li>\n<\/ol>\n<p>And a lot of &#8216;ConsoleUtilies.GetContextualControlMode had no currentPage so the current SPWebPartManager mode cannot be retrieved.&#8217; messages.<\/p>\n<p>The categories these messages came from where Publishing and Publishing Cache, so I checked some of the cache policies applied to the site. To be sure it didn&#8217;t had to do anything with the cache configured in the solution, I moved the webpart to a clean SharePoint installation, but the same exception did occur.<\/p>\n<p>The next unexpected exception:<\/p>\n<blockquote><p>&#8216;System.Web.HttpException: Failed to load viewstate.\u00a0 The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.\u00a0 For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.&#8217;<\/p><\/blockquote>\n<p>Ah, the viewstate was changed during the postback. But I couldn&#8217;t find anything in the code which would be responsible for that, except&#8230;<\/p>\n<p>The QueryOverride property was set in the EditorPart when configuring the webpart and this property wasn&#8217;t available in the .webpart file.<\/p>\n<p>When the QueryOverride property is set SharePoint adds a new label to the toolpane with the message &#8216;Some properties in this Web Part are not available because they are configured to have fixed values&#8217;:<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2013\/06\/CQWPMessageQueryOverride.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1610 alignnone\" title=\"CQWPMessageQueryOverride\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2013\/06\/CQWPMessageQueryOverride.png\" alt=\"\" width=\"305\" height=\"46\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2013\/06\/CQWPMessageQueryOverride.png 305w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2013\/06\/CQWPMessageQueryOverride-300x45.png 300w\" sizes=\"auto, (max-width: 305px) 100vw, 305px\" \/><\/a><\/p>\n<p>The message is fine, because that&#8217;s the purpose of the QueryOverride property, but the label changed the viewstate, because the QueryOverride wasn&#8217;t set at the initial loading state of the webpart, and occurred the exception.<\/p>\n<p>To solve this issue the QueryOverride property has to be set before the first time the viewstate is loaded. Afterwards it can be changed to another value, which will happen because this webpart changes the QueryOverride when a user changes one of the properties of the webpart.<\/p>\n<p>To set the QueryOverride property before the viewstate is loaded there are two option: set the property in the OnInit event or add the property to the .webpart definition file.<\/p>\n<h4>OnInit event<\/h4>\n<p>Only set a sort of dummy property value when the QueryOverride property is empty.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nprotected override void OnInit(EventArgs e)\r\n{\r\n  base.OnInit(e);\r\n\r\n  string baseOverride = &quot;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name='ID'\/&gt;&lt;Value Type='Counter'&gt;0&lt;\/Value&gt;&lt;\/Eq&gt;&lt;\/Where&gt;&quot;;\r\n  if (this.QueryOverride.Equals(string.Empty))\r\n  {\r\n    this.QueryOverride = baseOverride;\r\n  }\r\n}\r\n<\/pre>\n<h4>Webpart definition file<\/h4>\n<p>Add a dummy QueryOverride property to the xml file.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;property name=&quot;QueryOverride&quot; type=&quot;string&quot;&gt;&amp;lt;Where&amp;gt;&amp;lt;Eq&amp;gt;&amp;lt;FieldRef Name='ID'\/&amp;gt;&amp;lt;Value Type='Counter'&amp;gt;0&amp;lt;\/Value&amp;gt;&amp;lt;\/Eq&amp;gt;&amp;lt;\/Where&amp;gt;&lt;\/property&gt;\r\n<\/pre>\n<h3>Summary<\/h3>\n<p>Sometimes the content query webpart is somewhat inconvenient and you&#8217;ll have to implement some fixes to get around it.<\/p>\n<p>Of the two possible solutions provided for this inconvenience there is no &#8216;best one&#8217;, but I prefer using the OnInit method, because developers including myself, are most of the time investigating the code before the accompanying definition files.<\/p>\n<p>Both solutions add the label<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2013\/06\/CQWPMessageQueryOverride.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1610 alignnone\" title=\"CQWPMessageQueryOverride\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2013\/06\/CQWPMessageQueryOverride.png\" alt=\"\" width=\"305\" height=\"46\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2013\/06\/CQWPMessageQueryOverride.png 305w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2013\/06\/CQWPMessageQueryOverride-300x45.png 300w\" sizes=\"auto, (max-width: 305px) 100vw, 305px\" \/><\/a><\/p>\n<p>to the toolpane right away and the viewstate won&#8217;t change during a postback.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I was fixing a bug which occurred at a custom webpart derived from the Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart. The custom webpart has an EditorPart where the user is able to set some properties and after configuration the webpart shows an item from &#8230; <a class=\"more-link\" href=\"https:\/\/www.itidea.nl\/index.php\/content-by-query-webpart\/\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[20,42,6],"class_list":["post-1602","post","type-post","status-publish","format-standard","hentry","category-sharepoint-2010","tag-c","tag-sharepoint-2010","tag-visual-studio"],"_links":{"self":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1602","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/comments?post=1602"}],"version-history":[{"count":16,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1602\/revisions"}],"predecessor-version":[{"id":1620,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1602\/revisions\/1620"}],"wp:attachment":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/media?parent=1602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/categories?post=1602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/tags?post=1602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}