I have added a CQWP from visual studio. In that web part, I want to fetch the document names of the document library named "Sample Docs".
My code for doing so is as follows:
static void Main(string[] args)
{
try
{
SPSite site = new SPSite("http://..........");
SPWeb web = site.RootWeb;
web.AllowUnsafeUpdates = true;
SPLimitedWebPartManager webParts = web.GetLimitedWebPartManager("SiteAssets/webparts.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
ContentByQueryWebPart wp = new ContentByQueryWebPart();
wp.Title = "My web part using OM";
wp.ListUrl = web.Url + "/Sample Docs";
wp.ListName = "Sample Docs";
wp.XslLink = web.Url + "/Style Library/XSL Style Sheets/ItemStyle.xsl";
wp.QueryOverride = "<OrderBy>"+
"<FieldRef Name=\"FileLeafRef\" />"+
"</OrderBy>"+
"</Query>";
wp.CommonViewFields = "Title,Text";
webParts.AddWebPart(wp, "Right", 0);
webParts.SaveChanges(wp);
web.Update();
Console.WriteLine("Web part added successfully");
Console.ReadKey();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Web part gets added successfully but data is not displayed, the web part gives following error:
What should I do to resolve my problem? Please help.