/// <summary> /// Creates a PowerShell script file (.ps1) in the users temporary folder to support PowerShell based on CSOM. /// </summary> /// <param name="site"></param> /// <returns></returns> public static string CreatePowerShellScript(SiteAuthentication site) { string tempPSFilePath = Path.Combine(Path.GetTempPath(), string.Format("SPCB_PowerShell_{0}", POWERSHELL_OPENSITE_FILENAME)); if (File.Exists(tempPSFilePath)) { File.Delete(tempPSFilePath); } var assembly = System.Reflection.Assembly.GetExecutingAssembly(); var resourceName = assembly.GetManifestResourceNames().SingleOrDefault(s => s.Contains(POWERSHELL_OPENSITE_FILENAME)); using (Stream stream = assembly.GetManifestResourceStream(resourceName)) using (StreamReader reader = new StreamReader(stream)) { string powerShellScript = reader.ReadToEnd(); powerShellScript = ReplacePlaceHolder(powerShellScript, PLACEHOLDER_SCRIPT_LOCATION, tempPSFilePath); powerShellScript = ReplacePlaceHolders(site, powerShellScript); File.AppendAllText(tempPSFilePath, powerShellScript); } LogUtil.LogMessage("Created PowerShell script on location: {0}", tempPSFilePath); return(tempPSFilePath); }
public AddSiteForm(SiteAuthentication site) { InitializeComponent(); _site = site; InitializeForm(); }
private void LoadSite() { try { // Validate input Regex regex = new Regex(@"((\w+:\/\/)[-a-zA-Z0-9:@;?&=\/%\+\.\*!'\(\),\$_\{\}\^~\[\]`#|]+)"); if (!regex.IsMatch(tbSiteUrl.Text.Trim()) || tbSiteUrl.Text.Contains("_layouts")) { MessageBox.Show("Please enter a valid site collection URL, like https://company.sharepoint.com or https://company.sharepoint.com/sites/teamsite.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } // Add new site collection AuthenticationMode authentication = (AuthenticationMode)cbAuthentication.SelectedIndex; SiteAuthentication site = Globals.Sites.Add(new Uri(tbSiteUrl.Text), tbUsername.Text.Trim(), tbPassword.Text.Trim(), authentication); // Warning on mismatch SPCB and server build version Server server = null; if (!ProductUtil.DoesServerBuildVersionMatchThisRelease(site.BuildVersion, out server)) { if (MessageBox.Show($"The site you added is incompatible with this release of SharePoint Client Browser.\n\nThe site seems to be hosted on {server.ProductFullname} ({server.BuildVersion}) which is compatible with '{server.CompatibleRelease}', you can download this from {Constants.CODEPLEX_PROJECT_URL}.\n\nDo you want to download the '{server.CompatibleRelease}' release now?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) { System.Diagnostics.Process.Start(Constants.CODEPLEX_PROJECT_URL); } } // Close dialog this.DialogResult = DialogResult.OK; this.Close(); } catch (System.IO.FileNotFoundException ex) { // Handle the file not found exception "The specified module could not be found. (Exception from HRESULT: 0x8007007E)", // caused by not having installed the SharePoint Client Components string message = string.Format(Resources.ErrorMissingAssemblySDK, ex.Message, HelpUtil.GetSDKDownloadTitle(), Application.ProductName); ToggleButtons(true); if (MessageBox.Show(message, this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes) { System.Diagnostics.Process.Start(HelpUtil.GetSDKDownloadUrl()); } LogUtil.LogException(ex); } catch (Exception ex) { ToggleButtons(true); MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); LogUtil.LogException(ex); } }
public AddWebForm(SiteAuthentication site) { try { InitializeComponent(); _site = site; tbSiteUrl.Text = site.UrlAsString; } catch (Exception ex) { MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); LogUtil.LogException(ex); } }
/// <summary> /// Replace place holders within script. /// </summary> /// <param name="site">Site authentication data.</param> /// <param name="script">PowerShell script with place holders.</param> /// <returns>Returns PowerShell script without place holders.</returns> private static string ReplacePlaceHolders(SiteAuthentication site, string script) { script = ReplacePlaceHolder(script, PLACEHOLDER_CURRENT_DIRECTORY, Environment.CurrentDirectory); script = ReplacePlaceHolder(script, PLACEHOLDER_SITE_URL, site.Url.ToString()); script = ReplacePlaceHolder(script, PLACEHOLDER_LOGIN_NAME, site.UserName); script = ReplacePlaceHolder(script, PLACEHOLDER_CLIENT_SDK, HelpUtil.GetSDKDownloadTitle()); script = ReplacePlaceHolder(script, PLACEHOLDER_CLIENT_SDK_URL, HelpUtil.GetSDKDownloadUrl()); script = ReplacePlaceHolder(script, PLACEHOLDER_CLIENT_SDK_VERSION, HelpUtil.GetSDKMajorVersion()); switch (site.Authentication) { case AuthenticationMode.Default: if (string.IsNullOrEmpty(site.UserName)) { script = ReplacePlaceHolder(script, PLACEHOLDER_AUTHENTICATION, AUTHENTICATION_DEFAULT_CURRENT_USER); } else { script = ReplacePlaceHolder(script, PLACEHOLDER_AUTHENTICATION, AUTHENTICATION_DEFAULT_CUSTOM); } break; case AuthenticationMode.SharePointOnline: script = ReplacePlaceHolder(script, PLACEHOLDER_AUTHENTICATION, AUTHENTICATION_SHAREPOINT_ONLINE); break; case AuthenticationMode.Anonymous: script = ReplacePlaceHolder(script, PLACEHOLDER_AUTHENTICATION, AUTHENTICATION_ANONYMOUS); break; case AuthenticationMode.Forms: script = ReplacePlaceHolder(script, PLACEHOLDER_AUTHENTICATION, AUTHENTICATION_FORMS_BASED); break; default: break; } return(script); }