views:

938

answers:

4

Hi all,

It seems like every effort talking to Sharepoint through its web services needs a domain\user name. For example:

UserProfileService.GetUserProfileByName(string accountName)

The accountName should be domain\user name.

If I only got full name (FirstName LastName), is it possible to communicate with SharePoint? Is there any way to convert the full name into domain\user name? Is domain\user name the only way to do it?

Thank you in advance. :)

+1  A: 

I suppose that you could try to query active directory for the user's first and last name to determine their account name.

joe.liedtke
Thank you. It works well.
ira
+1  A: 

try using UserGroup.GetAllUserCollectionsFromWeb(), can iterate the results to get the Login Name for a given display name.

Jason
A query to AD has been implemented before I read your answer. Thank you for the alternative answer, Jason!
ira
A: 

You can use a PeopleEditor control, which will grab the user if you type in the lastname, firstname combination, or you can browse through the directory.

people = new PeopleEditor();
people.MultiSelect = false;

this.Controls.Add(people);

...

int userID = Int32.Parse((((PickerEntity)people.ResolvedEntities[0]).EntityData["SPUserID"]).ToString());

SPUser user = SPContext.Current.Site.RootWeb.SiteUsers.GetByID(userID);

It's a bit cumbersome and ridiculous, but it works. If you need to get it programmatically, you can do as Jason said above and get the SPUserCollection and loop through, looking for the SPUser with the appropriate display name.

Andy Mikula
Thank you for contributing, Andy! :)
ira
A: 

Dear all,

Thank you for the answers. :)

The code looks like this:

using System.DirectoryServices;



const string ADPATH = "LDAP://myLDAPserver,validUserforAD";
const string USERNAME = "myDomain\\myUserName";
const string PASSWORD = "myPassword";
const string DOMAIN = "myDomain\\";

public static DirectoryEntry GetDirectoryObject()
{
        DirectoryEntry directoryObject = new DirectoryEntry(ADPATH, USERNAME, PASSWORD, AuthenticationTypes.Secure);
        return directoryObject;
}


public string GetUserNameByCompleteName(string completeName)
{
            DirectoryEntry adObject = GetDirectoryObject();

            //filter based on complete name
            DirectorySearcher searcher = new DirectorySearcher(adObject);
            searcher.Filter = "displayname=" + completeName;
            SearchResult result = searcher.FindOne();

            DirectoryEntry userInfo = result.GetDirectoryEntry();

            //getting user name
            string userName = (string)userInfo.Properties["samaccountname"].Value ?? string.Empty;
            userInfo.Close();
            adObject.Close();

            return DOMAIN + userName;
}
ira