Tuesday, 6 November 2012

Programmatically Add Content By Query WebPart to Page


using (SPLimitedWebPartManager webPartManager = SPContext.Current.Web.GetLimitedWebPartManager("SitePages/CRISP/Home.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
{
ContentByQueryWebPart cqwp = new ContentByQueryWebPart();
cqwp.ContentTypeBeginsWithId = "0x0101006F0A470EEB1140E7AA14F4CE8A50B54C0001CB1477F4DD432AA86DD56CC3887AF40902";
cqwp.WebUrl = web.ServerRelativeUrl;
cqwp.MainXslLink = "/Style Library/XSL Style Sheets/ContentQueryMain.xsl";
cqwp.Title = "Recently Published";
webPartManager.AddWebPart(cqwp, "Top", 1);
webPartManager.Web.Dispose();
}
view raw cqwp.cs hosted with ❤ by GitHub

2 comments:

Unknown said...

How to get data from specific list or library into the above added CQWP?

Vardhaman Deshpande said...

You need to set the WebUrl, ListGuid, and ListName properties of the CQWP to get data from a specific list.