Getting User Email And Login Id From Sharepoint People Columns

The Core Issue – Retrieving User Info

Why Accessing User Data Matters

Being able to access user email addresses and login IDs programmatically from SharePoint people columns is critical for many business applications. The user data enables personalized communications, mapping to user profiles, syncing with directories, custom display formatting, and more.

Here are some common use cases that require access to SharePoint user email and login ID data:

  • Sending notification emails when items are assigned to users
  • Looking up user profile data based on the login ID
  • Syncing user data with Active Directory or Azure Active Directory
  • Showing avatar images for users using the email address
  • Integrating with other systems by passing user identifiers

Common Scenarios Requiring Access

Some typical scenarios that need access to SharePoint people column data include:

  • Workflow emails – Workflows often need to send personalized emails when users are assigned tasks or items. Accessing the email address from the people column enables sending notifications.
  • Directory synchronization – Keeping SharePoint user data in sync with AD or AAD requires mapping based on login IDs and email addresses.
  • User profile integration – Applications can pull additional user attributes from profiles to enhance solutions when given a login ID.
  • Mobile notifications – Mobile apps can send push notifications to users when accessed via people columns.
  • User interface customizations – Elements like avatars and welcome banners require access to user data.

These scenarios underline why access to user information is so important for business solutions built on SharePoint.

Methods to Get User Email and Login ID

Using Client Side Rendering

Client side rendering (CSR) enables customizing how people columns render in the user interface using JavaScript. The values can be accessed through the REST API from CSR code.

Key steps:

  1. Define a script editor or content editor web part to embed JavaScript
  2. Add code to retrieve the current user and login ID
  3. Access other properties like email addresses
  4. Render the data in the page or pass back to the parent page

Example Code for CSR

“`js
// Get reference to people picker column
var peoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict.peoplePickerDiv_TopSpan;

// Access login name
var loginName = peoplePicker.GetAllUserInfo()[0].Key;

// Lookup user profile data using REST
var endpoint = “_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='” + encodeURIComponent(loginName) + “‘”;

$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + endpoint,
headers: { Accept: “application/json;odata=verbose” },
success: function(data) {

// Access user profile properties from response
var email = data.d.Email;

// Render the information
$(‘.userInfo’).html(loginName + ‘ – ‘ + email);

}
});
“`

Server Side Options

SharePoint people columns can also be accessed server-side using C# or PowerShell. This allows for secured profile data lookup and robust integration options.

Approaches include:

  • C# code in the context of farm solutions or add-ins
  • PowerShell scripts for administration or automation
  • Workflows with stages to lookup and process user information

The SharePoint APIs provide methods for user profiling and directory management for server-side solutions.

Example Code for Server Side

“`cs
// C# example

using Microsoft.SharePoint; // SharePoint Context
using Microsoft.Office.Server.UserProfiles; // User Profiles

// Get SPWeb and people field value
SPWeb web = SPContext.Current.Web;
string userKey = web.Fields[“AssignedTo”].LookupValue;

// Get profile manager
UserProfileManager profileManager = new UserProfileManager(SPServiceContext.GetContext(web.Site));

// Load profile based on login name
UserProfile profile = profileManager.GetUserProfile(userKey);

// Access properties
string email = profile[“WorkEmail”].Value;
string loginName = profile[“UserName”].Value;

// Output properties
Label1.Text = loginName + ” – ” + email;
“`

Considerations and Best Practices

Permission Requirements

Accessing user profiles requires appropriate permissions for the solution:

  • User Profile Service Application permissions
  • Site collection user profile administrator role

Delegate access through groups or code logic rather than granting high-level admin rights.

Caching Lookup Data

Performance best practices include caching accessed user data instead of repeated lookups. Options:

  • In-memory caching in code logic
  • Caching proxies like Redis
  • SharePoint caching features

Avoid hitting user profiles repeatedly if accessing the same data across page requests.

Handling Errors Gracefully

Account for potential error cases:

  • User not found – Return defaults
  • Latency spikes – Retry logic
  • Partial profiles – Check for nulls

Use try/catch blocks and retry logic to fail gracefully if user data is unavailable.

Additional Tips for Working with User Data

Mapping Data to Profile Properties

Relevant user attributes can be scattered across profiles. Some pointers:

  • Prefer WorkEmail for notifications
  • Map SIP Address to enterprise voice
  • Department indicates organization

Familiarize yourself with the profile schema to harness available data.

Syncing with AD or Azure AD

Sync approach considerations:

  • Sync connection – Configure profile sync settings
  • Match users – Translate identifiers across systems
  • Handle errors – Reconciliation when data mismatches

Use out-of-box directory management functionality where possible.

Customizing Display Formatting

Options for better UI integration:

  • Avatar images via email Graves
  • Presence indicators from SIP address
  • Profile data web parts

Take advantage of available properties for dynamic UIs.

Leave a Reply

Your email address will not be published. Required fields are marked *