Skip to content

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

September 12, 2008

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

About these ads
13 Comments leave one →
  1. October 13, 2008 11:02 pm

    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. October 16, 2008 3:09 am

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

    Once again thank you for the solution.

  3. October 31, 2008 11:10 am

    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.

  4. December 7, 2008 11:43 pm

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

  5. chris buchholz permalink
    June 6, 2009 2:09 am

    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.

    • June 23, 2009 8:51 pm

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

      • Neha permalink
        January 8, 2010 2:34 am

        Hi Keith,

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

  6. Joe Winder permalink
    April 15, 2011 11:26 pm

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

  7. April 15, 2011 11:31 pm

    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.

  8. October 2, 2013 8:42 pm

    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.

Trackbacks

  1. Links (9/18/2008) « Steve Pietrek - Everything SharePoint
  2. Sharepoint site
  3. SharePoint Pros » Configuring SQL Report Services on SharePoint Farm

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

Follow

Get every new post delivered to your Inbox.

Join 1,624 other followers

%d bloggers like this: