0

I have added the CQWP successfully to the web part page programmatically. Now I want to query data for that web part i.e. I want to display the title of the file from the document library.

My code is:

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.ItemXslLink = web.Url + "/Style Library/ItemStyle.xsl";
wp.CommonViewFields = "Title,Text";
webParts.AddWebPart(wp, "Right", 0);
webParts.SaveChanges(wp);
web.Update();
Console.WriteLine("Web part added successfully");
Console.ReadKey();

Document Library is in the root site with name Sample Docs.

I am getting the output like:

enter image description here

How to query data? Please help.

users1100
  • 3,230
  • 6
  • 61
  • 114

3 Answers3

1

You need to set the QueryOverride property of ContentByQueryWebPart class. Create the desired query by a tool like CAML query builder and assign it to QueryOverride

EDIT

For example, you can write query like this:

wp.QueryOverride= "<Where><Eq><FieldRef Name='Status'/>" +
    "<Value Type='Text'>Completed</Value></Eq></Where>";
Nadeem Yousuf-AIS
  • 18,707
  • 4
  • 28
  • 59
  • Can you please post any reference link or suggestion if any? Because I am totally new to this concept.. – users1100 Sep 12 '13 at 06:40
  • 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 = "<where> <Eq> <FileLeafRef Name='Title' /> <Value Type='Text'/></Eq></Where>"; webParts.AddWebPart(wp, "Right", 0); webParts.SaveChanges(wp); web.Update();

    Though I am getting the error that there is a problem with query.

    – users1100 Sep 12 '13 at 09:07
  • 1
    "" + docname + "" See this for reference http://sharepoint.stackexchange.com/questions/35571/caml-query-to-get-listitem-by-name-field – Nadeem Yousuf-AIS Sep 12 '13 at 09:22
  • I wrote the line as per your suggestion i.e. <where> <Eq> <FieldRef Name='FileLeafRef'/><Value Type='File'>example1</Value></Eq></Where> still I am getting error. – users1100 Sep 12 '13 at 09:28
  • One more thing is that if I provide the document name in the query then what to do to get all document names from library? – users1100 Sep 12 '13 at 09:29
1

I would suggest you to check this out, it gives a good oversight of the posibilities and how to use the CQWP correctly. I hope this will help a bit.

Shkipper
  • 2,105
  • 1
  • 23
  • 52
  • I went through the article and changed the lines of code i.e., wp.QueryOverride = @"<Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='File'></Value></Eq></Where>"; wp.ListsOverride = @"<Lists> <List ID='{EFF50FAD-F899-4E26-A9A4-1B40AEDFC9BE}'/> </Lists>"; but I am getting error. – users1100 Sep 12 '13 at 10:35
0

there is another idea to do this which is adding your CQWP using the portal UI then export the webpart after that you can import the exported web part into any page using SPLimitedWebPartManager.ImportWebPart and here is an example to this http://avinash-moss-expert.blogspot.com/2010/08/add-web-part-programmatically-using.html

Hossam Barakat
  • 363
  • 1
  • 12