Error:"The user doesn't exist or is not unique" in sharepoint Adding user using CSOM

Error:"The user doesn't exist or is not unique" in sharepoint Adding user using CSOM

我正在尝试使用 CSOM 将用户添加到 SharePoint 网站,但我收到一条说明 'The user does not exist or is not unique' 我的登录名有问题吗?。 "anil@developer19.onmicrosoft.com" 是组织中的活跃用户。

using System;
using System.Security;
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;
namespace CreateSiteCollections
{
    class Program
    {
        static void Main(string[] args)
        {
           //Opens the Admin URL 
            using (ClientContext ctx1 = new ClientContext("https://developer19.sharepoint.com/sites/codesite"))
            {
                //Authenticating with Tenant Admin
                SecureString passWord = new SecureString();
                foreach (char c in "Password".ToCharArray())
                    passWord.AppendChar(c);
                ctx1.Credentials = new SharePointOnlineCredentials("kailash@developer19.onmicrosoft.com", passWord);
                        GroupCollection cg = ctx1.Web.SiteGroups;
                        Group oGroup = cg.GetById(14);

                        UserCreationInformation user = new UserCreationInformation();
                        user.Email = "anil@developer19.onmicrosoft.com";
                        user.LoginName = "developer19/anil";
                        user.Title = "Anil";
                        User oUser = oGroup.Users.Add(user);
                        ctx1.ExecuteQuery();
            }
      }
   }
}

我在 http://www.codesharepoint.com/csom/add-user-to-site-group-in-sharepoint-using-csom 上学会了其他方法,而不是尝试这些整个步骤。但是您可以同时使用 GetByName 或 GetById。

Group oGroup = ctx.Web.SiteGroups.GetByName("NewCSOM Group");
                User oUser = ctx.Web.EnsureUser("anil@developer19.onmicrosoft.com");
                oGroup.Users.AddUser(oUser);
                ctx.ExecuteQuery();

用户的登录名可以用Rest检查API:

siteurl/_api/web/siteusers

因此对于 UserCreationInformation class,更新有效的登录名将起作用: