Home

Using MicrosoftGraph API in C# (or VB) to access mailbox usage information

%3CLINGO-SUB%20id%3D%22lingo-sub-298014%22%20slang%3D%22en-US%22%3EUsing%20MicrosoftGraph%20API%20in%20C%23%20(or%20VB)%20to%20access%20mailbox%20usage%20information%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-298014%22%20slang%3D%22en-US%22%3E%3CP%3EHi%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECurrently%20I%20use%20a%20powershell%20script%20such%20as%20below%20to%20get%20sizes%20of%20mailboxes%20in%20my%20organization%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24UserCredential%20%3D%20Get-Credential%3CBR%20%2F%3E%24Session%20%3D%20New-PSSession%20-ConfigurationName%20Microsoft.Exchange%20-ConnectionUri%20%3CA%20href%3D%22https%3A%2F%2Foutlook.office365.com%2Fpowershell-liveid%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Foutlook.office365.com%2Fpowershell-liveid%2F%3C%2FA%3E%20-Credential%20%24UserCredential%20-Authentication%20Basic%20-AllowRedirection%3CBR%20%2F%3EImport-PSSession%20%24Session%20-DisableNameChecking%3C%2FP%3E%3CP%3E%24ExportFile%20%3D%20(%5BEnvironment%5D%3A%3AGetFolderPath(%22Desktop%22)%2B%22%5CMailboxes.csv%22)%3C%2FP%3E%3CP%3EGet-Mailbox%20%7C%20Get-MailboxStatistics%20%7C%20Select%20DisplayName%2C%20TotalItemSize%2C%20ItemCount%20%7C%20Sort%20ItemCount%20-Descending%20%7C%20Export-CSV%20%24ExportFile%20-NoTypeInformation%3C%2FP%3E%3CP%3ERemove-PSSession%20%24Session%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%20I%20want%20to%20start%20programming%20using%20the%20Microsoft%20Graph%20API%20so%20I%20can%20write%20an%20app%20to%20pull%20out%20various%20bits%20of%20information%20-%20the%20first%20of%20which%20is%20the%20mailbox%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EUsing%20the%20example%20here%20%3D%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoftgraph%2Fconsole-csharp-snippets-sample%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Fmicrosoftgraph%2Fconsole-csharp-snippets-sample%3C%2FA%3E%20I%20can%20connect%20to%20my%20Office%20365%20%2F%20Exchange%20Online%20mailbox%20and%20pull%20out%20information%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%20I%20can't%20see%20where%20I%20can%20get%20mailbox%20statistics%20such%20as%20mailbox%20size%20like%20I%20can%20get%20using%20my%20powershell%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPerhaps%20someone%20can%20point%20me%20in%20the%20right%20direction%3F%20or%20comment%20if%20better%20way%20of%20doing%20it%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBasically%20want%20to%20regularly%20check%20sizes%20and%20send%20alert%20when%20particular%20mailboxes%20get%20too%20large%20(usually%20ones%20used%20by%20a%20service%20account%20rather%20than%20a%20user)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMany%20thanks%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-309096%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20MicrosoftGraph%20API%20in%20C%23%20(or%20VB)%20to%20access%20mailbox%20usage%20information%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-309096%22%20slang%3D%22en-US%22%3E%3CP%3ECool%20%3A)%3C%2Fimg%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-309094%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20MicrosoftGraph%20API%20in%20C%23%20(or%20VB)%20to%20access%20mailbox%20usage%20information%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-309094%22%20slang%3D%22en-US%22%3E%3CP%3EThat%20is%20what%20I%20have%20done%20in%20the%20end%20-%20used%20PowerShell%20from%20VB.NET%20using%26nbsp%3BSystem.Management.Automation%20from%20NuGet%20and%20it%20is%20working%20fine.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOriginally%20I%20thought%20Graph%20API%20was%20the%20way%20forward%20and%20a%20better%20way%20to%20do%20things%2C%20but%20after%20lack%20of%20responses%20on%20all%20forums%2C%20lack%20of%20documentation%20etc%20I%20decided%20to%20stick%20with%20what%20I%20know%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-309091%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20MicrosoftGraph%20API%20in%20C%23%20(or%20VB)%20to%20access%20mailbox%20usage%20information%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-309091%22%20slang%3D%22en-US%22%3E%3CP%3EI%20see%20that%20above%20you're%20using%20Exchange%20Powershell%2C%20then%20why%20cant%20you%20automate%20it%20with%20.Net%20(using%20C%23.Net).%20Please%20refer%3A%20%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fwushuai%2F2016%2F09%2F18%2Faccess-exchange-online-by-powershell-in-c%2F%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fblogs.msdn.microsoft.com%2Fwushuai%2F2016%2F09%2F18%2Faccess-exchange-online-by-powershell-in-c%2F%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHope%20this%20helps.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Darren Rose
Occasional Contributor

Hi

 

Currently I use a powershell script such as below to get sizes of mailboxes in my organization

 

$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking

$ExportFile = ([Environment]::GetFolderPath("Desktop")+"\Mailboxes.csv")

Get-Mailbox | Get-MailboxStatistics | Select DisplayName, TotalItemSize, ItemCount | Sort ItemCount -Descending | Export-CSV $ExportFile -NoTypeInformation

Remove-PSSession $Session

 

But I want to start programming using the Microsoft Graph API so I can write an app to pull out various bits of information - the first of which is the mailbox 

 

Using the example here = https://github.com/microsoftgraph/console-csharp-snippets-sample I can connect to my Office 365 / Exchange Online mailbox and pull out information

 

But I can't see where I can get mailbox statistics such as mailbox size like I can get using my powershell

 

Perhaps someone can point me in the right direction? or comment if better way of doing it?

 

Basically want to regularly check sizes and send alert when particular mailboxes get too large (usually ones used by a service account rather than a user)

 

Many thanks

3 Replies

I see that above you're using Exchange Powershell, then why cant you automate it with .Net (using C#.Net). Please refer: https://blogs.msdn.microsoft.com/wushuai/2016/09/18/access-exchange-online-by-powershell-in-c/

 

Hope this helps.

That is what I have done in the end - used PowerShell from VB.NET using System.Management.Automation from NuGet and it is working fine.

 

Originally I thought Graph API was the way forward and a better way to do things, but after lack of responses on all forums, lack of documentation etc I decided to stick with what I know

 

Thanks

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
35 Replies
Extentions Synchronization
Deleted in Discussions on
3 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies