How to Identify the In-Active Users from SharePoint Online using C# CSOM Programmatically

Sathish Nadarajan
 
Solution Architect
February 13, 2019
 
Rate this article
 
Views
4369

A common problem during the data management within SharePoint is, some of the users might be leaving the company and the documents created by them will remain on their name. Though, the admin will delete the user from the Admin portal, the Users will be available on the Site Collection Level. Recently, I came up with a requirement to update these users with a given service account.

As part of that, Started writing a Piece of C# Script, which will identify, whether the particular user is Active or not. There are two ways, which I was thinking. Either to query and identify any unique property on the “User Information List”. I was able to query that hidden list and get the User details. But, I could not find any difference between an active user and an In-Active User.

Hence, I tried with querying on the User Profiles. The deleted User Profile is not available on the User Profile of the SharePoint admin portal itself. Hence, I was about to mimic the same thing using the C#. The Code is very straight forward.

 namespace CS.Help.Console
 {
     using Microsoft.SharePoint.Client.UserProfiles;
     using OfficeDevPnP.Core;
 
     class Program
     {
         static void Main(string[] args)
         {
             string webURL = "https://****.sharepoint.com/sites/DemoTeamSite/";
             string userName = "sathish@*****.com";
             string password = "*****";
 
 //Initiating the PNP Auth Manager
             AuthenticationManager authenticationManager = new AuthenticationManager();
 
 // Get the ClientContext
             var clientContext = authenticationManager.GetSharePointOnlineAuthenticatedContextTenant(webURL, userName, password);
 
             clientContext.Load(clientContext.Web);
             clientContext.ExecuteQuery();
 
 // create the PeopleManager Object
             PeopleManager peopleManager = new PeopleManager(clientContext);
 // Query for that User
             var profileProperty = peopleManager.GetPropertiesFor("i:0#.f|membership|sathish@*****.com");
             clientContext.Load(profileProperty);
             clientContext.ExecuteQuery();
 
 //The Profileproperty has a property called “ServerObjectIsNull”.  
 //For the InActive Users, it is TRUE and for the Active Users it is FALSE.
 
             bool activeStatus = profileProperty.ServerObjectIsNull == false ? true : false;
 
             System.Console.ReadLine();
         }
     }
 }
 

The code seems to be simple and straight forward. Hope that helps.

Happy Coding,

Sathish Nadarajan.

Category : .Net, Office 365, SharePoint

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment