private LinkSharePointSiteResult InternalLinkSharePointSite(Uri sharePointUrl, out string sharePointTeamMailboxKey, bool siteChecked, bool forceToUnlink) { LinkSharePointSiteResult linkSharePointSiteResult = LinkSharePointSiteResult.Success; sharePointTeamMailboxKey = "N/A"; bool flag = sharePointUrl == null; Uri uri = sharePointUrl ?? this.teamMailbox.SharePointUrl; Exception ex = null; if (flag) { if (this.teamMailbox.SharePointUrl == null) { return(LinkSharePointSiteResult.CurrentlyNotLinked); } sharePointUrl = this.teamMailbox.SharePointUrl; } try { using (ClientContext clientContext = new ClientContext(sharePointUrl.AbsoluteUri)) { clientContext.RequestTimeout = 60000; bool flag2; TeamMailboxHelper.GetCredentialAndConfigureClientContext(this.actAsUser, this.actAsUserOrgId, clientContext, true, out flag2); Web web = clientContext.Web; clientContext.Load <Web>(web, new Expression <Func <Web, object> >[] { (Web x) => x.AllProperties, (Web x) => (object)x.Id }); Site site = clientContext.Site; clientContext.Load <Site>(site, new Expression <Func <Site, object> >[] { (Site x) => x.Url }); clientContext.ExecuteQuery(); if (!siteChecked && clientContext.ServerVersion.Major < 15) { return(LinkSharePointSiteResult.SPServerVersionNotCompatible); } string text = this.teamMailbox.PrimarySmtpAddress.ToString(); if (web.AllProperties.FieldValues.ContainsKey("ExchangeTeamMailboxEmailAddress")) { sharePointTeamMailboxKey = (string)web.AllProperties["ExchangeTeamMailboxEmailAddress"]; if (!string.Equals(text, sharePointTeamMailboxKey, StringComparison.OrdinalIgnoreCase)) { linkSharePointSiteResult = LinkSharePointSiteResult.LinkedByOthers; } else if (flag) { web.AllProperties["ExchangeTeamMailboxEmailAddress"] = null; web.Update(); clientContext.ExecuteQuery(); this.teamMailbox.SharePointUrl = null; this.teamMailbox.SetSharePointSiteInfo(null, Guid.Empty); this.teamMailbox.SharePointLinkedBy = null; } else { if (this.actAsUser.Id.Equals(this.teamMailbox.SharePointLinkedBy)) { linkSharePointSiteResult = LinkSharePointSiteResult.AlreadyLinkedBySelf; } else { this.teamMailbox.SharePointLinkedBy = this.actAsUser.Id; } this.teamMailbox.SharePointUrl = sharePointUrl; } } else if (flag) { this.teamMailbox.SharePointUrl = null; this.teamMailbox.SetSharePointSiteInfo(null, Guid.Empty); this.teamMailbox.SharePointLinkedBy = null; } else { Uri url = TeamMailbox.GetUrl(site.Url); if (url == null) { throw new RecipientTaskException(Strings.ErrorSharePointSiteHasNoValidWebCollectionUrl(site.Url)); } web.AllProperties["ExchangeTeamMailboxEmailAddress"] = text; web.AllProperties["ExchangeTeamMailboxSharePointUrl"] = sharePointUrl.AbsoluteUri; web.AllProperties["ExchangeTeamMailboxSiteID"] = web.Id.ToString(); web.AllProperties["ExchangeTeamMailboxSiteCollectionUrl"] = site.Url; web.Update(); clientContext.ExecuteQuery(); this.teamMailbox.SharePointUrl = sharePointUrl; this.teamMailbox.SetSharePointSiteInfo(url, web.Id); this.teamMailbox.SharePointLinkedBy = this.actAsUser.Id; } } } catch (ClientRequestException ex2) { ex = new RecipientTaskException(Strings.ErrorTeamMailboxContactSharePointSite(uri.AbsoluteUri, ex2.Message)); } catch (ServerException ex3) { ex = new RecipientTaskException(Strings.ErrorTeamMailboxContactSharePointSiteWithCorrelationId(uri.AbsoluteUri, ex3.Message, ex3.ServerErrorTraceCorrelationId)); } catch (TimeoutException ex4) { ex = new RecipientTaskException(Strings.ErrorTeamMailboxContactSharePointSite(uri.AbsoluteUri, ex4.Message)); } catch (IOException ex5) { ex = new RecipientTaskException(Strings.ErrorTeamMailboxContactSharePointSite(uri.AbsoluteUri, ex5.Message)); } catch (WebException e) { SharePointException ex6 = new SharePointException(uri.AbsoluteUri, e, false); ex = new RecipientTaskException(Strings.ErrorTeamMailboxContactSharePointSite(uri.AbsoluteUri, ex6.DiagnosticInfo)); } catch (FormatException ex7) { ex = new RecipientTaskException(Strings.ErrorTeamMailboxContactSharePointSite(uri.AbsoluteUri, ex7.Message)); } if (flag && forceToUnlink) { if (ex != null || linkSharePointSiteResult == LinkSharePointSiteResult.LinkedByOthers) { this.teamMailbox.SharePointUrl = null; this.teamMailbox.SetSharePointSiteInfo(null, Guid.Empty); this.teamMailbox.SharePointLinkedBy = null; linkSharePointSiteResult = LinkSharePointSiteResult.Success; } } else if (ex != null) { throw ex; } return(linkSharePointSiteResult); }
internal static MultiValuedProperty <CultureInfo> CheckSharePointSite(SmtpAddress teamMailboxEmailAddress, ref Uri sharePointUrl, ADRawEntry actAsUser, OrganizationId actAsUserOrgId, ADUser executingUser, out SharePointMemberShip executingUserMembership, out Uri webCollectionUrl, out Guid webId) { MultiValuedProperty <CultureInfo> multiValuedProperty = new MultiValuedProperty <CultureInfo>(); executingUserMembership = SharePointMemberShip.Others; try { using (ClientContext clientContext = new ClientContext(sharePointUrl.AbsoluteUri)) { clientContext.RequestTimeout = 60000; bool flag; TeamMailboxHelper.GetCredentialAndConfigureClientContext(actAsUser, actAsUserOrgId, clientContext, true, out flag); Web web = clientContext.Web; clientContext.Load <Web>(web, new Expression <Func <Web, object> >[] { (Web x) => x.AllProperties, (Web x) => x.Url, (Web x) => (object)x.Language, (Web x) => x.Features, (Web x) => (object)x.Id }); Feature byId = web.Features.GetById(new Guid("{502A2D54-6102-4757-AAA0-A90586106368}")); clientContext.Load <Feature>(byId, new Expression <Func <Feature, object> > [0]); Group associatedOwnerGroup = clientContext.Web.AssociatedOwnerGroup; Group associatedMemberGroup = clientContext.Web.AssociatedMemberGroup; clientContext.Load <UserCollection>(associatedOwnerGroup.Users, new Expression <Func <UserCollection, object> > [0]); clientContext.Load <UserCollection>(associatedMemberGroup.Users, new Expression <Func <UserCollection, object> > [0]); Site site = clientContext.Site; clientContext.Load <Site>(site, new Expression <Func <Site, object> >[] { (Site x) => x.Url }); bool flag2 = false; try { clientContext.ExecuteQuery(); if (byId.ServerObjectIsNull != null) { flag2 = !byId.ServerObjectIsNull.Value; } } catch (UnauthorizedAccessException) { } if (!flag2) { throw new RecipientTaskException(Strings.ErrorTeamMailboxFeatureNotInstalled(web.Url)); } if (clientContext.ServerVersion.Major < 15) { throw new RecipientTaskException(Strings.ErrorTeamMailboxSharePointServerVersionInCompatible); } if (web.AllProperties.FieldValues.ContainsKey("ExchangeTeamMailboxEmailAddress") && (teamMailboxEmailAddress == SmtpAddress.Empty || !string.Equals(teamMailboxEmailAddress.ToString(), (string)web.AllProperties["ExchangeTeamMailboxEmailAddress"], StringComparison.OrdinalIgnoreCase))) { throw new RecipientTaskException(Strings.ErrorTeamMailboxSharePointSiteAlreadyLinkedWithOtherTM(sharePointUrl.ToString(), (string)web.AllProperties["ExchangeTeamMailboxEmailAddress"])); } webCollectionUrl = TeamMailbox.GetUrl(site.Url); webId = web.Id; if (webCollectionUrl == null) { throw new RecipientTaskException(Strings.ErrorSharePointSiteHasNoValidWebCollectionUrl(site.Url)); } multiValuedProperty.Add(new CultureInfo((int)web.Language)); try { foreach (User spUser in associatedOwnerGroup.Users) { if (TeamMailboxHelper.ExchangeSharePointUserMatch(executingUser, spUser)) { executingUserMembership = SharePointMemberShip.Owner; break; } } if (executingUserMembership == SharePointMemberShip.Others) { foreach (User spUser2 in associatedMemberGroup.Users) { if (TeamMailboxHelper.ExchangeSharePointUserMatch(executingUser, spUser2)) { executingUserMembership = SharePointMemberShip.Member; break; } } } } catch (CollectionNotInitializedException) { executingUserMembership = SharePointMemberShip.Others; } string uriString = null; if (!MaintenanceSynchronizer.TryEscapeAndGetWellFormedUrl(web.Url, out uriString)) { throw new RecipientTaskException(Strings.ErrorSharePointSiteHasNoValidUrl(web.Url)); } Uri uri = new Uri(uriString); if (uri != sharePointUrl) { sharePointUrl = uri; } } } catch (ClientRequestException ex) { throw new RecipientTaskException(Strings.ErrorTeamMailboxContactSharePointSite(sharePointUrl.AbsoluteUri, ex.Message)); } catch (ServerException ex2) { throw new RecipientTaskException(Strings.ErrorTeamMailboxContactSharePointSiteWithCorrelationId(sharePointUrl.AbsoluteUri, ex2.Message, ex2.ServerErrorTraceCorrelationId)); } catch (TimeoutException ex3) { throw new RecipientTaskException(Strings.ErrorTeamMailboxContactSharePointSite(sharePointUrl.AbsoluteUri, ex3.Message)); } catch (IOException ex4) { throw new RecipientTaskException(Strings.ErrorTeamMailboxContactSharePointSite(sharePointUrl.AbsoluteUri, ex4.Message)); } catch (WebException e) { SharePointException ex5 = new SharePointException(sharePointUrl.AbsoluteUri, e, false); throw new RecipientTaskException(Strings.ErrorTeamMailboxContactSharePointSite(sharePointUrl.AbsoluteUri, ex5.DiagnosticInfo)); } catch (FormatException ex6) { throw new RecipientTaskException(Strings.ErrorTeamMailboxContactSharePointSite(sharePointUrl.AbsoluteUri, ex6.Message)); } return(multiValuedProperty); }