Krichie – That SharePoint Guy

September 12, 2008

Resetting the Author on a SharePoint site, or Wherefore Art Thou Author Redux

Filed under: SharePoint — Keith Richie @ 7:32 pm

In my previous post Wherefore Art Thou Author? I discussed a problem whereby removing a user from a site collection will cause a “User not found exception” when trying to access the Author property of any web they may have created.

In this post, I’ll provide a supportable solution to resolving this without having to revert to modifying the SharePoint content databases directly.

The following code can be used to reset the author of a SharePoint site:

// BEGIN SPResetAuthor
using System;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace SPResetAuthor
{
  class Program
  {
    static void Main(string[] args)
    {
      if (args.Length < 2)
      {
        string sFormat = "{0,5}{1,-20}{2}\n";
        StringBuilder sb = new StringBuilder();
        sb.Append("\nSYNTAX: SPResetAuthor.exe   \r\n");
        sb.Append("\nwhere:\n\n");
        sb.AppendFormat(sFormat, " ", "", "The Web URL where"
            + " the author needs to be reset");
        sb.AppendFormat(sFormat, " ", "", "The Login Account"
            + " for the new author, i.e. DOMAIN\\USER");
        Console.WriteLine(sb.ToString());
        return;
      }

      // Open the site collection
      Console.WriteLine("Opening site collection for: {0}",args[0]);
      SPSite site = new SPSite(args[0]);

      Console.WriteLine("Opening site (web)...");
      // Open the web via the URL passed in
      SPWeb web = site.OpenWeb();
      Console.WriteLine("Site (web) title: " + web.Title);

      Console.WriteLine("Resetting Author to: {0}",args[1]);
      SPUser user = web.EnsureUser(args[1]);
      web.Author = user;
      web.Update();

      Console.WriteLine("Author for site {0}, reset to {1}", args[0], args[1]);
      Console.WriteLine("Author ID: " + web.Author.ID.ToString());
      Console.WriteLine("Author Name: " + web.Author.Name);
    }
  }
}
// END SPResetAuthor

To build this, simple execute the following at the same location where you save the source.

%WINDIR%\Microsoft.NET\Framework\v2.0.50727\csc /target:exe /out:SPResetAuthor.exe SPResetAuthor.cs
/reference:"%COMMONPROGRAMFILES%\Microsoft Shared\web server extensions\12\ISAPI\Microsoft.SharePoint.dll"

The syntax is as follows:

SYNTAX: SPResetAuthor.exe  

where:

                 The Web URL where the author needs to be reset
              The Login Account for the new author, i.e. DOMAIN\USER

When executed, the results should resemble the following:

C:\Dev\SPResetAuthor>SPResetAuthor.exe http://krichiedev/deadauthorsite krichie\user1
Opening site collection for: http://krichiedev/deadauthorsite
Opening site (web)...
Site (web) title: DeadAuthorSite
Resetting Author to: krichie\user1
Author for site http://krichiedev/deadauthorsite, reset to krichie\user1
Author ID: 13
Author Name: Test User One

Hope this helps!

– Keith

10 Comments »

  1. [...] Resetting the Author on a SharePoint site, or Wherefore Art Thou Author Redux Possibly related posts: (automatically generated)APAC SharePoint Conference 2007Office 2007, SharePoint Server 2007 and Windows SharePoint Services V3 Link…SharePoint Interview QuestionsMicroLink Launches Its MicroLink Autonomy Integration Suite for SharePoint [...]

    Pingback by Links (9/18/2008) « Steve Pietrek - Everything SharePoint — September 19, 2008 @ 6:57 am

  2. Thank you and sorry, I had not had a chance to even check back to see you posted a solution until now (when yet another client is being bitten by poorly written code for SSRS integration).

    For the non developers out there this might be a little much, but it should solve the “author” issue. I will pass this along to the client.

    Of course there is not good way from SSRS to see which author is broken, since it works up the tree using the parent information, then finds one and just stops.

    Thank you,
    Eric VanRoy

    Comment by Eric VanRoy — October 13, 2008 @ 11:02 pm

  3. And finally today, I find the email you sent on 9/12 in my Junk email.

    Once again thank you for the solution.

    Comment by Eric VanRoy — October 16, 2008 @ 3:09 am

  4. Great post! I seem to encounter this problem a lot with SQL Server Reporting Services. This utility is a life-saver.

    I modified your code a bit to allow you to show the current author instead of just resetting it.

    Comment by Aaron Dutton — October 31, 2008 @ 11:10 am

  5. Nice post. Thank you for the info. Keep it up.

    Comment by Tim Reynolds — December 7, 2008 @ 11:43 pm

  6. I came up with a similar solution. However have you been able to fix the Site owner? I mean SPSite primary and seconary owners. I have quite a few MySites that have owners set to users in an old domain, and stsadm -o migrateuser is not working.

    I can set the author fine, but if I try to change the SPSite.Owner field it still gives me the same “user cannot be found” exception, even though the user exists I just used it to set the Author field! So I assume the user that is not found may be the old owner, and for some reason it is checking the value of the old owner. I get the same result using stsadm -o siteowner.

    Comment by chris buchholz — June 6, 2009 @ 2:09 am

    • migrateuser should have taken care of that. When setting SPSite.Owner, does the users already exist in the SiteUsers collection?

      Comment by Keith Richie — June 23, 2009 @ 8:51 pm

      • Hi Keith,

        Is it possible similar way to update the SPList’s Author property.. ?

        Comment by Neha — January 8, 2010 @ 2:34 am

  7. [...] Resetting the Author on a SharePoint site, or Wherefore Art Thou … [...]

    Pingback by Sharepoint site — July 18, 2009 @ 3:30 pm


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.