Tuesday, April 23, 2013

SharePoint: Content Query Webpart - OOB, Custom Fields, Custom Style with Table format

Objective: Quick tips for Content by Query Webpart (CQWP)

Out of Box:
  1. You can display consolidated data from different lists, libraries, blogs, task lists etc.
  2. Best way to implement is with Content Type. Ex: To fetch data from multiple lists with same columns, first create Site content type, assign to all those lists and in CQWP select the content type.
  3. By default, you can pick two columns; one as Title and other as Description. you can also set one of default styles available like below:


Adding Custom Fields:
General requirement is to display more columns from list or library in webpart. Its small trick -
    1. Add out of box CQWP webpart on page and assign the lists, content types etc.
    2. Now export the webpart to your local machine.
    3. Open the webpart either in SP designer or visual studio or as matter of fact in Notepad and search for "CommonViewField" property like below screen.
    4. Add your list columns which you wants to display addition to Title & Description(both added earlier so no need to repeat here):
    5. TIPS:  Here you have to use internal name. In my example, my column names are "Request Set Status" and "File Number".
    6. Save your webpart, import it back to SharePoint and add to page. You can delete your earlier out of box webpart which we added in step 1.

Adding Custom Style Sheet:  Once you added your own fields, I am sure you want to change the display/style of data. Here you go to change style:
  1. Go to site style library(under view all site content) and first take copy of ItemStyle.xsl as backup before you play with it.
  2. Now open the ItemStyle.xsl in either designer, visual studio or even notepad. This xslt contains all the out of box styles so don't get scare by its long complicated look.
  3. Create a copy of entire template section of name "Default" and in your copied section change name as suited to your need. Here I changed to "CustomStyle". Don't forget to change in Match attribute as well other wise it will reflect in all styles
  4. Now, add your additional columns(added in step 6 of earlier part of custom field) in the Item section as below. I have marked columns with horizontal yellow line.
  5. TIPS: Here you can notice that the columns internal names are slightly different format. That's how SharePoint except :) . So be careful here.
  6. You can play with HTML tags, CSS to give your own style,color etc
  7. To implement Table header and footer, you need to do couple of steps. As you can see in above picture, I have marked few lines with vertical yellow color. That will help in setting up table headers. You can change header as per your need in Header variable section.
  8. After completing all changes in ItemStyle.xsl, upload back to Style Libray.

Happy Learning!





Microsoft Reference Link: http://msdn.microsoft.com/en-us/library/aa981241(v=office.14).aspx