SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

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.