Thursday, 13 February 2014

SharePoint 2010 Content Query Web Part (CQWP) to display items from a List and Format it


In this article i will show how to query a SharePoint List using Content Query Webpart and display the results in a Nice formatted way
Here is a screenshot of what i have done using CQWP , querying a Custom SharePoint List:

image

I will explain the steps below on how i have accomplished this :

Step 1.
I have created a custom list with appropriate list columns and it has got columns with the names : “Contract Name” [Single Line of Text]  “Alert Date”[Date and Time] and “Notes” [Multiple line of Text].

1

Step 2.
Add a Content Query Webpart [CQWP] on to the site, now for displaying this in the above format we need to do two things
- Need to Edit the “ItemStyle.xslt” in the Styles folder and need to add a bit “CSS” on to the Site. Both of these can be accomplished as long as you have access to these folders and if you can add a Content Editor Webpart on to the page.
Now let me walk you through these two steps:

Step3.
First lets see how we can edit the “Item Sytle.xl” file
Navigate to Site Actions > View All Site Content > Style Library > XSL Style Sheet > “Item Style.xsl” open this file in the SharePoint Designer (recommend to take a backup of that before you do anything) and then you have to add a bit code in the end to create a new template,   i have created a new one called “EventDisplay” .This will create a new style in your Content Query Web Part’s “Item Style” dropdown.  Make sure you add the code which you can find on the end of the file from code line 647, also this should be added inside the  “</xsl:stylesheet>”. The file is self explanatory, make necessary changes in accordance with your list and column names.


Make sure you add the ddwrt namespace to your .xsl file in the beginning, Do this by pasting this line near the top of the file, right below the “xmlns:cmswrt” (usually line 6)


xmlns:ddwrt=http://schemas.microsoft.com/WebParts/v2/DataView/runtime
Here i have created a completed “ItemSyle.xsl” which you can DownLoad Here

To get this new XSL style to look decent, we’ll also need to add some CSS to make it stand out. If you already have a custom stylesheet attached to your masterpage, open that up. If not just add a Content Editor Webpart to the page to add the styles[if you don’t know how to do it find  here a similar example http://sharepointlogics.com/2013/02/site-action-menu-styling-in-sharepoint.html . So with your CSS open, paste in these styles, on the Item style.xsl file there are references to these CSS classes which gives the the formatting.

You can download the code from here again DownLoad Here

/*NewList XSLT Styles*/ .xslCal .left{width:82px; position:relative; left:-21px; float:left;} .xslCal .right span{display:block;} .xslCal .month{text-transform:uppercase; background:#71cdf4; color:#fff; padding:3px 0 3px 18px; width:68px; display:block; } .xslCal .day{font-size:61px; width:86px; display:block; line-height:70px; text-align:right;} .xslCal .title{color:#0775aa; display:block; padding-top:3px;}
Now navigate to the page where you want to display CQWP, and click Site Actions->Edit Page. Then add a Content Query Web Part to the zone you want. Get into the toolpane for that web part by clicking the small black arrow in the title, and selecting “Edit Web Part”. This will open the toolpane on the right side of the screen. Fill out your query by clicking the “Show items from the following list” radio button, and using the “Browse” button, goto the “Custom” list you created earlier. Under the “Content Type” heading, select “List Content Types” from the first dropdown, then “Item” from the second.


image

Open up the “Presentation” tab and make sure that the Group Style is “Banded” and Item Style is “EventDisplay”. Then, to ensure that the correct data goes in the correct slots, setup the “Fields to Display” like shown.

image

If you got it all done correctly, you will get a nicely formatted CQWP like this, Hope this helps you with your journey with CQWP.


image


No comments: