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

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

13 Replies to “Resetting the Author on a SharePoint site, or Wherefore Art Thou Author Redux”

  1. 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

  2. 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.

  3. 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.

  4. Pingback: Sharepoint site
  5. I know it probably isn’t a big deal in this instance, but the SPSite and SPWeb objects needs to be disposed.

  6. In this instance, all we are doing is resetting the values, and then the app closes, so they’re immediately disposed..but you are correct, if you were to use this code in a method from something else, you bet, you would want to free those up asap.

  7. jeder diewurde gesehen diefashion Schals Teil nur alle diie meisten wichtig Großhandel einkaufen, die beidenhaben wahrscheinlich bemerkt der, diejenigen Menschen,
    die Suche innerhalb dieser Abschnitt neigen zu Frauen.
    Plohemd Lacost Da, Bekleidung und darüber
    hinaus im Berdeich Bekleidung Anbhieter getkommenin fassen Sie jederBedeutungvon Suche nach Menschen Mit dem Kombinationenvon busiess Weisheit und
    auch schöne mode sense. ein weiterer erhebliche ÄnderungZwischen US
    undFranzösische Mode kannwichtig für Universität
    Studenten.

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 )

Connecting to %s

%d bloggers like this: