Hi I am trying to access the created by coloumn in item added event reciever.But when i debug it shows null value...below is the code

public override void ItemAdded(SPItemEventProperties properties)
   var CreatedBy = (string)properties.AfterProperties["Author"];
   // ....

Try using ListItem instead of AfterProperties

var CreatedBy = (string)properties.ListItem["Author"];

I believe the AfterProperties are for the synchronous events (end in ING - e.g. Adding, Deleting, etc.)

Furthermore you can get an SPUser object from this. Refer to the following site for details on how that might be done. Once you have an SPUser object you have access to all sorts of useful properties such as Name, Email, and more.

|improve this answer|||||
  • I i am getting the CreatedBy value as 9;#domain/username ..i am little confuse as to from where the value 9;# is coming from? – spStacker Jan 24 '12 at 22:34
  • 3
    A user field like that is essentially a lookup to the users list within the SharePoint Site Collection. 9 is the id of the user in the list, the ;# is a separator (and is the syntax used for all lookup fields) and domain\username is the value of the lookup. – SpartanDonut Jan 24 '12 at 22:38
  • 2
    @spStacker you can parse lookup field by using following code fragments: var id = new SPFieldLookupValue(fieldValue).LookupId and var value = new SPFieldLookupValue(fieldValue).LookupValue – Andrey Markeev Jan 24 '12 at 22:42
  • No Problem! I updated my answer with a link on how you can get an SPUser object from that value as well. – SpartanDonut Jan 24 '12 at 22:42
  • Excellent, Thanks guys for such prompt and detailed help! – spStacker Jan 24 '12 at 22:48

are you trying to access the value in the specific field? if so, try something like this:

using (SPWeb web = SPContext.Current.Web)
                SPList list = web.Lists["yourListHere"];
                SPListItemCollection items = list.GetItems();
|improve this answer|||||

The better way is use the property UserLoginName, that returns the user login which fires the event, example below:

SPUser authorUser = web.EnsureUser(properties.UserLoginName);

//properties is the SPItemEventProperties properties
//SPWeb is properties.OpenWeb();
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.