O365 – CSOM Check if a user does not exist in your tenant anymore

I had a situation where I needed to make sure if a user does not exist anymore in the O365 system. I had to use CSOM for this solution and no matter how I tried I could not get any indication that a user has been deleted from the system.

I tried the IsActive field and Deleted field of an user object but no luck. No matter what I did even if the user was deleted and the expiration date of 30 days expired the user was still active.

My solution was that I checked to see if CSOM could retrieve a user profile data. If no data could be retrieved then this is an indication that the user profile has been deleted, hence the user has been deleted from the system. That is it. I used a field like account name since I know it must exist for all users.

There may be a “proper” solution for this but I just could not find one no matter how hard I tried(or maybe I just looked in the wrong place 🙂 ). Anyway see my previous post on how to get user profile information on how to do this workaround:


O365 CSOM Getting User profile information without possible errors


Here is a solution for a particular problem trying to access O365 user profiles without specifying credentials which proper privileges to the user profiles. Notice that it is not enough to add permissions to your app which uses CSOM. Believe me I tried all I could think of.

With the help of a colleague of mine I got a tip to try to explicitly specify credentials. After a few tinkering and wonderin this worked. So the error message which you might get would look

User ‘some user guid data’ doesn’t exist in UPA by UPN or SID, and user with this SID was not found in AD.

Below is a code sample which retrieved encrypted credentials and passes them on the the CSOM client context. After that the code tries to get some data from the user profile.

String manager = String.Empty;
 SecureString adminPWSecure = new SecureString();

 //get the base tenant admin urls
 string tenantAdminLoginName = ConfigurationManager.AppSettings["TenantAdminLoginName"];
 string tenantAdminPassword = ConfigurationManager.AppSettings["TenantAdminPassword"];
 string tenantAdminUrl = ConfigurationManager.AppSettings["SiteCollectionRequests_TenantAdminSite"];

 if (String.IsNullOrEmpty(tenantAdminLoginName) && String.IsNullOrEmpty(tenantAdminPassword))
 return null;

 tenantAdminLoginName = EngineCommon.Hide.Decrypt(tenantAdminLoginName);
 foreach (char c in EngineCommon.Hide.Decrypt(tenantAdminPassword).ToCharArray()) adminPWSecure.AppendChar(c);

 using (ClientContext clientContext = new ClientContext(tenantAdminUrl))
 clientContext.Credentials = new SharePointOnlineCredentials(tenantAdminLoginName, adminPWSecure);

 // Get the people manager instance for tenant context
 PeopleManager peopleManager = new PeopleManager(clientContext);

 var managerData = peopleManager.GetUserProfilePropertyFor(userName, "Manager");


 if (managerData != null && !String.IsNullOrEmpty(managerData.Value))
 PersonProperties personProperties = peopleManager.GetPropertiesFor(managerData.Value);

 manager = personProperties.Email;

 } catch(Exception ex)
 Console.Write("Failed to get a manager info for a user: " + ex.Message + ex.StackTrace);

Resources: SharePoint MySites, User Profiles, AD/Users and Alerts

Here are some links to resources that I have used in my years with SharePoint developing and fixing SP solutions. Hope this helps someone.


Customizing MOSS 2007 My Sites within the enterprise

Customising the “My Site” Templates – Point2Share – Site Home – MSDN Blogs

Talk Sharepoint Only: MySIte Customization

Customizing MySite in MOSS 2007 – Sridhar’s blog – Site Home – MSDN Blogs

Customizing My Sites in Microsoft SharePoint 2010 – Microsoft SharePoint Social Computing Team Blog – Site Home – MSDN Blogs

SharePoint @ Big Scholar » Blog Archive » Customise and Branding MySites

SharePoint MySite Branding

Salman’s Blogs » MySite Branding for SharePoint 2010

Set up My Sites (SharePoint Server 2010)

Configuring My Site in SharePoint 2010 | SharePoint George

User Profiles:

Syncing WSS and MOSS User profile properties with Active Directory « SharePoint Notes

Updating SharePoint 2010 User Information « SharePoint Automation

Account Deletion and SharePoint 2010 User Profile Synchronization

User Profile creation and the impact of AD synchronization/import | dev.notes.share()

Get user Profile in SharePoint 2010 | Enter your blog name here

SharePoint 2007: A user’s presence information does not appear correctly in People search results intermittently

People Search: Use a custom Ranking Model to search in added Profile Properties

SharePoint Steve » Making Custom User Profile Properties Searchable in SharePoint 2010

SharePoint User Profile Custom Properties Keyword Search Problem « kgraeme

SharePoint 2007: All you ever wanted to know about User Profiles – Sahil Malik | blah.winsmarts.com

How user profile synchronization works in SharePoint 2010 – Microsoft SharePoint Social Computing Team Blog – Site Home – MSDN Blogs

User Profile Information not updated on site collection’s people and group – Sharepoint made easier – Site Home – TechNet Blogs

Missing “I like it” and Tags on SharePoint 2010 – Jojo Koshy – Site Home – MSDN Blogs

Troubleshooting User Profile Sync Issues in Office SharePoint Server 2007 – My SharePoint World! – Nishant Shah – Site Home – TechNet Blogs


SharePoint Blog – René Hézser – Display all my alerts

How To Create Alerts Programmatically | Sharepointology

Sending alerts to groups in Sharepoint 2007 « Gavin’s Sharepoint Blog

Sharepoint 2007 task notification alert emails not working – Mel Lota’s Weblog

Sharepoint Tips And Tricks: Email Alerts not getting sent, while other emails are

SharePoint Alerts Not Working | SharePoint Geek

How to Troubleshoot Alerts in WSS 3.0 / MOSS – THE OFFICIAL BLOG OF THE SBS “DIVA”

Robin’s Sharepoint Blog: Alerts not working *all the time*

Zeb Sadiq | SharePoint 2010 custom alert template

Customizing Alert Notifications and Alert Templates in Windows SharePoint Services 3.0 – Microsoft SharePoint Developer Documentation Team Blog – Site Home – MSDN Blogs

Creating custom list alert templates in SharePoint 2010

Customizing Alert Templates in SharePoint Foundation 2010 – Developer – NothingButSharePoint.com

How To: Customizing alert emails using IAlertNotifyHandler – Microsoft SharePoint Developer Documentation Team Blog – Site Home – MSDN Blogs

Configure alert settings for a Web application (SharePoint Foundation 2010)

Create an Alert for a List or Library

People Picker:

Getting your ‘People Picker’ to pick AD groups « SharePoint Internals – Hristo Pavlov’s Blog

All you want to know about People Picker in SharePoint ( Functionality | Configuration | Troubleshooting ) Part-2 – Lesson’s Learnt – Site Home – MSDN Blogs

Bharat Reddy Basani: Mysterious bug in SharePoint People Editor Control

No exact match found when adding users from different domains in SharePoint « Fast .NET