1

I have added a content query web part from visual studio and I am displaying the data from a document library of root site.

I want to set the ItemStyle property to Title and Description and assigne them respective value i.e. Title=Document Title and Description=Editor.

My code for adding web part is:

            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";                                         
            wp.WebUrl = "~SiteCollection";                
            wp.ItemStyle = "Title and Description";
            webParts.AddWebPart(wp, "Right", 0);
            webParts.SaveChanges(wp);
            web.Update();
            Console.WriteLine("Web part added successfully");
            Console.ReadKey();

How do I change the property? Please help.

1

You'll have to set the wb.ItemStyle with the item styles template name.

Examples:

wb.ItemStyle = "Default";
wb.ItemStyle = "ImageLeft";

It does not understand the display names of the item styles.

MSND page explaining the different ItemStyles.

  • Should I look in the ItemStyle.xsl file from the Style Library to get the name? – users1100 Sep 16 '13 at 7:52
  • @users1100 Yes. – user2536 Sep 16 '13 at 7:52
  • I went through the file but did not find any template name having Title and Description. – users1100 Sep 16 '13 at 7:58
  • I did one thing to find the name of Title and Description. I exported the web part after setting the Title and Description manually and checked the ItemStyle property. It was displaying the name i.e.NoImage. I assigned in wp.ItemStyle but how to assign the individual values? – users1100 Sep 16 '13 at 8:11
  • I mean that template is assigned but how to assign Title=Title and Description=Editor ? @Daniel Ziga – users1100 Sep 16 '13 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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