SharePoint 2010 401 Unauthorized Error23 May 2013
There are many, many reasons why SharePoint will throw a 401 error. Most of them are hard to track down and have absolutely no bearing on reality unless you’re a genius. So, your only hope is to track down obscure registry updates (hacks), scour the internet for random things other people have found or just give up. :-)
I just about pulled my hair out yesterday trying to get to the bottom of a 401 Unauthorized error I kept getting for my anonymous access-enabled web application. I’m not sure why, but I never tried debugging my application to see where the error was actually being thrown. I assumed it was on the IIS level or some SharePoint level, so I never tried debugging.
So, lesson #1 when getting 401 in SharePoint: debug your application.
Lesson #2: apparently accessing the DefaultPage property of a PublishingWeb requires elevated privileges. Here was my code:
That last line was throwing the 401. So, the solution was to wrap it in an SPSecurity.RunWithElevatedPrivileges call, like this:
Thanks to this post for the answer about re-opening the site and web inside the RunWithElevatedPrivileges code: http://henry-chong.com/2010/11/sharepoint-anonymous-access-and-publishing-web-default-page.