Wednesday, October 20, 2010

Making the content query web part deployable

My biggest issue with the CQWP? its not deployable when connected to a specific list in a specific site.
If you configure a CQWP web part to connect to a list in a site, the web part saves the ID (GUID) of the list - and if you want to deploy the web part as part of a feature or onet.xml so that the web part gets added every time a user creates a site, it will fail - because the ID of the list changes every time you create a new site.
My solution? override the web part and implement the following functions:
private void FixListGuid()
{
try
{

if (!string.IsNullOrEmpty(base.WebUrl) && !string.IsNullOrEmpty(base.ListName))
{
using (SPWeb web = SPContext.Current.Site.OpenWeb(base.WebUrl, true))
{
base.ListGuid = web.Lists[base.ListName].ID.ToString();
}
}
}
catch (Exception ex)
{
SPZetaContext.GetContext(HttpContext.Current).Log.Warn("Unable to resolve SPList '" + this.ListName + "'", ex);
}
}
protected override void OnLoad(EventArgs e)
{
FixListGuid ();
base.OnLoad(e);
}
}

This code works for me..

How to create custom Content Query Web Part


Thanks!
Avinash

No comments: