Did You Know!?: SharePoint Webparts and libraries

Hello again!

I decided to start a new blogging category named “Did You Know!?”. The idea behind this is that I write tips and tricks you might not think of or may not have heard but will help you and make you life easier.


Content Query WebPart – Audience targeting

Custom views for document library or lists webparts

So here we go:

Content Query WebPart – Audience targeting:

If you did not know you can filter content of the CQWP(and also in the code if you are using CrossListQueryInfo and CrossListQueryCache) by enabling Audience Targeting to the webpart.

What do you need to do to make it to work:

– Enable the CQWP Audience targeting – from the webpart properties

cqwp audience

– Enable your content source to support audience targeting – Example: your library or list – from the list/library settings

Go to your library settings => Under “General Settings” => Select “Audience Targeting Settings” => Then enable Audience targeting by checking the checkbox.

After this you will get a new field to your list named as “Target Audiences”. Now when you create or edit items you will be able to chose an audience to that item. When a CQWP has audience targeting enabled what will happen is that the webpart will check the item audience targeting values and compare them to the user profile audiences. If the user is part of the items audience then the item will show up in the CQWP

– Configure your user profile service to support audience targeting

This is the tricky part. You need to configure a whole lot of places to get your audience targeting for a user to work so that webpart and other content related queries etc filter data properly.

I will do it in three parts the configuration:

1. Configure a Term Set in your Manage Metadata Service. Let’s say named “Location” that has 10 different countries your organization is interested in. This I will not explain in much detail. I hope you are familiar with MMS.

article mms

2. Go to your user profile service and create a new User Property(The picture below as an example). Also take notice(I do not remember exactly) but the privacy settings needs to set to everyone for this field to work properly.

new user property

3. Next assign to a user profile(s) a desired location to the newly created field.

4. Then lets create a new audience. In your user profile service go to “Manage Audiences”. Then Press the “New Audience” button. In the next view give the audience a name and an owner. And remember to select the rule strategy. I chose “Satisfy any of the rules so that I may include many rules to a single audience. This way I can aggregate two or more location to an audience and have any of them apply to an item and user.

5. In the next step select as an “Operand” property and select the location property we created and as the operator I would chose “Contains”. Next in the Value field select a value.


6. Now all you have to do is compile the audience. Press “Compile Audience”. After the compilation is complete you should see how many members are apart of this audience. Next when you assign this audience to an item any user from is apart of this audience through his location value that corresponds to the audience location values are going to see the CQWP item.

Custom views for document library or lists webparts:

You are able to create custom Views for your library or list webparts that show you content from your library. You are not limited to what you get once you add a documents library to a page as a webpart. BUT there is a catch!!! If you do select and existing view and use it to display content in a certain manner in a page YOU WILL NOT have a reference to that view in you library BUT A COPY held in the webpart. So lets say that you have a view that lists the TOP 5 newest documents in your library. You decided to add the document library webpart to a page, you then select this TOP 5 view, what happens is that you will get a copy that is not related to the original view of that document library. SO if you make changes to the view in the document library view those changes will not be seen in your webpart because it is not a reference. So remember this when you use a view from a library or list into your webpart.

New icon to CQWP – Content Query Webpart


So you may have wondered HOW to enable or add a new icon to a CQWP. Well aparently you can not do it through the user interface and you need to do some XSLT modifications. Here is what you need to do:

1. You need to download the following MS XSL file to datetime processing and add it into your XSLT file that displays your data:

The MS BLOG file

The XSL piece of code to add your XSL file, this will include the above downloaded file into your XSL file:

<xsl:import href=”/Style Library/XSL Style Sheets/date_templates.xsl”/>

2. Inside your XSL file that displays data(in the case of a CQWP lets say the item style XSL template) add the following lines:

<xsl:call-template name=”getDayDelta”>
<xsl:with-param name=”paramDateA” select=”@ArticleDate”/>
<xsl:with-param name=”paramDateB” select=”ddwrt:Today()”/>

<xsl:variable name=”article_date”>
<xsl:value-of select=”ddwrt:FormatDate(string(@ArticleStartDate), 1033,1 )”/>
<xsl:variable name=”today”>
<xsl:value-of select=”ddwrt:FormatDate(string(ddwrt:Today()), 1033, 1)”/>
<xsl:variable name=”date_diff”>
<xsl:call-template name=”getDayDelta”>
<xsl:with-param name=”paramDateB” select=”ddwrt:FormatDateTime(string(@PublishedDate),1033, ‘yyyy-MM-dd’)”/>
<xsl:with-param name=”paramDateA” select=”ddwrt:FormatDateTime(string(ddwrt:Today()),1033, ‘yyyy-MM-dd’)”/>

The above was meant for a CQWP that would lift the latest blogs inside a blog site. Notice the @PublishedDate parameter. This can be @ArticleDate or @ArticleStartDate etc. What you need to make the date difference calculations.

The date_diff XSL variable will hold the date time calculation between the blog post published date and the present date. Then you can use this value to determine when to show the New icon.

3. Add the logic to display the New icon. The below logic tells that if the items is less than 7 days old you need to display the New icon.

<xsl:if test=”$date_diff &lt; 7″>
<img src=”/_layouts/1033/images/new.gif” alt=”New!” />

Sample in the XSL Blog title:

<div class=”blogListPostBlock”>
<!– Title –>
<a style=”color: #295AC6;”>
<xsl:attribute name=”href”>
<xsl:value-of select=”$SafeLinkUrl”/>
<xsl:value-of select=”$DisplayTitle” />
<xsl:if test=”$date_diff &lt; 7″>
<img src=”/_layouts/1033/images/new.gif” alt=”Smiley face” />

Also please take into consideration that this is for the LCID language ID 1033 English  You need to make sure it fits your language if you wish to use a different language translation for New icon.