Wherefore Art Thou Author?

Just discovered this and thought I would share

Ever site (web) within a site collection has a property called Author which is set to the account that created the site (Wether it be via the SharePoint UI, or the OM).  To the best of my knowledge, this is not exposed in the UI.  So for example, if I’m logged in a “John Doe” to a site, and create a subsite, the Author property of the new subsite will be set to “John Doe”.

The problem, is that if “John Doe” is ever removed from the site collection, accessing this property will result in a User not found exception such as the following:

Unhandled Exception: Microsoft.SharePoint.SPException: User cannot be found.
at Microsoft.SharePoint.SPUserCollection.GetByID(Int32 id)
at Microsoft.SharePoint.SPWeb.get_Author()

This doesn’t present itself as a problem by just using the SharePoint UI, but if you have custom code that inspects this property, be sure to add some additional exception handling.The problem, is that if “John Doe” is ever removed from the site collection, accessing this property will result in an exception.

To see an example of this problem, do the following:

  1. Create a new site collection
  2. Add a test account with Full Control to the root site (Web) of this site collection
  3. Login to that site as the test account, and create a subsite (Subweb in OM talk)
  4. Run the following sample code that dumps out the fields of the Author property
  5. As a site collection admin, or another account with rights to manage permissions, remove the test account from the site collection via  “People and Groups: All People”
  6. Run the following sample code again, and you’ll see it throw the exception when you try to access any of the fields of the property.

Sample Code

using System;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace WhereforeArtThou
{
    class Program
    {
        static void Main(string[] args)
        {
            // Open the site collection
            Console.WriteLine("Opening site");
            SPSite site = new SPSite(args[0]);

            Console.WriteLine("Access properties from the web");
            // Open the web via the URL passed in
            SPWeb web = site.OpenWeb();
            Console.WriteLine("web title: " + web.Title);
            Console.WriteLine("Author ID: " + web.Author.ID.ToString());
            Console.WriteLine("Author Name: " + web.Author.Name);
        }
    }
}

Is it a bug?  No, not really IMHO, just something to watch out for.  Besides, if the user is removed from the site collection, who is the author supposed to be replaced with?  Regardless of who it was replaced with, the property will no longer hold a valid value of the original author of the site.

Hope this helps!

– Keith

5 thoughts on “Wherefore Art Thou Author?

  1. I wish Microsoft Reporting services authors would read this. The issue becomes pretty common when a document library, or site that contains reports for SSRS in Sharepoint integrated mode is accessed.

    I have not found a MS support solution. I have needed to modify the database directly, I am glad this is only in test (wink) since Microsoft does not support directly modifying data in the database.

    I just thought I would pass the information on.

    Eric VanRoy

  2. Ouch Eric, don’t do that. Modifying the databases is so unsupported. I’ll put you something together that will update the author in a supported fashion. Stand by for a followup to this Bat Post on this same Bat Blog.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s