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:


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].


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:

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)

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 . 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.


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.


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.


How to filter SharePoint List based on user permission

Have you ever had a scenario where you had to create a custom list and then you should only show items in the list which the user is supposed to see based on the user permission or the User Group he belongs to?
Read on if you want to find a solution for this scenario..... I did tried lot of researching and I got bits and pieces of information scattered in the net but couldn’t find a compete article, so thought to blog it, hope this will help you.

I will brief a bit more on the scenario:

I was asked to create a Master Custom list for users of different departments in my company however since there are confidential documents these items should not be shown to each other. My initial thought was to achieve this OOB using views and permission however while digging down I understood that this was not straight forward and I couldn’t find much help from the internet. To achieve this I have done the below : 

Step 1.

First I have created a Custom list called “Contract Manager” with the necessary columns for me to store contracts (My Company teams were supposed to store contract proposals in a List), I created a Column called “Departments” on the list which is basically a “Person or Group” field. 

Step 2.

I have created SharePoint Groups with Contribute access with the names “CTProcurement”, “CTHR” ..... (Prefixed CT as I was called this site Contract Manager, so you can create the group with any logical name and then set it to Contribute permission. So while creating a new item in the list the users will be able to add this group on to the Department field and also CAN add any department on to the field, for me there was a business case that “Procurement” team will create contract proposals for them and for other team. In this was users can create items which can be visible for other teams as well.

Step 3.

Once completing the above 2 steps you need to create a New View or edit the “View All” default view using SharePoint Designer, and what we are going to do is we will set the view in such a way that it will only display items which the user belongs to and will pick the “Group Name” which the user belong to and will filter the “Department” column to show only items with the Group Name. In- essence the users will only see the items in the list for which their department is added on the “Department” column. This is a simple logic, you might need to take a moment to contemplate.

Now here are the steps where you can put this Query in the default view/Or a new View you create : 

- Open the Site in SharePoint Designer 
- Select the List, and on the right hand side, click on the “All Items” view Once opened  Search for “ &lt;Query&gt;

- Paste the below code, analyse the code it is straight forward make appropriate changes for yours if needed

You can also download the full working view code from here - Download Code

-       Sometimes when you try to save the view it might throw you error, for me it was saying “Reference field not found”, so I have the add the “Department” reference field properly below the “ &lt;Query&gt; tag – 

Step 4 –

Once you done this and if everything is working, well done you are almost there J, Now we need to edit the permission level’s slightly so that the “Contributors” group should not create a new “View” where they can see all the items, for that follow the step below
            Site Action &gt; Permission Levels &gt; Contribute &gt; Uncheck “Manage Personal Views  -  Create, change, and delete personal views of lists” , I have also unchecked “Manage Lists  -  Create and delete lists, add or remove columns in a list, and add or remove public views of a list.”. So now the contribute users will not be having permission to create a new view where they can change the filters.

For my project this was a perfect solution, I did this in SharePoint 2010, I believe the same principle can apply in SharePoint 2013 there is also something called “Content Search Webpart” which you can make use of, now I have also created a simple reporting mechanism to display all the items for which the alert date is set by using Content Query Webpart. I will soon write about that for you….till then happy SharePointing :)… Hope this helps you.

Here is a screenshot of CQWP that –

SharePoint Template Sites

  SharePoint look book Get inspired Discover the modern experiences you can build with SharePoint in Microsoft 365 https://lookbook.microsof...