Monday, 22 September 2008

How to get SPItem's "Created by" SPUser

I wanted to get the user who created a certain item and I found a simple sulution.

// Get the 'Created By' string.
string userValue = currentSPItem["Created By"].ToString();

// Get index of ';'
int index = userValue.IndexOf(';');

// Get id which is number before ';', so Substring from 0 to index
int id = Int32.Parse(userValue.Substring(0, index));

// Get the current user
SPUser currentSPUser = currentSPWeb.SiteUsers.GetByID(id);

1 comment:

Anonymous said...

web.SiteUsers.GetByID(Int32.Parse(item["Created By"].ToString().Split(';')[0]))