Content Query Web Parts, Anonymous Access, and the Dreaded CopyUtil.aspx in SharePoint 2010
December 10, 2010 5 Comments
I spent some decent time over the past couple of days trying to get a Content Query Web Part in SharePoint 2010, that was aggregating blogs from several different sites to “just work”, and was not having any luck. Basically, the CopyUtil.aspx page, which resides in the SharePointRoot/TEMPLATE/LAYOUTS/ folder, redirects you based on the ListID, ItemID, and WebID of the target item within the Content Query Web Part.
This work is being done for a client, who is migrating their public website from SharePoint 2007 to SharePoint 2010. It’s been a great project for a great client, however, as with any project, there are always a few curveballs unique to that specific project. This was one of them.
In their prior version of the site, the previous developer implemented the solution which can be found here: http://www.sharepointforum.se/en-US/Bugday/Lists/Posts/Post.aspx?ID=9, a custom application page and a DLL to go with it. This did not work on my client’s 2010 environment, so I had to go looking elsewhere, and was not finding too much. My good friend, Christina Wheeler, happened to direct me to a blog post she did on the matter, which was a great idea, however, this one was also directed at 2007 – http://sharepoint.mindsharpblogs.com/ChristinaW/archive/2009/12/07/Content-Query-Web-Parts-and-Anonymous-Access.aspx, or at least, an environment that was not similar to my client. There was no dispform.aspx within the layouts folder, and dropping one copied out of a document library required a bit of toiling to work… and it would not reference the master page I was hoping to use.
All looked bleak and hopeless, however, the #2 method in Christina’s post was the ticket, and, I found the example code that I needed out on the interwebs as well! Hooray for the interwebs! Which led me to this blog post: http://www.sectorpoint.com/blog/post/2008/07/10/Anonymous-access-and-the-Content-by-Query-Web-Part-(CQWP).aspx
Now, the version there however, is for 2007 as well, but, looking at how they did it, made perfect sense, and I was able to put it to practice in SharePoint 2010.
Here are the steps to get this functional…
Go into the SharePointRoot/TEMPLATE/LAYOUTS/ folder –
NOTE: DO NOT DO ANYTHING TO THE ORIGINAL COPYUTIL.ASPX!
And, create a new file, for example, GVCopyUtil.aspx, and put the following code into it, the only change from CopyUtil.aspx, is the line highlighted below.
<%@ Assembly Name=”Microsoft.SharePoint.ApplicationPages, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”%>
<%@ Page Language=”C#” CodeFile=”CopyUtil.aspx.cs” Inherits=”CopyUtil” MasterPageFile=”~/_layouts/application.master” %>
<%@ Import Namespace=”Microsoft.SharePoint.ApplicationPages” %>
<%@ Register Tagprefix=”SharePoint” Namespace=”Microsoft.SharePoint.WebControls” Assembly=”Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>
<%@ Register Tagprefix=”Utilities” Namespace=”Microsoft.SharePoint.Utilities” Assembly=”Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>
<%@ Import Namespace=”Microsoft.SharePoint” %>
<%@ Assembly Name=”Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>
<%@ Register TagPrefix=”wssuc” TagName=”TopNavBar” src=”~/_controltemplates/TopNavBar.ascx” %>
<%@ Register TagPrefix=”wssuc” TagName=”LinksTable” src=”/_controltemplates/LinksTable.ascx” %>
<%@ Register TagPrefix=”wssuc” TagName=”InputFormSection” src=”/_controltemplates/InputFormSection.ascx” %>
<%@ Register TagPrefix=”wssuc” TagName=”InputFormControl” src=”/_controltemplates/InputFormControl.ascx” %>
<%@ Register TagPrefix=”wssuc” TagName=”LinkSection” src=”/_controltemplates/LinkSection.ascx” %>
<%@ Register TagPrefix=”wssuc” TagName=”ButtonSection” src=”/_controltemplates/ButtonSection.ascx” %>
<%@ Register TagPrefix=”wssuc” TagName=”ActionBar” src=”/_controltemplates/ActionBar.ascx” %>
<%@ Register TagPrefix=”wssuc” TagName=”ToolBar” src=”/_controltemplates/ToolBar.ascx” %>
<%@ Register TagPrefix=”wssuc” TagName=”ToolBarButton” src=”/_controltemplates/ToolBarButton.ascx” %>
<%@ Register TagPrefix=”wssuc” TagName=”Welcome” src=”/_controltemplates/Welcome.ascx” %>
<%@ Register Tagprefix=”wssawc” Namespace=”Microsoft.SharePoint.WebControls” Assembly=”Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>
<%@ Register Tagprefix=”SharePoint” Namespace=”Microsoft.SharePoint.WebControls” Assembly=”Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>
<asp:Content ID=”Content1″ ContentPlaceHolderId=”PlaceHolderPageTitle” runat=”server”></asp:Content>
Now, save that, and create another file in the folder, called CopyUtil.aspx.cs, and paste the following C# code into that:
using Microsoft.SharePoint.ApplicationPages;
public partial class CopyUtil : Microsoft.SharePoint.ApplicationPages.CopyUtil
{
protected override bool AllowAnonymousAccess
{
get
{
return true;
}
}
}
Not so hard, huh? And actually, it is pretty simple, even if you are a non-developer, so don’t be scared. The first section, and more importantly, the highlighted section, says that the language of the page is C# – a programming language. CodeFile references the code it will use. Inherits, means that the page will utilize the class CopyUtil from the CodeFile mentioned previously. And the master page directive just states that we will use that as our master page.
How did we figure out the class to use? Easy, that is highlighted in the 2nd code snippet above.
Now for the second code snippet, all this does, is make use of the Microsoft.SharePoint.ApplicationPages namespace – found on the first line with “using”. We are then inheriting all of the code that makes up this page, and then overriding the boolean value for AllowAnonymousAccess, and setting that to TRUE, rather than it’s normal value, which is FALSE.
In any event, Christina really saved my tuckus today – so thank you Christina!
Thank you so much Geoff. This worked a treat on SharePoint 2007 by copying the code from copyUtil.aspx into a new file and applying your changes. There’s lots of blogs explaining the problem with a one sentence fixes which for people like me who fear meddling about with the guts of SharePoint doesn’t help. Your complete solution was therefore invaluable. Thanks again.
Steven
Happy this helped you out Steven!
Pingback: sharepoint public website customization- bloody hell! « Rocky`s World
Hi, I’ve tried this on SharePoint 2016 on-prem and I just get an “error” message when I load the page with the CQWP on it. Any ideas appreciated?
Hi Julie, this was for 2010. I have not had to perform this sort of rocket surgery since then (thankfully!). I would check the versioning of the assemblies. This has version 14.0.0.0, however, 2016 is 16.0.0.0, and that may be the source of your problem. Its trying to call 2010 DLLs from 2016. Other than that, I would check ULS and Event logs to see if anything is being logged there causing the issue. Hope this helps!