将所有用户从活动目录获取到sharepoint

时间:2011-10-25 08:43:48

标签: sharepoint sharepoint-2010 active-directory peoplepicker

我必须使用来自我的AD域中的所有人来填充基于brililant ASPTokenInput的类似于PeopleEditor的自动完成控件。反映PeopleEditor在他们的Active Directory搜索引擎中显示了一个真正的混乱,所有可能有用的类都是内部的。

我的测试方法运行正常,但我需要从AD(而不是sharepoint站点)中获取所有用户来填充我的列表: How it looks

public string GetUsers(string filter)
    {
        var spWeb = SPContext.Current.Web;
        SPUserCollection allusers = spWeb.AllUsers;
        List<SPUser> users = allusers.Cast<SPUser>().ToList();
        var query = from spUser in users.Select(usr => new {id = usr.ID, name = usr.Name})
                        .Where(p => p.name.IndexOf(filter, StringComparison.InvariantCultureIgnoreCase) >= 0)
                    select new {id = spUser.id.ToString(), spUser.name};

        return new JavaScriptSerializer().Serialize(query);
    }

如何像这样查询活动目录?是否可以从sharepoint本身检索所有AD连接设置?我只需要id和用户名来填充我的下拉列表将其转换为SPUserCollection是另一件大事。

使用像这样的内置SP方法会很棒:

 [SubsetCallableExcludeMember(SubsetCallableExcludeMemberType.UnsupportedSPType)]
public static IList<SPPrincipalInfo> SearchWindowsPrincipals(SPWebApplication webApp, string input, SPPrincipalType scopes, int maxCount, out bool reachMaxCount)

2 个答案:

答案 0 :(得分:1)

解决方案很简单,我唯一需要的是SharePoint Group搜索实现(如果在Field Editor Control中指定)。 SP有一个很好的内置方法,所以我使用它。

/// <summary>
/// Provides searching for AD or SharePoint group if specified in field setting
/// </summary>
public static class ActiveDirectorySearchProvider
{
    public static IList<SPPrincipalInfo> Search(string filter, string selectionGroup, string principalType)
    {
        var site = SPContext.Current.Site.WebApplication;
        bool reachmaxcount;
        var scope = SPUtils.GetSpPrincipalType(principalType);

        if (!String.IsNullOrEmpty(selectionGroup)) //search for users in SPGroup if present
        {
            var rawSPGroupList = SPUtility.GetPrincipalsInGroup(SPContext.Current.Web, selectionGroup, 100,
                                                           out reachmaxcount).ToList();

            string lowerFilter = filter.ToLowerInvariant();

            var filteredGroupList =
                rawSPGroupList.Where(
                    pInfo =>
                    pInfo.LoginName.Substring(pInfo.LoginName.IndexOf('\\') + 1).StartsWith(lowerFilter) ||
                    pInfo.DisplayName.ToLowerInvariant().StartsWith(lowerFilter) ||
                    pInfo.DisplayName.ToLowerInvariant().Substring(pInfo.DisplayName.IndexOf(' ') + 1).StartsWith(
                        lowerFilter)).ToList();

            return filteredGroupList;
        }

       return SPUtility.SearchWindowsPrincipals(site, filter, scope, 100, out reachmaxcount); //Search in AD instead

    }

答案 1 :(得分:0)

我可以在这里想到两个选择。

  1. 您可以使用System.DirectoryServices并在c#代码中查询Active Directory中的所有用户。

  2. 您可以设置User Profile Sync,以便SharePoint用户数据库是最新的。