// Get a context from the passed authentication settings public static ClientContext GetContext(string strURL, AuthenticationSettings authenticationsettings) { ClientContext clientcontext = new ClientContext(strURL); /* Using authentication settings supplied by settings object */ if (authenticationsettings.AuthenticationType == AuthenticationSettings.AuthenticationTypes.Current) { clientcontext.Credentials = CredentialCache.DefaultCredentials; } if (authenticationsettings.AuthenticationType == AuthenticationSettings.AuthenticationTypes.Specified) { clientcontext.Credentials = new NetworkCredential(authenticationsettings.username, authenticationsettings.password, authenticationsettings.domain); } if (authenticationsettings.AuthenticationType == AuthenticationSettings.AuthenticationTypes.Forms) { clientcontext.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication; clientcontext.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo(authenticationsettings.username, authenticationsettings.password); } if (authenticationsettings.AuthenticationType == AuthenticationSettings.AuthenticationTypes.Office365) { // To get the authentication for Office 365 the site url needs to end with a / string strHelperSite; if (strURL.EndsWith("/")) { strHelperSite = strURL; } else { strHelperSite = strURL + "/"; } helper = new MsOnlineClaimsHelper( authenticationsettings.username, authenticationsettings.password, strHelperSite); clientcontext.ExecutingWebRequest += new EventHandler <WebRequestEventArgs>(ctx_ExecutingWebRequest); } return(clientcontext); }
// Main function call to populate a Tree View from SharePoint public static TreeNode SiteAndListTreeLoader(string strURL, AuthenticationSettings authenticationsettings, BackgroundWorker worker) { worker.ReportProgress(0, MethodBase.GetCurrentMethod().Name.ToString() + ":" + strURL); TreeNode treenode = new TreeNode(strURL); try { // Get that the URL is valid if ((!strURL.ToLower().StartsWith("http://")) && (!strURL.ToLower().StartsWith("https://"))) { throw new Exception("The specified URL is not valid. It should start with http: or https://"); } ClientContext clientcontext = GetContext(strURL, authenticationsettings); Web web = clientcontext.Web; clientcontext.Load(web); clientcontext.ExecuteQuery(); PopulateTreeView(web, treenode, clientcontext, worker); } catch (Exception ex) { worker.ReportProgress(0, MethodBase.GetCurrentMethod().Name.ToString() + ":Exception:" + ex.Message); } return treenode; }
// Main function call to populate a Tree View from SharePoint public static TreeNode SiteAndListTreeLoader(string strURL, AuthenticationSettings authenticationsettings, BackgroundWorker worker) { worker.ReportProgress(0, MethodBase.GetCurrentMethod().Name.ToString() + ":" + strURL); TreeNode treenode = new TreeNode(strURL); try { // Get that the URL is valid if ((!strURL.ToLower().StartsWith("http://")) && (!strURL.ToLower().StartsWith("https://"))) { throw new Exception("The specified URL is not valid. It should start with http: or https://"); } ClientContext clientcontext = GetContext(strURL, authenticationsettings); Web web = clientcontext.Web; clientcontext.Load(web); clientcontext.ExecuteQuery(); PopulateTreeView(web, treenode, clientcontext, worker); } catch (Exception ex) { worker.ReportProgress(0, MethodBase.GetCurrentMethod().Name.ToString() + ":Exception:" + ex.Message); } return(treenode); }
private AuthenticationSettings AuthenticationSettingsFromUI() { // Create a settings object AuthenticationSettings authenticationsettings = new AuthenticationSettings(); // Get the authentication type if (radioButtonAuthenticationModeCurrent.Checked) { authenticationsettings.AuthenticationType = AuthenticationSettings.AuthenticationTypes.Current; } if (radioButtonAuthenticationModeForms.Checked) { authenticationsettings.AuthenticationType = AuthenticationSettings.AuthenticationTypes.Forms; authenticationsettings.username = textBoxAuthenticationUsername.Text.Trim(); authenticationsettings.password = textBoxAuthenticationPassword.Text; } if (radioButtonAuthenticationModeSpecified.Checked) { authenticationsettings.AuthenticationType = AuthenticationSettings.AuthenticationTypes.Specified; authenticationsettings.domain = textBoxAuthenticationDomain.Text.Trim(); authenticationsettings.username = textBoxAuthenticationUsername.Text.Trim(); authenticationsettings.password = textBoxAuthenticationPassword.Text; } if (radioButtonAuthenticationModeOffice365.Checked) { authenticationsettings.AuthenticationType = AuthenticationSettings.AuthenticationTypes.Office365; authenticationsettings.username = textBoxAuthenticationUsername.Text.Trim(); authenticationsettings.password = textBoxAuthenticationPassword.Text; } return authenticationsettings; }
// Get the fields from the SharePoint list for the UI public static Control[] RefreshSharePointFields(string strURL, AuthenticationSettings authenticationsettings, string strDocumentLibrary) { FieldCollection fieldcollection = null; // Get that the URL is valid if ((!strURL.ToLower().StartsWith("http://")) && (!strURL.ToLower().StartsWith("https://"))) { throw new Exception("The specified URL is not valid. It should start with http: or https://"); } ClientContext clientcontext = GetContext(strURL, authenticationsettings); Web web = clientcontext.Web; clientcontext.Load(web); ListCollection lists = web.Lists; clientcontext.Load(lists); clientcontext.ExecuteQuery(); foreach (List list in lists) { fieldcollection = list.Fields; clientcontext.Load(list); clientcontext.Load(fieldcollection); clientcontext.ExecuteQuery(); if (list.Title == strDocumentLibrary) { break; } } Control[] controls = new Control[fieldcollection.Count]; //foreach (Field field in fieldcollection) for (int i = 0; i < fieldcollection.Count ; i++) { Label label = new System.Windows.Forms.Label(); label.Text = fieldcollection[i].Title; controls[i] = label; } return controls; }
// Get a context from the passed authentication settings public static ClientContext GetContext(string strURL, AuthenticationSettings authenticationsettings) { ClientContext clientcontext = new ClientContext(strURL); /* Using authentication settings supplied by settings object */ if (authenticationsettings.AuthenticationType == AuthenticationSettings.AuthenticationTypes.Current) { clientcontext.Credentials = CredentialCache.DefaultCredentials; } if (authenticationsettings.AuthenticationType == AuthenticationSettings.AuthenticationTypes.Specified) { clientcontext.Credentials = new NetworkCredential(authenticationsettings.username, authenticationsettings.password, authenticationsettings.domain); } if (authenticationsettings.AuthenticationType == AuthenticationSettings.AuthenticationTypes.Forms) { clientcontext.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication; clientcontext.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo(authenticationsettings.username, authenticationsettings.password); } if (authenticationsettings.AuthenticationType == AuthenticationSettings.AuthenticationTypes.Office365) { // To get the authentication for Office 365 the site url needs to end with a / string strHelperSite; if (strURL.EndsWith("/")) { strHelperSite = strURL; } else { strHelperSite = strURL+"/"; } helper = new MsOnlineClaimsHelper( authenticationsettings.username, authenticationsettings.password, strHelperSite); clientcontext.ExecutingWebRequest += new EventHandler<WebRequestEventArgs>(ctx_ExecutingWebRequest); } return clientcontext; }