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.

3 Answers 3

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.

1
  • 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
    Commented Sep 12, 2013 at 10:35
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>";
5
  • Can you please post any reference link or suggestion if any? Because I am totally new to this concept..
    – users1100
    Commented Sep 12, 2013 at 6: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
    Commented Sep 12, 2013 at 9:07
  • 1
    "<Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='File'>" + docname + "</Value></Eq></Where>" See this for reference sharepoint.stackexchange.com/questions/35571/… Commented Sep 12, 2013 at 9: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
    Commented Sep 12, 2013 at 9: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
    Commented Sep 12, 2013 at 9:29
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.