Tag: Fixed List
Use Content by Query Web Part on a Fixed List created dynamic in an onet.xml
by mysticslayer on Nov.07, 2011, under features, Programming, SharePoint 2010
There is nothing as worse as a Content By Query Web Part that you can’t 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 functionality is used for exposing news of the Team or Department.
But when using the functionality like
<Property name=”WebUrl” type=”string”>~site/News</Property>
and using the property
<Property name=”ListName” type=”string”>Posts</Property>
and you removed the value of the property
<Property name=”ListGuid” type=”string”></Property>
it won’t render the CQWP web part by default. The reason is that the ListGuid is needed to render the Web Part.
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.
So basically I created the follow code which will fix that issue.
using System; using System.ComponentModel; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using Microsoft.SharePoint.Publishing; using Microsoft.SharePoint.Publishing.WebControls; namespace AK.Custom.CQWP { [ToolboxItemAttribute(false)] public class AKCQWP : ContentByQueryWebPart { protected override void OnLoad(EventArgs e) { if(!string.IsNullOrEmpty(this.WebUrl) && !string.IsNullOrEmpty(this.ListName) && string.IsNullOrEmpty(this.ListGuid)) { SPWeb webRoot = SPContext.Current.Site.OpenWeb(); if (this.WebUrl.StartsWith("~site")) { this.WebUrl = this.WebUrl.Replace("~site", ""); using(SPWeb web = SPContext.Current.Site.OpenWeb(webRoot.ServerRelativeUrl + this.WebUrl, true)) { SPList list = web.Lists[this.ListName]; this.ListGuid = list.ID.ToString(); //this.ListUrl = webRoot.ServerRelativeUrl + this.WebUrl; this.WebUrl = webRoot.ServerRelativeUrl + this.WebUrl; }; } else if(this.WebUrl.StartsWith("/News")) return; else { using(SPWeb web = SPContext.Current.Site.OpenWeb(this.WebUrl, true)) { SPList list = web.Lists[this.ListName]; this.ListGuid = list.ID.ToString(); this.WebUrl = this.WebUrl; }; } } base.OnLoad(e); } protected override void OnInit(EventArgs e) { base.OnInit(e); } protected override void CreateChildControls() { base.CreateChildControls(); } } }
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.
<metaData> <type name="AK.Custom.CQWP.AKCQWP, $SharePoint.Project.AssemblyFullName$" /> <importErrorMessage>$Resources:core,ImportErrorMessage;</importErrorMessage> </metaData>
Change the properties as following:
<Property name=”WebUrl” type=”string”>~site/News</Property> <Property name=”ListName” type=”string”>Posts</Property> <Property name=”ListGuid” type=”string”></Property>