/// <summary> /// Enumerates email addresses from identities referenced by the specified SharePoint users or groups. /// For SharePoint users that fail to be resolved, no exception will be thrown. /// To eliminate duplication on subequent calls, first call <see cref="CreatePrincipalContextScope"/>. /// </summary> /// <param name="members">A list of SharePoint users or groups to be resolved.</param> /// <returns>A enumerable object containing resolved email addresses.</returns> public static IEnumerable <string> ResolveEmailAddresses(IEnumerable <SPPrincipal> members) { CommonHelper.ConfirmNotNull(members, "members"); using (HostingEnvironment.Impersonate()) { IDisposable implicitScope = null; try { PrincipalContextScope.Current.GetType(); } catch (MemberAccessException) { implicitScope = CreatePrincipalContextScope(); } try { PrincipalResolver resolver = new PrincipalResolver(true); foreach (SPPrincipal member in members) { foreach (PrincipalInfo info in resolver.Resolve(member, null)) { if (info.IsResolved && !CommonHelper.IsNullOrWhiteSpace(info.EmailAddress)) { yield return(info.EmailAddress); } } } } finally { if (implicitScope != null) { implicitScope.Dispose(); } } } }
/// <summary> /// Enumerates identities referenced by the specified SharePoint user or group, and optionally throw exception when error is encountered. /// If <paramref name="throwOnException"/> is set to *false*, a <see cref="PrincipalInfo"/> object that <see cref="IsResolved"/> is set to *true* is returned when error is encountered. /// To eliminate duplication on subequent calls, first call <see cref="CreatePrincipalContextScope"/>. /// </summary> /// <param name="member">A SharePoint user or group to be resolved.</param> /// <param name="throwOnException">Whether to throw exception when error is encountered.</param> /// <returns>A enumerable object containing resolved identities.</returns> public static IEnumerable <PrincipalInfo> Resolve(SPPrincipal member, bool throwOnException) { CommonHelper.ConfirmNotNull(member, "member"); using (HostingEnvironment.Impersonate()) { IDisposable implicitScope = null; try { PrincipalContextScope.Current.GetType(); } catch (MemberAccessException) { implicitScope = CreatePrincipalContextScope(); } PrincipalResolver resolver = new PrincipalResolver(throwOnException); return(resolver.Resolve(member, implicitScope)); } }