/// <summary> /// Connect to the ClientContext and returns an instance of it. /// </summary> /// <param name="host">host to connect</param> /// <param name="username">the username</param> /// <param name="password">the password</param> /// <param name="domain">domain of SharePoint</param> /// <param name="proxySettings">settings of proxy</param> /// <returns>instance of ClientContext if connection works, otherwise null will be returned</returns> internal ClientContext ConnectToClientContext(string host, string username, string password, string domain, ProxySettings proxySettings = null) { ClientContext clientContext = new ClientContext(host); this.SetProxy(clientContext, proxySettings); var cc = new CredentialCache(); cc.Add(new Uri(clientContext.Url), "NTLM", new NetworkCredential(username, password, domain)); clientContext.Credentials = cc; try { clientContext.ExecuteQuery(); return(clientContext); } catch (Exception) { return(null); } }
/// <summary> /// Tries to connect to the server and loads the migration tree. /// </summary> /// <returns>Task with bool as return value.</returns> private async Task <bool> ApplyConfigurationAndLoadSourceTreeAsync() { try { string txt = "Trying to connect to source"; this.waitForm.SpecialText = txt; this.log.AddMessage(txt); await this.ConnectToSource(); } catch (Exception ex) { this.waitForm.SpecialText = string.Empty; Debug.WriteLine(ex.ToString()); throw new LoginFailedException("Could not connect to source SharePoint. Please check your login Data"); } finally { this.waitForm.SpecialText = string.Empty; } try { // as there is no site collection at the destination it makes no sense to connect to it if (!this.settings.SiteCollectionMigration) { string txt2 = "Trying to connect to destination"; this.waitForm.SpecialText = txt2; this.log.AddMessage(txt2); await this.ConnectToDestination(); } else { Connector connector = new Connector(); ProxySettings proxySettings = null; if (this.checkBoxProxyActivate.Checked) { proxySettings = new ProxySettings(this.textBoxProxyUrl.Text.Trim(), this.textBoxProxyUsername.Text.Trim(), this.textBoxProxyPassword.Text.Trim()); } this.migrationData.TargetClientContext = connector.ConnectToClientContext(this.settings.ToHost, this.settings.ToUserName, this.settings.ToPassword, this.settings.ToDomain, proxySettings); this.migrationData.SourceClientContext = connector.ConnectToClientContext(this.settings.FromHost, this.settings.FromUserName, this.settings.FromPassword, this.settings.FromDomain, proxySettings); } } catch (Exception ex) { Debug.WriteLine(ex.ToString()); throw new LoginFailedException("Could not connect to destination SharePoint. Please check your login Data"); } finally { this.waitForm.SpecialText = string.Empty; } string txt3 = "Generating migration tree"; this.waitForm.SpecialText = txt3; this.log.AddMessage(txt3); Task <SSiteCollection> t = Task.Factory.StartNew(() => { return(this.contentLoader.LoadSourceSiteCollection()); }); this.sourceSiteCollection = await t; // make sure, the site collection is checked, if it will be migrated if (this.settings.SiteCollectionMigration) { this.sourceSiteCollection.Checked = true; this.sourceSiteCollection.Migrate = true; foreach (SSite s in this.sourceSiteCollection.Sites) { if (s.IsSiteCollectionSite) { s.Migrate = true; s.Checked = true; } } } return(true); }