{"id":215,"date":"2011-11-07T17:34:31","date_gmt":"2011-11-07T16:34:31","guid":{"rendered":"http:\/\/www.mysticslayer.com\/?p=215"},"modified":"2011-11-07T17:37:06","modified_gmt":"2011-11-07T16:37:06","slug":"use-content-by-query-web-part-on-a-fixed-list-created-dynamic-in-an-onet-xml","status":"publish","type":"post","link":"http:\/\/www.mysticslayer.com\/?p=215","title":{"rendered":"Use Content by Query Web Part on a Fixed List created dynamic in an onet.xml"},"content":{"rendered":"<p>There is nothing as worse as a Content By Query Web Part that you can\u2019t use when you want to fix it against a List which will be made automatically for example.<\/p>\n<p>I would like to create a Team Site based on a Custom Web template with an extra sub-site with blogging functionality. This blogging functionality is used for exposing news of the Team or Department.<\/p>\n<p>But when using the functionality like <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">&lt;Property name=\u201dWebUrl\u201d type=\u201dstring\u201d&gt;~site\/News&lt;\/Property&gt;<\/pre>\n<p> and using the property <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">&lt;Property name=\u201dListName\u201d type=\u201dstring\u201d&gt;Posts&lt;\/Property&gt;<\/pre>\n<p> and you removed the value of the property <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">&lt;Property name=\u201dListGuid\u201d type=\u201dstring\u201d&gt;&lt;\/Property&gt;<\/pre>\n<p> it won\u2019t render the CQWP web part by default. The reason is that the ListGuid is needed to render the Web Part.<\/p>\n<p>But also an issue is that, when your list is created it has a changed ListGuid. The ListGuid is never the same when you create a new List.<\/p>\n<p>So basically I created the follow code which will fix that issue.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing System;\r\nusing System.ComponentModel;\r\nusing System.Web;\r\nusing System.Web.UI;\r\nusing System.Web.UI.WebControls;\r\nusing System.Web.UI.WebControls.WebParts;\r\nusing Microsoft.SharePoint;\r\nusing Microsoft.SharePoint.WebControls;\r\nusing Microsoft.SharePoint.Publishing;\r\nusing Microsoft.SharePoint.Publishing.WebControls;\r\n\r\nnamespace AK.Custom.CQWP \r\n{\r\n    &#x5B;ToolboxItemAttribute(false)]\r\n    public class AKCQWP : ContentByQueryWebPart\r\n    {        \r\n\r\n        protected override void  OnLoad(EventArgs e)\r\n        {\r\n \t        if(!string.IsNullOrEmpty(this.WebUrl) &amp;&amp; \r\n                !string.IsNullOrEmpty(this.ListName) &amp;&amp; \r\n                string.IsNullOrEmpty(this.ListGuid))\r\n            {\r\n                SPWeb webRoot = SPContext.Current.Site.OpenWeb();\r\n                if (this.WebUrl.StartsWith(&quot;~site&quot;))\r\n                {\r\n                    this.WebUrl = this.WebUrl.Replace(&quot;~site&quot;, &quot;&quot;);\r\n                    using(SPWeb web = SPContext.Current.Site.OpenWeb(webRoot.ServerRelativeUrl \r\n+ this.WebUrl, true))\r\n                    {\r\n                        SPList list = web.Lists&#x5B;this.ListName];\r\n                        this.ListGuid = list.ID.ToString();\r\n                        \/\/this.ListUrl = webRoot.ServerRelativeUrl + this.WebUrl;\r\n                        this.WebUrl = webRoot.ServerRelativeUrl + this.WebUrl;\r\n                    };\r\n                    \r\n                }\r\n                else if(this.WebUrl.StartsWith(&quot;\/News&quot;))\r\n                    return;\r\n                else\r\n                {\r\n                    using(SPWeb web = SPContext.Current.Site.OpenWeb(this.WebUrl, true))\r\n                    {\r\n                        SPList list = web.Lists&#x5B;this.ListName];\r\n                        this.ListGuid = list.ID.ToString();\r\n                        this.WebUrl = this.WebUrl;\r\n                    };\r\n                }\r\n                \r\n            }\r\n            base.OnLoad(e);\r\n\r\n            \r\n        }\r\n\r\n        protected override void OnInit(EventArgs e)\r\n        {\r\n            base.OnInit(e);\r\n        }\r\n\r\n        protected override void CreateChildControls()\r\n        {\r\n            base.CreateChildControls();\r\n        }\r\n    }\r\n}\r\n\r\n<\/pre>\n<p>When you have created this part of your solution ensure that you Export the standard CQWP and edit it like you want to. First of all, what you need is to ensure that it takes your assembly instead of the default CQWP assembly.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n    &lt;metaData&gt;\r\n      &lt;type name=&quot;AK.Custom.CQWP.AKCQWP, $SharePoint.Project.AssemblyFullName$&quot; \/&gt;\r\n      &lt;importErrorMessage&gt;$Resources:core,ImportErrorMessage;&lt;\/importErrorMessage&gt;\r\n    &lt;\/metaData&gt;\r\n<\/pre>\n<p>Change the properties as following:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;Property name=\u201dWebUrl\u201d type=\u201dstring\u201d&gt;~site\/News&lt;\/Property&gt;\r\n&lt;Property name=\u201dListName\u201d type=\u201dstring\u201d&gt;Posts&lt;\/Property&gt;\r\n&lt;Property name=\u201dListGuid\u201d type=\u201dstring\u201d&gt;&lt;\/Property&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There is nothing as worse as a Content By Query Web Part that you can\u2019t use when you want to fix it against a List which will be made automatically for example. I would like to create a Team Site based on a Custom Web template with an extra sub-site with blogging functionality. This blogging [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[277,101,224],"tags":[341,340,346,343,344,342,345],"class_list":["post-215","post","type-post","status-publish","format-standard","hentry","category-features","category-programming","category-sharepoint-2010","tag-content-by-query-webpart-onet","tag-cqwp","tag-fixed-list","tag-listguid","tag-listname","tag-onet-xml","tag-weburl"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pe1EH-3t","_links":{"self":[{"href":"http:\/\/www.mysticslayer.com\/index.php?rest_route=\/wp\/v2\/posts\/215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.mysticslayer.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.mysticslayer.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.mysticslayer.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.mysticslayer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=215"}],"version-history":[{"count":11,"href":"http:\/\/www.mysticslayer.com\/index.php?rest_route=\/wp\/v2\/posts\/215\/revisions"}],"predecessor-version":[{"id":225,"href":"http:\/\/www.mysticslayer.com\/index.php?rest_route=\/wp\/v2\/posts\/215\/revisions\/225"}],"wp:attachment":[{"href":"http:\/\/www.mysticslayer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mysticslayer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mysticslayer.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}