azure 在C#中从SharePoint网站删除用户

odopli94  于 2023-05-07  发布在  C#
关注(0)|答案(1)|浏览(244)

我需要在C#. net中从SharePoint网站集中删除用户。这里是powershell代码的链接,但我需要用C#编写。https://learn.microsoft.com/en-us/sharepoint/remove-users
下面的代码不工作。

private void RemoveUser(String user)
 {
    using (SPSite sps = new SPSite("http://serverName"))
            {
                using (SPWeb spWeb = sps.RootWeb)
                {
                    SPUserCollection spuc = sps.RootWeb.AllUsers;
                    spuc.Remove(user);
                }
            }
        }
j2cgzkjk

j2cgzkjk1#

我有一个名为**sridemosite**的SharePoint网站,成员如下:

来自SharePoint管理中心:

来自SharePoint门户:

要在C#.net中从SharePoint站点集合中删除用户,我使用了以下代码:

using System;
using System.Linq;
using System.Security;
using Microsoft.SharePoint.Client;

namespace CSOMAuthentication
{
    class Program
    {
        static void Main(string[] args)
        {
            // Provide Site URL
            string SiteURL = "https://xxxxxxxxxxx.sharepoint.com/sites/sridemosite";
            string Environmentvalue = "o365";
            string username = "admin@xxxxxxxxxx.onmicrosoft.com";
            string password = "xxxxxxxxxxx";
            AuthenticateUser(new Uri(SiteURL), Environmentvalue, username, password);
        }

        private static void AuthenticateUser(Uri TargetSiteUrl, string Environmentvalue, string username, string password)
        {
            try
            {
                // Based on the environmentvalue provided it execute the function.
              if (string.Compare(Environmentvalue, "o365", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    ClientContext Context = O365LogOn(username, password, TargetSiteUrl);

                    Group oGroup = Context.Web.SiteGroups.GetByName("sridemosite Members");

                    // Get user using Logon name
                    User oUser = Context.Web.EnsureUser("devi@xxxxxxxxxxxx.onmicrosoft.com");

                    Context.Load(oUser);
                    Context.ExecuteQuery();

                    // Remove user from the group
                    oGroup.Users.RemoveByLoginName(oUser.LoginName);
                    
                    Context.ExecuteQuery();         
                }  
            }
            catch (Exception ex)
            {
                // log error               
            }
        }

        private static ClientContext O365LogOn(string userName, string password, Uri url)
        {
            ClientContext clientContext = null;
            ClientContext ctx = null;
            try
            {
                clientContext = new ClientContext(url);

                // Condition to check whether the user name is null or empty.
                if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
                {
                    SecureString securestring = new SecureString();
                    password.ToCharArray().ToList().ForEach(s => securestring.AppendChar(s));
                    clientContext.Credentials = new SharePointOnlineCredentials(userName, securestring);
                    clientContext.ExecuteQuery();
                }
                else
                {
                    clientContext.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
                    clientContext.ExecuteQuery();
                }
                ctx = clientContext;
            }
            finally
            {
                if (clientContext != null)
                {
                    clientContext.Dispose();
                }
            }
            return ctx;    
        }
    }
}

答复:

为了确认这一点,我在两个门户中进行了相同的检查,其中**Devi**用户成功地从网站集中删除,如下所示:

来自SharePoint管理中心:

来自SharePoint门户:

参考文献:

Remove User From Site Group in SharePoint using CSOM - Code SharePoint
Connect to SharePoint Online, On premise and Extranet using CSOM - Code SharePoint

相关问题