9

I'm trying to get the creator of an item, but when I access the CreatedBy property, it returns an empty string. When I debugged I could see the all the other fields were populated, but not the CreatedBy:

enter image description here

var contextDatabase = Sitecore.Context.Database;
Item blogPost = contextDatabase.GetItem(data.ID);
var creator =  blogPost.Statistics.CreatedBy;

I really don't know why it shows as empty. Any idea why?

Edit: In the content tree, in the statistic field section, the CreatedBy is there:

enter image description here

3
  • 1
    can you try in this way: blogPost.Fields["__created by"].Value Commented Oct 20, 2017 at 15:13
  • @SitecoreClimber Yeah that worked, thanks. I still dont understand why the other way doesnt work, that;s weird. Commented Oct 20, 2017 at 15:25
  • 1
    I am transforminng my comment in an answer. Commented Oct 20, 2017 at 15:28

1 Answer 1

8

"Statistics.CreatedBy" property does not return standard values.

To fix this problem please use : blogPost.Fields["__created by"].Value

Statistics.Created use next code:

private string GetFieldValue(ID fieldId)
{
    Field field = this._item.Fields[fieldId];
    return field.GetValue(false, false) ?? string.Empty;
}

where field.GetValue has next parameters

public virtual string GetValue(bool allowStandardValue, bool allowDefaultValue)
3
  • Wut? :D So if Creator happens to be the same account as the one who created the Standard Values item, this applies?
    – Mark Cassidy
    Commented Oct 20, 2017 at 15:37
  • Oh I see, thanks really!. Do you know where can I get the API documentation. I've looked for it everywhere, with no luck at all. Commented Oct 20, 2017 at 15:38
  • Also, why is it there? the "Statistics.CreatedBy" if it's not gonna be used? Commented Oct 20, 2017 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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