0

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:

Web part in web part page.

What should I do to resolve my problem? Please help.

2 Answers 2

1

Actually I think you should not have the Query tags at all! Try:

<OrderBy>
    <FieldRef Name='FileLeafRef' />
</OrderBy>
9
  • I added the the startin <Query> tag and found the error i.e.: "There is a problem with the query that this Web Part is issuing. Check the configuration of this Web Part and try again."
    – users1100
    Commented Sep 13, 2013 at 6:32
  • I think the key is to remove the query tags completely , I'll update my answer Commented Sep 13, 2013 at 6:33
  • Thanks for answering sir, it started displaying data, but not from the library specified, i.e. Sample Docs, it is displaying the pages from all sites.
    – users1100
    Commented Sep 13, 2013 at 7:12
  • Could be something with your ListUrl and ListName being wrong Commented Sep 13, 2013 at 7:26
  • OK, I an checking it..
    – users1100
    Commented Sep 13, 2013 at 7:33
0

I found my solution:

First -> give the Guid of the list which you want to display, to get the Guid go to the library/list , select Library Setting in the url copy the "List={-----}" parameter value.

Second -> Assign the WebUrl property of the Web Part Object.

Here is my code:

static void Main(string[] args)
{
     try
     {
        SPSite site = new SPSite("http://bipc11:1234/sites/publishing1");
        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.ListGuid = "eff50fad-f899-4e26-a9a4-1b40aedfc9be"; \\ Paste the Guid of List/Library.
        wp.WebUrl = "~SiteCollection";                
        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);
    }

}

The data is displayed.. :-)

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.