20 March, 2014

Listing User Profile Accounts from SharePoint 2013

Today, I am doing a thought experiment / learning exercise. The goal is to come up with a few different ways to display data from SharePoint’s User Profile Service.

Thought A – Manage User Profiles within Central Admin.


From the Manage User Profile page within Central Administration, I can search for a user or a group of user. For example, if SharePoint is setup to use claim based identity then a search phrase “i:0” will give list of users.


While using this method is fast and easy trying to get a list of all the accounts or to create a file with all the accounts in it is difficult.

Thought B - PowerShell



With a simple PowerShell script, it is easy to get a list of all the user profiles. What is really nice about this method is it is very simple to pipe the results of the GetEnumerator method to filter the results. PowerShell has built-in functionality that makes creating a file with the account information in it a snap.


add-pssnapin "Microsoft.Sharepoint.Powershell"

$siteList = Get-SPSite 
$serviceContext = Get-SPServiceContext($siteList[0])
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext)

foreach($usrProfile in $profileManager.GetEnumerator()) {
    Write-Host $usrProfile.AccountName "|" $usrProfile.DisplayName
}

Thought C – Visual Studio


Referencing the correct libraries and little bit of code, it very simple to get back a list of all the accounts within User Profile. With a little bit of extra code searching the accounts, searching for a specific account or writing the accounts to a file is no problem.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System.Diagnostics;

namespace UserProfileList
{
    class Program
    {
        static void Main(string[] args)
        {
            var siteList = new SPSite("http://sp2013");
            SPServiceContext svcCtx = SPServiceContext.GetContext(siteList);
            var profileMgt = new UserProfileManager(svcCtx);
            var userProfiles = profileMgt.GetEnumerator();

            while (userProfiles.MoveNext())
            {
                UserProfile userProfile = (UserProfile)userProfiles.Current;
                
                Console.WriteLine(userProfile.AccountName);
            }

            Console.ReadLine();
     }
    }
}


Wrap Up

This was a fun experiment in figuring out different ways to do the same task. It showed me a bit about the effort it takes to get a result. Using the out of the box functionality got me to a result very fast and it should cover 80% of the needs around looking up a user or a small group of users. PowerShell makes it simple and easy to get a list of users. 



No comments:

Challenging myself to learn something new

I have recently set a big challenge for myself. I want to know about Machine Learning . To add to the challenge, I am trying out usin...