Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm trying to get Document Authors (Created By) Email Id using SPMetal generated class code.

It gives DocumentCreatedBy as one of the Sharepoint Foundation Base field.

When I try to get LoginName from that field I'm getting the following error:

Value does not fall within the expected range

Here is my code...

DocumentsDocument docItem
SPFieldUserValue usrValue = new SPFieldUserValue(web, docItem.DocumentCreatedBy);

//I tried this too...But no luck....:(
//SPFieldUserValue usrValue = new SPFieldUserValue(web, "Created_x0020_By");

SPUser docUser = usrValue.User;
var usrEmail = docUser.LoginName;
share|improve this question

migrated from stackoverflow.com Mar 31 '12 at 0:38

This question came from our site for professional and enthusiast programmers.

2 Answers 2

you can get the userid or DisplayName from a USER field with Linq2Sharepoint.

write a partial class like this.

public partial class DocumentsDocument 
{
    private string _sysAuthor;
    [Microsoft.SharePoint.Linq.ColumnAttribute(Name = "Created_x0020_By", Storage = "_sysAuthor", ReadOnly = true, FieldType = "User", IsLookupValue = true)]
    public string SysAuthor
    {
        get
        {
            return this._sysAuthor;
        }
        set
        {
            if ((value != this._sysAuthor))
            {
                this.OnPropertyChanging("SysAuthor", this._sysAuthor);
                this._sysAuthor = value;
                this.OnPropertyChanged("SysAuthor");
            }
        }
    }

    private System.Nullable<int> _sysAuthorId;
    [Microsoft.SharePoint.Linq.ColumnAttribute(Name = "Created_x0020_By", Storage = "_sysAuthorId", ReadOnly = true, FieldType = "User", IsLookupId = true)]
    public System.Nullable<int> SysAuthorId
    {
        get
        {
            return this._sysAuthorId;
        }
        set
        {
            if ((value != this._sysAuthorId))
            {
                this.OnPropertyChanging("SysAuthorId", this._sysAuthorId);
                this._sysAuthorId = value;
                this.OnPropertyChanged("SysAuthorId");
            }
        }
    }
}

then you can retrieve a SPUser like this

SPUser user = web.AllUsers.GetByID(docItem.SysAuthorId);
share|improve this answer

Are you able to run a debug session? If you look at the usrValue object, what's returned? The display name is Created By but I believe the internal field name is Author.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.