2

I have created user profile application successfully and also I got all user profiles to Central Administration from AD. but now how do I save these all user profiles details into list in subsite.

I am looking for any solution COM/SOM.

I got some solution by PowerShell:

  #Add SharePoint PowerShell SnapIn if not already added 
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) { 
    Add-PSSnapin "Microsoft.SharePoint.PowerShell" 
} 


$site = new-object Microsoft.SharePoint.SPSite("http://abcd01:9999/");  
$ServiceContext = [Microsoft.SharePoint.SPServiceContext]::GetContext($site);  

#Get UserProfileManager from the My Site Host Site context 
$ProfileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)    
$AllProfiles = $ProfileManager.GetEnumerator() 

#Open SharePoint List 
$SPServer="http://abcd01:9999/" 
$SPAppList="/Lists/UPList/" 
$spWeb = Get-SPWeb $SPServer 
$spData = $spWeb.GetList($SPAppList)

foreach($profile in $AllProfiles)  
{
    #Create a new item
    $newItem = $spData.Items.Add()

    #Add properties to this list item
    $DisplayName = $profile.DisplayName
    $AccountName = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::AccountName].Value  

    $newItem["DispName"] = $DisplayName 
    $newItem["AccName"] = $AccountName

    write-host "Profile for account ", $AccountName 
    $newItem.Update()

}  

write-host "Finished." 
$site.Dispose() 

But in User profile service application showing 1700+ number of user profiles but I got 93 profiles to my list.

Anyone know why? How do I get remaining?

  • Why? You have REST access to the User Profiles. It makes no sense to store a copy in a list. – Matthew McDermott Oct 1 '15 at 16:24
  • @MatthewMcDermott My actual req is that I need to get all user profiles to SP list in my application one time . Later management can update the users info directly in the application. (Actually it is enough to export to list by using excel but admin not interested to provide the listed users info in excel format). Req is clear . no need of updations from AD. – Flying Hope Oct 6 '15 at 7:10
1

I have a complete solution for you using SOM. You have to develop a timer job for this. The timer job will sync AD users to you sub site's list.

public override void Execute(Guid targetInstanceId)
        {
            try
            {
                SPWebApplication webApplication = this.Parent as SPWebApplication;
                var config = WebConfigurationManager.OpenWebConfiguration("/", webApplication.Name);
                var syncPath = config.AppSettings.Settings["syncPath"].Value;
                var syncUserName = config.AppSettings.Settings["syncUserName"].Value;
                var syncPassword = config.AppSettings.Settings["syncPassword"].Value;
                var syncSiteUrl = config.AppSettings.Settings["syncSiteUrl"].Value;
                var adUserSyncHelper = new AdUserSyncHelper(syncPath, syncUserName, syncPassword, syncSiteUrl);
                adUserSyncHelper.Sync();
                adUserSyncHelper.RemoveItemsIfNotExistInAd();
            }
            catch (Exception ex)
            {
                //Handle exception here
            }
            base.Execute(targetInstanceId);
        }

// Sync implementation

 public void Sync()
        {
            using (var directoryInfo = new DirectoryEntry(SyncPath, UserName, Password))
            {
                var userFindingfilter = "(&(objectClass=user)(objectCategory=person))";
                var userProperties = new string[] { "title", "whenChanged", "displayName", "department", "sAMAccountName", "userPrincipalName", "manager" };
                using (var directoryInfoSearch = new DirectorySearcher(directoryInfo, userFindingfilter, userProperties, SearchScope.Subtree))
                {
                    var directoryEntryUserSearchResults = directoryInfoSearch.FindAll();
                    foreach (SearchResult searchResult in directoryEntryUserSearchResults)
                    {
                        var searchResultDirectoryEntry = searchResult.GetDirectoryEntry();
                        if (searchResultDirectoryEntry.Properties["manager"].Value == null)
                            continue;
                        var managerDnName = searchResultDirectoryEntry.Properties["manager"].Value.ToString();
                        var manager = new DirectoryEntry("LDAP://" + managerDnName);
                        SaveItemIfNotExists(searchResultDirectoryEntry, manager);
                    }
                }
            }
        }

Read the full article here.

If you need only once to get and save, then follow below answer

If you want to get all users of site then

_spPageContextInfo.webAbsoluteUrl + "/_api/Web/SiteUsers" If You want to display information of User in site then

_spPageContextInfo.webAbsoluteUrl + "/_api/Web/SiteUserInfoList" Display particular user information then

_spPageContextInfo.webAbsoluteUrl + "_api/web/GetUserById("+UserID+")" But in your case if you want to get all user's properties then you need to loop through and get user profile. You can also use CSOM by which you can get all user and Groups.

You can use this method to retrieve particular property of user (Reference)

SP.UserProfiles.PeopleManager.getUserProfilePropertyFor(accountName, propertyName)

See here

  • Hi Atish, I saw ur suggested article. that is for getting and saving users in list from AD. Here my case, I have already have user profiles in Central admin. then why again have to do operations on AD. -thanks. – Flying Hope Oct 1 '15 at 6:52
  • if you need to keep list users synchronized with AD, then you can follow my article. If you wish to get all AD users and save them only once, then you can follow next @Milind's answer – Atish Dipongkor - MVP Oct 1 '15 at 7:27
  • My requirement is, Only one time(first time) i have to get users to list. later we will not use user profiles. anyway thanks for ur time. – Flying Hope Oct 1 '15 at 7:38
  • see my updated code – Atish Dipongkor - MVP Oct 1 '15 at 8:01
  • @AtishDipongkor, Hi I had read your article, it exactly answers my needs. I had download your solution and put it to my farm solution. (I created a list "Active Directory Users") The time job "Sync OU users into a SharePoint list" works well, but there is no data in my list. Am I miss something ? – AnnaCICI Nov 5 '15 at 13:41
0

You don't have to create extra over head on SharePoint. User Profile Service can be directly retrieve by REST API or CSOM

REST API

1) Get all properties of current user:
http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties

2) Get single property of current user:
http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties/PictureUrl
OR
http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=PictureUrl

3) Get Multiple Properties for the current user:
http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=PictureUrl,AccountName

4) Get all properties of Specific User:

For Office 365/SharePoint Online:
http://siteurl/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='i:0%23.f|membership|vardhaman@siteurl.onmicrosoft.com'

For SharePoint 2013 On-Premise:
http://siteurl/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='domain\username'

5) Get Specific UserProfile Property of Specific User:

For Office 365/SharePoint Online:
http://siteurl/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='LastName')?@v='i:0%23.f|membership|vardhaman@siteurl.onmicrosoft.com'

For SharePoint 2013 On-Premise:
http://siteurl/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='LastName')?@v='domain\username'

6) Get Multiple UserProfile Properties for Specific User:

http://siteurl/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertiesFor

CSOM

var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);

    using (var clientContext = spContext.CreateUserClientContextForSPHost())
    {
        // Get the people manager instance and load current properties
        PeopleManager peopleManager = new PeopleManager(clientContext);
        PersonProperties personProperties = peopleManager.GetMyProperties();
        clientContext.Load(personProperties);
        clientContext.ExecuteQuery();

       // just to output what we have now to text box
       txtProperties.Text = "";
       foreach (var item in personProperties.UserProfileProperties)
       {
           txtProperties.Text += string.Format("{0} - {1}{2}", 
                           item.Key, item.Value, Environment.NewLine);
       }
   }

Hope this will solve your issue.

  • Hi Milind , This looks pretty great! But I dont know how to utilize your suggestions. I think it is for getting current user info?? – Flying Hope Oct 1 '15 at 6:54
  • Hi, can you tell me why you want to make list !! so I can suggest best solution for your requirement – Milind Oct 1 '15 at 12:55
  • Simple req.Just i want to show all user profiles info into list. No need to update user profiles from AD later. No continuous relation from AD to my application. – Flying Hope Oct 6 '15 at 7:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.