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

How can retrieve email address from the Infopath people/Group picker?When I add the people/Group picker into the infopath form, I only get 3 fields DisplayName,AccountId,AccountType.Can anyone show me how can I get the email either by configurations or by code.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You can query the UserProfileService using the accountid from your people picker.

I wrote a simple class to do this from my form's code behind. Here's a quick sample with just the relevent bits (I'm typing from memory as I don't have the code in front of me, but you should be able to figure it out)...

First, add a web reference to your machine's UserProfileService

http://[your server]/_vti_bin/userprofileservice.asmx

Then...

class UserInfo
{
    private UPSvc.UserProfileService _svc = null;
    private UPSvc.PropertyData[] _pData = null; 
    private string _domainLogin = string.Empty;
    public WorkEmail = string.Empty;

    public UserInfo( string domainLogin )
    {
        _domainLogin = domainLogin;
        _svc = new UPSvc.UserProfileService();
        _svc.UseDefaultCredentials  = true;
        _pData = _svc.GetUserProfileByName( domainLogin );

        int WORK_EMAIL = GetIndexOfProperty( "WorkEmail" );
        WorkEmail = GetADValue( WORK_EMAIL );

    }

    private string GetADValue( int idx )
    {
        return _pData[ idx ].Values[0].Value.ToString();
    }

    private int GetIndexOfProperty( string propertyName )
    {
        for( int i=0; i<_pData.Length; i++ )
        {
            if( _pData[i].Name.CompareTo( propertyName ) == 0 )
                    return i;
        }

        return -1;
    }
}

To call...

UserInfo ui = new UserInfo( "domain\username" ); string email = ui.WorkEmail;

Hope that helps!

Cheers, Bob

share|improve this answer
    
It is awesome bro.Thanks. –  Ybbest Dec 15 '10 at 7:27
add comment

Your Answer

 
discard

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

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