Sharepoint ResolvePrincipals function
I'm trying to add a user, programmtically, to a field of Type="User" in a SharepointList. Since I do not know the user's unique ID within the site in advance, I'm using the ResolvePrincipals function to add the user to the SPUserCollection as follows:
Dim managerDN() As String = {"some.user@email.com"}
Dim principalInfo() As PrincipalInfo = people.ResolvePrincipals(managerDN, SPPrincipalType.User, True)
Console.WriteLine(principalInfo(0).UserInfoID)
开发者_如何学C
Problem is when I look at the UserInfoID, which is what I'm looking for, I get back -1. I assumed that the ResolvePrincipals function would add the user to the site user collection automatically (According to MSDN documentation) and create a unique, positive UserInfoID in the process. I'm not sure if I have the right idea or not
I always use this code:
SPUser user = web.EnsureUser("login");
SPFieldUserValue value = new SPFieldUserValue(web, user.ID, user.LoginName);
If you only have an email address, you could use this:
SPUser user = web.AllUsers.GetByEmail("email");
if (user != null)
{
SPFieldUserValue value = new SPFieldUserValue(web, user.ID, user.LoginName);
}
I'm not really sure if GetByEmail returns null or throws an error if the user cannot be found, so be sure to check that first !
精彩评论