SharePoint SPWeb.EnsureUser fails to resolve when users have same display name

Veda Selvam 20 Reputation points
2023-02-16T23:12:16.6066667+00:00

I am trying to store and retrieve SharePoint Users in a list.

Say user accounts - A and A1 - both have same display name 'A'. However their email addresses are different - ******@domain.com - ******@domain.com.

After storing the user account A1 in list, when I retrieve I get the error 'the specified user could not be found'. Other accounts seem fine.


AD users:

dark-knight-users

iron-man-user


Here's the list data:

list-data


Web part view:

webpart


Here's user control code:

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VisualWebPart1UserControl.ascx.cs" Inherits="VisualWebPartProject2.VisualWebPart1.VisualWebPart1UserControl" %>

<div class="form-group" id="divudn" style="width: 750px;">
    <label class="required control-label col-md-2">Marvel</label>
    <div class="col-md-10">
        <SharePoint:PeopleEditor runat="server" ID="peMarvel" Width="100%" AllowEmpty="true" ValidatorEnabled="false" MultiSelect="false" SelectionSet="User,SecGroup,SPGroup" />
    </div>
</div>

<div class="form-group" id="divsdn" style="width: 750px;">
    <label class="required control-label col-md-2">DC</label>
    <div class="col-md-10">
        <SharePoint:PeopleEditor runat="server" ID="peDC" Width="100%" AllowEmpty="true" ValidatorEnabled="false" MultiSelect="false" SelectionSet="User,SecGroup,SPGroup" />
    </div>
</div>

<label class="control-label col-md-2" id="logLabel" runat="server"></label>

Here's is the code-behind:

using Microsoft.SharePoint;
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace VisualWebPartProject2.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            using (SPSite cSite = new SPSite("http://portal.site.com/sites/home1"))
            {
                using (SPWeb cWeb = cSite.OpenWeb())
                {
                    SPList grp = cWeb.Lists.TryGetList("group");
                    SPUser user = null;
                    foreach (SPListItem item in grp.Items)
                    {
                        string logonName = item["owner"].ToString().Split('#')[1];
                        try
                        {
                            // Error at this line
                            user = cWeb.EnsureUser(logonName);
                            
                            switch(item.Title) 
                            {
                                case "DC":
                                    peDC.CommaSeparatedAccounts = user.LoginName;
                                    break;
                                case "Marvel":
                                    peMarvel.CommaSeparatedAccounts = user.LoginName;
                                    break;
                            }
                        }
                        catch (Exception ex)
                        {
                            logLabel.InnerText = ex.Message + ex.StackTrace + ex.InnerException;
                        }
                    }
                        
                }
            }
        }
    }
}
Microsoft 365 and Office | SharePoint Server | For business
Microsoft 365 and Office | SharePoint | For business | Windows
Microsoft 365 and Office | SharePoint Server | Development
0 comments No comments
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2023-02-17T06:43:39.2933333+00:00

    Hi @Veda Selvam ,

    Per my test, I can ensure the user by logon name. The format of the logonName should be like following in SharePoint 2013

    i:0#w|<domainname>\<username>

    However, If two users have same display name. SPWeb.EnsureUser will return 'the specified user could not be found'. I will recommend you to set unique display name for users.

    Here is a issue similar with yours. Please make a reference.

    https://github.com/Microsoft/mattercenter/issues/57


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.