Get number of new emails from Microsoft Exchange server in SharePoint
-
05-10-2020 - |
Question
We need to create widget that displays number of new/unread emails of current logged SharePoint user. Preferably using JSOM/Rest API for Javascript
We've done the following:
- Link SharePoint to sync profiles from Active Directory (AD)
- Configure SharePoint/Exchange server Single Sign-On (SSO)
Solution
I somehow figured to have a workaround
- download EWS (Exchange Web Services Managed API)
- create and configure impersonate application account
http://www.techieshelp.com/exchange-2013-configuring-impersonation-applications/ - install .NET EWS client library (https://www.nuget.org/packages/Microsoft.Exchange.WebServices/)
here is my web part C# code behind
using System; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.DirectoryServices; using System.Net; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.Threading.Tasks; using Microsoft.Exchange.WebServices; using Microsoft.Exchange.WebServices.Data; using Microsoft.SharePoint; namespace Messagerie.WidgetVisualWebPart { public partial class WidgetVisualWebPartUserControl : UserControl { protected void Page_Load(object sender, EventArgs e) { ServicePointManager.ServerCertificateValidationCallback = delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { if (errors == System.Net.Security.SslPolicyErrors.None) { return true; } if ((errors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0) { if (chain != null && chain.ChainStatus != null) { foreach (System.Security.Cryptography.X509Certificates.X509ChainStatus status in chain.ChainStatus) { if ((certificate.Subject == certificate.Issuer) && (status.Status == System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.UntrustedRoot)) { continue; } else { if (status.Status != System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NoError) { return false; } } } } return true; } else { return false; } }; ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); service.EnableScpLookup = true; try { service.Credentials = new WebCredentials("impersonateAccount@example.com", "impersonateAccountPassword", System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName); service.Url = new Uri("https://mail.domain.com/EWS/Exchange.asmx"); service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SID, SPContext.Current.Web.CurrentUser.UserId.NameId); Folder inbox = Folder.Bind(service, WellKnownFolderName.Inbox); // ... } catch (Exception exception) { ErrorMessage = exception.Message; } } } }
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange