Articles about European Sharepoint Hosting Service
SharePoint 2013 Hosting – HostForLIFE :: Check If The User Is Active
Recently, I had a requirement to construct the OneDrive URL from the email address. As many of us know, OneDrive is a cloud storage service provided by Microsoft, which lets the user store up to 25 TB. Users are required to have a proper subscription (E1, E3, E5, etc) to utilize this service.
A couple of points to note, if you observe the URL of the One Drive for an organization, let’s consider Contoso. If the email of the user is vinay.deep@contoso.com, (usually the email address will be in the format of firstname.lastname@organizationname.com). Then the one drive URL would be:

https://contoso-my.sharepoint.com/personal/vinay_deep_contoso_com/_layouts/15/OneDrive.aspx
Let’s try understanding by breaking down the URL,
- https://contoso-my.sharepoint.com – This will be in format https://[YOURORGNAME]-my.sharepoint.com
- /personal – it is the managed path defined for all OneDrive sites
- /vinay_deep_contoso.com – observe the email address is vinay.deep@contoso.com. It replaces special characters with ‘_’.
- /_layouts/15/onedrive.aspx – it is constant to access one drive pages
Steps
To get the final OneDrive URL from the given email address, I have followed the below steps. At a glance,
- Getting the user input from the command prompt,
- Extracting the org name from the email.
- Building the URL string from email, basically replacing special characters with underscore ‘_’.
- Constructing the URL.
I have used Powershell with a regular expression to achieve the result set.
Step 1
Getting the user input from the command prompt. Just read the user input from the command prompt and storing in $Email variable,
1 |
$Email = Read-host "Enter the Email ID of user" |
Step 2
Extracing the org name from the given email address. Here I am storing the regex pattern in variable called ‘$pattern’ and then finally matching with given email to extract the string which is organization name here,
1 2 |
$pattern = '(?<=\@).+?(?=\.)' $OrgName = [regex]::Matches($Email, $pattern).Value |
Step 3
Building the One Drive string from the email. To replace the special character, I have written the function here called ‘Construct-OneDriveString’ from the given email.
1 2 3 4 5 6 7 8 9 10 |
function Construct - OneDriveString { param( [string] $InputString, [string] $Replacement = "_", [string] $SpecialChars = ".@") $rePattern = ($SpecialChars.ToCharArray() | ForEach - Object { [regex]::Escape($_) }) - join "|" $InputString - replace $rePattern, $Replacement } |
Step 4
Finally, constructing the OneDrive URL from the values generated from above steps. We have got the OneDrive string and organization name, which is enough to build the URL.
1 |
$OneDriveUrl = "https://$OrgName-my.sharepoint.com/personal/" + $OutputString + "/_layouts/15/onedrive.aspx" |
Complete Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
function Construct - OneDriveString { param( [string] $InputString, [string] $Replacement = "_", [string] $SpecialChars = ".@") $rePattern = ($SpecialChars.ToCharArray() | ForEach - Object { [regex]::Escape($_) }) - join "|" $InputString - replace $rePattern, $Replacement } $Email = Read - host "Enter the Email ID of user" $OutputString = Construct - OneDriveString $Email $pattern = '(?<=\@).+?(?=\.)' $OrgName = [regex]::Matches($Email, $pattern).Value $OneDriveUrl = "https://$OrgName-my.sharepoint.com/personal/" + $OutputString + "/_layouts/15/onedrive.aspx" #Checking the URL try { Write - Host "verifying the url $OneDriveUrl" - ForegroundColor Yellow $CheckConnection = Invoke - WebRequest - Uri $OneDriveUrl if ($CheckConnection.StatusCode - eq 200) { Write - Host "Connection Verified" - ForegroundColor Green } } catch [System.Net.WebException] { $ExceptionMessage = $Error[0].Exception if ($ExceptionMessage - match "403") { Write - Host "URL exists, but you are not authorized" - ForegroundColor Yellow } elseif($ExceptionMessage - match "503") { Write - Host "Server Busy" - ForegroundColor Red } elseif($ExceptionMessage - match "404") { Write - Host "URL doesn't exists" - ForegroundColor Red } else { Write - Host "There is an error" - ForegroundColor Red } } |
In this script, after completing the construction of the OneDrive URL, I am also checking the connection to One Drive by using Invoke-WebRequest function, which is built in a function used to invoke a web request to any https/http URLs and have the response back.
If the resultant OneDrive URL is valid, the user will be given a success message called ‘connection verified’.
If the resultant OneDrive URL is not valid, the user will be given a failure message called ‘URL doesn’t exist.
Conclusion
Thus, in this article, we have seen how to construct the OneDrive URL from the user’s email input. Hope you find this article useful.
Print article | This entry was posted by Peter on April 21, 2022 at 4:13 am, and is filed under European SharePoint 2013 Hosting. Follow any responses to this post through RSS 2.0. Both comments and pings are currently closed. |