Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all users from active directory to sharepoint

I have to populate my autocomplete PeopleEditor-like control based on brililant ASPTokenInput with all people from my AD domain. Reflecting PeopleEditor shows a real mess in their Active Directory search engine and all potentially helpful classes are internal.

My test method works fine, but I need to get ALL users from AD(not sharepoint site ones) to populate my list: 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);
    }

How can I query active directory like this? Is it possible to retrieve all AD connection settings from sharepoint itself? I need just id and user name to fill my dropdownlist Converting this to SPUserCollection is another big deal.

It would be great to use some built-in SP methods like this:

 [SubsetCallableExcludeMember(SubsetCallableExcludeMemberType.UnsupportedSPType)]
public static IList<SPPrincipalInfo> SearchWindowsPrincipals(SPWebApplication webApp, string input, SPPrincipalType scopes, int maxCount, out bool reachMaxCount)
like image 868
Bassist Avatar asked Oct 25 '11 08:10

Bassist


People also ask

How do I add people to SharePoint in bulk?

Under Groups, click the name of the group in which you want to add users. On the menu bar, click the arrow next to the New button, and then click Add Users. In the Users/Groups box, type the name of the user you want to add. To add multiple users, type their names separated by a semicolon.

How does SharePoint sync with Active Directory?

On the Manage Profile Service page, in the Synchronization section, click Configure Synchronization Settings. On the Configure Synchronization Settings page, in the Synchronization Options section, select the Use SharePoint Active Directory Import option, and then click OK.

How can I get a list of access to SharePoint sites?

Login to https://www.office.com >> In the App Launcher, click on “SharePoint” to get the SharePoint Online start page. On the search box, type “contentclass:STS_Site” and hit enter. Click on the “Sites” tab, and this will show all sites you have access to.


1 Answers

Solution was simple, the only thing I needed was SharePoint Group search implementation (If specified in Field Editor Control). SP has a nice built-in method, so I use it.

/// <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

    }
like image 182
Bassist Avatar answered Sep 28 '22 09:09

Bassist