1

I have an event receiver to capture the editor of an updated item.

string Editor = properties.ListItem["Editor"].ToString().Trim();

From the above code i can get the user name. But I want the active directory user id.

How can I get it? Thanks in advance

  • why do you want the id of the user from AD? what if there are two or three or four Jon Smiths? – Ali Jafer Sep 5 '16 at 10:24
  • No, I want the user ID, not the user's name. from the code above i get the user's name – Anshu Sep 5 '16 at 10:35
2

still dont know why you want this as its not unique. you can do the following:

SPUser editor = new SPFieldUserValue(properties.Web, properties.ListItem["Editor"].ToString()).User;

string Editor = editor.ID;

its not the AD ID but the sitecollection user ID. to get AD user ID you need to query AD specifically using ldap or other means. like:

private UserPrincipal GetUser(String samAccountName)
{

    PrincipalContext principalContext = GetPrincipalContext;

    return UserPrincipal.FindByIdentity(principalContext, IdentityType.SamAccountName, samAccountName);

}

private UserPrincipal GetUser(String displayName)
{

    PrincipalContext principalContext = GetPrincipalContext;

    return UserPrincipal.FindByIdentity(principalContext, IdentityType.DisplayName, samAccountName);

}

private UserPrincipal GetUser(String identity, IdentityType identityType)
{

    PrincipalContext principalContext = GetPrincipalContext;

    return UserPrincipal.FindByIdentity(principalContext, identityType, identity);

}

http://social.technet.microsoft.com/wiki/contents/articles/19115.sharepoint-querying-active-directory-using-c-net.aspx

how to use above:

https://msdn.microsoft.com/en-us/library/bb344891(v=vs.110).aspx

as an example:

UserPrincipal getUser = GetUser(properties.ListItem["Editor"].ToString().Trim());
0

Ended up doing the following.

SPUser user = null;
string[] data = username.Split(new string[] { ";#" }, StringSplitOptions.None);
SPPrincipalInfo pinfo = SPUtility.ResolvePrincipal(web, data[1], SPPrincipalType.User, SPPrincipalSource.All, web.Users, false);
user = web.Users[pinfo.LoginName];
string[] unames = pinfo.LoginName.Split(new string[] { @"|xxxxx\" },StringSplitOptions.None);
string username = unames[1];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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