Sunday, November 15, 2015

Get and Set User Profile Properties using Console Application in SharePoint Online

Get and Set User Profile Properties using Console Application in SharePoint Online
File -> New -> Project -> Installed -> Templates -> Visual C# -> Windows -> Console Application ->Enter Name (UserProfilePropertySync)
Go to UserProfilePropertySync -> References -> Add Referece

Add below three dlls. Make sure add 16 version dlls.
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Runtime;
using Microsoft.SharePoint.Client.UserProfiles;
Make sure here we are adding 16th version dlls.
Go to UserProfilePropertySync -> Open Program.cs
add below code
-------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security;
using System.Net;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.UserProfiles;
namespace UserProfilePropertySync
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string tenantAdminUrl = GetString("Enter your tenant admin url (https://tenantname-admin.sharepoint.com): ");
                string tenantAdminUser = GetString("Enter your tenant admin user (user@tenantname.onmicrosoft.com): ");
                PersonProperties personProperties = null;
                SecureString tenantAdminPassword = GetPassword();
                string userToUpdate = "i:0#.f|membership|" + tenantAdminUser;
                using (ClientContext clientContext = new ClientContext(tenantAdminUrl))
                {
                    clientContext.Credentials = new SharePointOnlineCredentials(tenantAdminUser, tenantAdminPassword);
                    PeopleManager peopleManager = new PeopleManager(clientContext);
                    string userProfileProperty = "AboutMe";
                    personProperties = peopleManager.GetPropertiesFor(userToUpdate);
                    clientContext.Load(personProperties, p => p.UserProfileProperties);
                    clientContext.ExecuteQuery();
                    foreach (var property in personProperties.UserProfileProperties)
                    {
                        if (property.Key == userProfileProperty)
                            Console.WriteLine("Current value of the {0} property for user {1}:{2}", userProfileProperty, userToUpdate, property.Value.ToString());
                    }
                    string newAboutMeValue = GetString(String.Format("Enter a new value to be set for property {0}:", userProfileProperty));
                    Console.WriteLine("Setting new value...");
                    peopleManager.SetSingleValueProfileProperty(userToUpdate, userProfileProperty, newAboutMeValue);
                    personProperties = peopleManager.GetPropertiesFor(userToUpdate);
                    clientContext.Load(personProperties, p => p.UserProfileProperties);
                    clientContext.ExecuteQuery();
                    foreach (var property in personProperties.UserProfileProperties)
                    {
                        if (property.Key == userProfileProperty)
                        {
                            Console.WriteLine("Current value of the {0} property for user {1}:{2}", userProfileProperty, userToUpdate, property.Value.ToString());
                        }
                    }
                }
                Console.WriteLine("Press any key to continue.");
                Console.Read();
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(string.Format("Exception!"), ex.ToString());
                Console.WriteLine("Press any key to continue.");
                Console.Read();
                throw;
            }
        }
        public static string GetString(string question)
        {
            string userInput = string.Empty;
            try
            {
                Console.Write(question);
                userInput = Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                userInput = string.Empty;
            }
            return userInput;
        }
        public static SecureString GetPassword()
        {
            SecureString sStrPwd = new SecureString();
            try
            {
                Console.Write("Enter the SharePoint Tenant Admin password: ");
                for (ConsoleKeyInfo keyInfo = Console.ReadKey(true); keyInfo.Key != ConsoleKey.Enter; keyInfo = Console.ReadKey(true))
                {
                    if (keyInfo.Key == ConsoleKey.Backspace)
                    {
                        if (sStrPwd.Length > 0)
                        {
                            sStrPwd.RemoveAt(sStrPwd.Length - 1);
                            Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
                            Console.Write(" ");
                            Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
                        }
                    }
                    else if (keyInfo.Key != ConsoleKey.Enter)
                    {
                        Console.Write("*");
                        sStrPwd.AppendChar(keyInfo.KeyChar);
                    }
                }
                Console.WriteLine("");
            }
            catch (Exception e)
            {
                sStrPwd = null;
                Console.WriteLine(e.Message);
            }
            return sStrPwd;
        } 
    }
}
-------------------------------------------------------------
Press F5 to run and enter required input.

No comments:

Post a Comment

Featured Post

Protect an API by using OAuth 2.0 with Azure Active Directory and API Management

Protect an API by using OAuth 2.0 with Azure Active Directory and API Management https://docs.microsoft.com/en-us/azure/api-management/api-...

Popular posts