Skip to main content

Mike Geyer's Blog

A fun place to read and collaborate about SharePoint and related technologies.
Go Search
  

Keep in touch: RSS Feed  


I'm a


My company is a


And we deliver

  (we're certified)
Mike Geyer's Blog > Posts > Content Query Web Part (CQWP) with Anonymous Access
Content Query Web Part (CQWP) with Anonymous Access
As I've finally deployed my blog to the world, I stumbled across an interesting quirk associated with the Content Query Web Part (CQWP).  The problem, which has been described by other blogs, is that the links to items presented in the CQWP are not anonymous-friendly.  The titles are shown as expected, but clicking a link result in a request that the user provides credentials.  Without proper credtials, users are directed to a "403" error.
 
Rather than linking to the item directly, all links go through CopyUtil.aspx.  After some research, I've learned that this page, without customization, does not support anonymous access.
 
I decided to follow some instructions from Heather Solomon's Blog on customizing Item Styles for the CQWP and create my own Item Style for Blog Posts.
 
For this example I am going to present a list of Blog Posts -- which you can see on most (if not all) of my Blog Pages
 
1.  Configure the CQWP with Default Settings
To begin, follow any number of instruction sets for using the CQWP.  If you don't know how to use the CQWP, try starting with some help from Microsoft.
 
Once I was convinced the query was right (using the default style), I saved my changes and moved over to SharePoint Designer (SPD).
 
2.  Create a Custom Item Style
Within SPD, navigate to the XML Style Sheets folder within the Style Library.  Notice that I'm at the parent site level, not the sub-site.
 
Open the ItemStyle.xml file (you may have to check it out).CQWP - Item Style
Looking in this file, and this is important if you're not familiar with working with it, you'll see that there are a number of XML entries starting with <XML:template...  This indicates a style template that you can customize, or copy and change for your own liking.  That's what we're going to do.
 
Copy the XML.template section for default (see image) and paste it down at the bottom of the file (but before </xsl:stylesheet>).
CQWP 2
Now change the template name and match="Row... entries:
<xsl:template name="ListofBlogPosts" match="Row[@Style='ListofBlogPosts']" mode="itemstyle">
CQWP 3
Next, let's replace the code that displays the hyperlink to the blog item.  To do this, I commented out the code provided by Microsoft and added my own:
CQWP 4
Here's the code if you'd like to copy & paste
 <xsl:text disable-output-escaping="yes"><![CDATA[<a href="Lists/Posts/Post.aspx?ID=]]></xsl:text>
 <xsl:value-of select="@ID"/>
 <xsl:text disable-output-escaping="yes"><![CDATA[">]]></xsl:text>
 <xsl:value-of select="$DisplayTitle"/>
 <xsl:text disable-output-escaping="yes"><![CDATA[</a>]]></xsl:text>
Save the file (you may have to check it in and publish it).
 
 
3.  Update the CQWP to use this new Style
Return to your browser and Modify the Shared Web Part for your CQWP.  Expand the Presentation section.  Under Styles, and select the new Item Style (ListofBlogPosts, for my example).
 
CQWP 5
Click the OK button and confirm that your new style displays the correct blog items and the hyperlink links directly to the post.
 
I hope this helps.
 
- Mike
 

Comments

Re: Content Query Web Part (CQWP) with Anonymous Access

Excellent tutorial. I was having this exact problem and would never have figured it out.
at 8/9/2009 4:00 PM

Re: Content Query Web Part (CQWP) with Anonymous Access

Thanks for this - solved the problem nicely!
at 8/9/2009 4:00 PM

Accessing multiple sites

Mike,

I am using the CQWP to access items from multiple lists in multiple sites. From what I can tell, it looks like the code only accesses one list. Is that correct?
at 8/9/2009 4:00 PM

Accessing multiple sites - response to Kris

Hello Kris,

If you set up the CQWP (using the tool pane) before embarking on step #2, you can set the query to pull items from all lists within a Site Collection or below a specific site.  If you've already customized the CQWP and don't want to "break things" by using the tool pane, take a look at the WebURL property in the CQWP text file.

This value, for example, will query all lists within a Site Collection:
<property name="WebUrl" type="string">/</property>

This value will query all sites at and below SomeSite:
<property name="WebUrl" type="string">/SomeSite</property>

I hope this helps.

-- Mike
at 8/9/2009 4:00 PM

Accessing multiple sites

Mike,

I feel like I'm going to lose my mind. I'm trying to the same thing as Chris...I want to display announcements from subsites is seperate content query webparts, i.e. different announcements list from different subsites, but I didn't quite understand the response you wrote (about the weburl property).

I changed your 'Lists/Posts' to 'Lists/Announcements' and instead, ALL my CQWPs ran an error.. HELP. Please.
at 8/9/2009 4:00 PM

Almost have Multiple Sites Working

I have almost figured out the problem with the multiple sites problem (I have been experiencing the same thing).  I have added to Mike provided:

<xsl:text disable-output-escaping="yes">
<![CDATA[<a href="/]]>
</xsl:text>
<xsl:value-of select="@NEED SITE URL"/>
<xsl:text disable-output-escaping="yes">
<![CDATA[</Lists/Posts/ViewPost.aspx?ID=]]>
</xsl:text>
<xsl:value-of select="@ID"/>
<xsl:text disable-output-escaping="yes"><![CDATA[">]]>
</xsl:text>
<xsl:value-of select="$DisplayTitle"/>
<xsl:text disable-output-escaping="yes"><![CDATA[</a>]]>
</xsl:text>

I just need to figure out how to pull the site url name from SharePoint;  I have figured out ways to pull the site name, but that displays with spaces and such.

Thanks!
at 8/9/2009 4:00 PM

@NEED SITE URL

The variable name is $SiteUrl to pull the site url in this way.
at 8/9/2009 4:00 PM

Re: Content Query Web Part (CQWP) with Anonymous Access

Here is a link to an article I wrote on modifying CopyUtil to allow anonymous access to CQWP links:
http://www.sectorpoint.com/blog/post/2008/07/10/Anonymous-access-and-the-Content-by-Query-Web-Part-(CQWP).aspx
at 8/9/2009 4:00 PM

Multiple Sites Issue

Yeah this only "fixes" the problem if you have Lists in the same sub directories on every site in the same manner.  I need this web part to pull from any list in the Site Collection that matches a content type of my selection.

The process in which this web part works is flawed.
at 8/9/2009 4:00 PM

Re: Content Query Web Part (CQWP) with Anonymous Access

This post was very helpful for me. Thanx a lot. Didn't have 2 take the risk of trying out with users who had no rights.Its a gud idea to make it anonymous accessible. But is there any way we can customize CQWP to only display links for lists to which the logged in user has access and leave out other lists??? Please sugggest.
at 8/9/2009 4:00 PM

Re: Content Query Web Part (CQWP) with Anonymous Access

How can u pull out the name of the list to which each of the items in CQWP belongs? What changes are to be made in ItemStyle.xsl as well as CQWP.webpart?
Pls help.
Thanx in advance
at 8/9/2009 4:00 PM

 All Posts

I've Moved to Hitachi Consulting
SharePoint 2010 Sneak Peak!
Business Intelligence During an Uncertain Time
Keeping In Touch with Family Around the US
Using Javascript to Manipulate a List Form Field
How to Use JavaScript to access SharePoint Query String
First Blog with Windows Live Writer
Microsoft to offer free classes and certification testing
Job Description - SharePoint Solution Architect
SharePoint Blogs as Status Reporting... and community building
Installing MOSS on WinServer 2008 with Hyper-V and SQL08
Keeping up with SP Blogs
Use Search as Content Roll-up
Web browser compatibility in InfoPath Forms Services
Picture Library: Getting to the Thumbnails
All about dates and SPD
Data View Web Part, learn it... love it!
MSFT's SharePoint 2007 Buzz Kit
Content Query Web Part (CQWP) for All List Types
Content Query Web Part (CQWP) with Anonymous Access
My Blog is Live
Examples of Common Formulas
Creating a Form to Add Items to a SharePoint List
Building a Blog Host
Scaling to Extremely Large Lists
Configuring Email Alert Templates
Populating Form Data from SharePoint List Views
SharePoint Designer Workflows and InfoPath Form Libraries
Restoring the Quick Launch Bar in Web Part Pages
Creating a Hyperlink to an InfoPath Form
Changes to SPD Created Workflows
How to find the CSS Class Applied to any SharePoint Element
Publishing an InfoPath 2007 Form Template to a Server Running InfoPath Forms Services
Why you Shouldn't Choose "Enable Rendering on a Mobile Device" when Publishing an InfoPath Form Template
Find Form Template for an Existing Library
Adding a Portal Breadcrumb to your My Site
Define Custom Permission Levels
Exchange 2003 Hotfix (Outlook 2007 and SP Alerts)
Copyright© 1997-2009 Mike Geyer
The posts on this weblog are provided “AS IS” with no warranties, and confer no rights. The opinions expressed herein are personal.