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.
“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.”
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.
- There are 3 possible ways on how to figure it out.
- 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
- 2. You can query the guest user and check if its type is ‘Member’ via MSGraph api call : https://graph.microsoft.com/v1.0/users?$filter=userType eq ‘Guest’
- 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;
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
AAD UserId Add-SPOTheme Apply Theme Content Type contentTypeHub CopyExcelRecordToList Create List Item Create list item using rest Current User Permission Custom Theme DenyAddAndCustomizePages ExcelPowerAutomate Expand External Users Flows isExternalGuestUser Library Views List Templates List Views Metadata Microsoft Flow O365 PageContext People People Metadata Permission PnP Power Automate PowerAutomate PowerShell React Rest API Rest Endpoint Save as Template Send an HTTP Request to SharePoint SharePoint SharePoint Color Theme SharePointList SharePoint Modern SharePoint Online SharePoint OOTB SharePoint PageContext SPFX SPO UserIsExternal