Get Friendly User Name For SPAuditEntry UserId

I saw this post on the forums:

Where the user states (paraphrased):

I’ve been tasked with delivering a custom auditing solution in Sharepoint 2007 and become a little stuck.

Is there an easy way to translate a UserID (GUID) – returned from calling an instance of SPAuditEntry.UserID – into the corresponding SPUser.Name?


string userName = SPUser.getNameByUserID(userID);

The long and short of the answer is, kind of. This is how I would go about it.

When doing this, you probably have a SPAuditEntryCollection that you are hydrating with entries using the SPSite.Audit.GetEntries method which takes a SPAuditQuery object as a parameter. You can build said SPAuditQuery objects by placing a SPSite object in as a parameter, then following setting the property constraints i.e. SPAuditQuery l_oQuery = new SPAuditQuery(SPSite). Therefore, you already are hydrating a SPSite reference within your code that should be available later. When looping through your SPAuditEntry objects in your SPAuditEntryCollection and trying to get the user name through the properties, the user name, as the person who asked the question alluded to, is not directly available through the properties. However, since you have your site reference, you can just do the following to get the User Name instead of the not entirely readable User Id:
using (SPSite l_oSite = new SPSite(“”)
SPAuditQuery l_oQuery = new SPAuditQuery(l_oSite);
SPAuditEntryCollection l_oReport = l_oSite.Audit.GetEntries(l_oQuery);
foreach (SPAuditEntry l_oEntry in l_oReport)

string l_strUserName = l_oSite.OpenWeb().Users.GetByID(l_oEntry.UserId).ToString();



That should return the username.