How to check if current user is external in SharePoint

Why do we need to check for external users in SharePoint?

There are a lot of instances that affect the possibilities that we should. We might not need a check now but eventually when your applications requirements changed we need our app to be as robust as possible. It might not all about the user if an external case but I can say it is a possibility. SharePoint is known for collaboration platform from Microsoft. Given that it is a collaboration platform, we can’t deny the chance of external users or users that is not part of your organization be invited in your site to do some random stuffs. It could be an approval, about a sales document, a template for some stuff or a file for anything.

The Problem

“We have our custom application in our HR SharePoint site that tracks all the personal records of an employee. It has records for absences, filed leaves, deductions, promotions, salary information and more. The site was purely for internal users until our IT director changed. He wants to have some collaboration with an external resources. For some reasons we needed to share the application for the external user for some information audits and checks. Now we needed our custom WebPart to hide some part of it if it is being accessed by an external user.”

The Solution

SharePoint Online has leverage SharePoint Framework for SharePoint site application customizations and WebParts. There are a lot of API’s and endpoints available now provided by SharePoint. The problem above asked on how do we hide some part of the custom WebPart if the current logged in user is an external one.

  1. There are 3 possible ways on how to figure it out.
  2. 1. Check if the current users login name has “#EXT#” in their principal login name which is fine. You can query the user via SharePoint API

you can get the user id by accessing it in the SharePoint site context

  1. 2. You can query the guest user and check if its type is ‘Member’ via MSGraph api call :$filter=userType eq ‘Guest’
  2. 3. You can just directly have it from the pageContext available directly in your WebParts context with out doing an extra query! You can access the current users object via this path;
This is how the exact object looks like
Solution Screenshots

From our scaffolded solution, in the WebPart.ts we have here a property for our HelloWorld component called ‘isExternalUser

In our main component’s TSX file we have our logical condition inside the render function to show different text if the current logged in user is external with an internal user. Below is the screenshot

Now a screenshot on how it looks like if the user is an internal user:

And a screenshot of the custom WebPart if the user is an external user:

Now that’s another tip! Hope it helps somehow. Let me know if you have questions or if just leave a comment if you have anything to add up on the list of possible current user check usage!

Happy SharePointing! #SharingIsCaring

Accounting.js Automation Collaboration Competitors Connect Content Type Design Expand Flows Hillbilly Tabs Issues Javascript Limitation Limitations Microsoft Teams ModernScriptEditor NodeJs Node Versioning Numeral.js O365 Office 365 OneDrive Out Of The Box PnP Power Automate PowerShell Pwermissions Rest Endpoint ScriptEditor Send an HTTP Request to SharePoint SharePoint SharePoint Architecture SharePoint Designs SharePoint Modern SharePoint Online SharePoint Tabs ShellScript SPFX SPO Sync Teams Transform JS TypeScript Versioning Workflows

1 thought on “How to check if current user is external in SharePoint”

  1. Pingback: Actually, It’s Already In The SharePoint PageContext! -

Leave a Comment

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