Wednesday, September 10, 2008

Modifying Content Query Webpart programatically

Scenario:
You want to move your CQWP from one environment to another. Suggested way is to configure them in DEV environment and Export them. But painful aspect of this process is that with most of the Out of Box webparts they carry LISTGUIDs which are not available in next environment.

Steps:
1. Configure CQWP with filters and List you want to attach
2. Export the WEBPART file and open it using Visual Studio or Notepad
3. Look for ListGuid property and delete it all together.( Look for remarks )
4. Now Import it in new environment
5. Set List either manually in new environment or Set it programatically as shown , i created a layout page to configure the webparts )

ConfigureCQWP.ASPX ( 12/Template/Layouts/Client/):

<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, 
Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master" Inherits="Microsoft.SharePoint.WebControls.LayoutsPageBase" %>

<%@ Import Namespace="Microsoft.SharePoint" %>

<script runat="server">
protected override void OnLoad(EventArgs e)
{

try
{
SPWeb web= SPContext.Current.Web;
lblSiteTitle.Text = site.Title;

string sDocumentLibraryName = "Shared Documents";
Guid listID = default(System.Guid);

bool bFound = false;

foreach (SPList list in web.Lists)
{
if (list.Title.ToLower() == sDocumentLibraryName.ToLower())
{
listID = list.ID;
bFound = true;
break;
}
}

if (bFound)
{

web.AllowUnsafeUpdates = true;

Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager WPMgr = web.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared);

foreach (WebPart wp in WPMgr.WebParts)
{
ContentByQueryWebPart CQWPart = wp as ContentByQueryWebPart;
if (CQWPart != null)
{
CQWPart.WebUrl = site.ServerRelativeUrl;
WPMgr.SaveChanges(CQWPart);
//CQWPart.ListName = sDocumentLibraryName;
//CQWPart.ListGuid = listID;
//CQWPart.Title = "NewP";
}
}
lblSiteStatus.Text = "Configured Sucessfully...";
}
else
{
lblSiteStatus.Text = "Some Error occured during WebPart configuration, Please configure manually...";
}
}
catch (Exception ex)
{
lblSiteStatus.Text = "Error :" + ex.ToString();
}
}
</script>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<table border="1" cellpadding="4" cellspacing="0" style="font-size: 12">
<tr>
<td>
Site Title:</td>
<td>
<asp:Label ID="lblSiteTitle" runat="server" /></td>
</tr>
<tr>
<td>
Site ID:</td>
<td>
<asp:Label ID="lblSiteStatus" runat="server" /></td>
</tr>
</table>
</asp:Content>
<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">Configure Content Query Webpart</asp:Content>
<asp:Content ID="PageTitleInTitleArea" runat="server" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea">Configure Content Query Webpart
</asp:Content>
Remarks:
The WebUrl property determines the ContentByQueryWebPart object's mode is determined by the WebUrl, ListGuid, and ListName properties.

-- If the WebUrl, ListGuid, and ListName properties are set, the mode is List.

-- If the WebUrl is set and ListGuid and ListName are empty, the mode is Web.

-- If WebUrl, ListGuid and ListName are empty, the mode is Site Collection.

-- If both ListGuid and ListName properties are set, ListGuid takes precedence.

1 comments:

Anonymous,  June 29, 2011 at 5:38 AM  

good one