EnsureUser using email address in SharePoint client object model

Page F.P.T picture Page F.P.T · Jul 2, 2015 · Viewed 14.1k times · Source

I need to update a FieldUserValue field in sharepoint 2013. i am only given an email address data. I can't user EnsureUser since it only accepts the logonName. i used the FromUser method but it gives me an error that says "the user does not exist or is not unique"

FieldUserValue user = FieldUserValue.FromUser(email);

it worked when i tried using my email address but when i use the email addresses in my data it results in an error. how do i fix the issue?

Answer

Vadim Gremyachev picture Vadim Gremyachev · Jul 2, 2015

You could resolve user by email address using Utility.ResolvePrincipal method, for example:

var result = Microsoft.SharePoint.Client.Utilities.Utility.ResolvePrincipal(ctx, ctx.Web, emailAddress,Microsoft.SharePoint.Client.Utilities.PrincipalType.User,Microsoft.SharePoint.Client.Utilities.PrincipalSource.All, null, true);
ctx.ExecuteQuery();
if (result != null)
{
    var user = ctx.Web.EnsureUser(result.Value.LoginName);
    ctx.Load(user);
    ctx.ExecuteQuery();    
} 

References

Get user identity and properties in SharePoint 2013