Exemple #1
0
        private void GetUserCreds(ProvisioningTemplateToolsConfiguration config, string projectFolderPath, string credsFilePath)
        {
            ProvisioningCredentials creds = null;

            //prompt for credentials then persist to .user xml file
            VSToolsConfigWindow cfgWindow = new VSToolsConfigWindow();

            cfgWindow.txtSiteUrl.Text  = config.Deployment.TargetSite;
            cfgWindow.txtUsername.Text = config.Deployment.Credentials.Username;
            cfgWindow.ShowDialog();

            bool saveNeeded = false;

            if (cfgWindow.DialogResult.HasValue && cfgWindow.DialogResult.Value)
            {
                config.Deployment.TargetSite = cfgWindow.txtSiteUrl.Text;
                creds = new ProvisioningCredentials()
                {
                    Username = cfgWindow.txtUsername.Text,
                };
                creds.SetSecurePassword(cfgWindow.txtPassword.Password);
                config.Deployment.Credentials = creds;

                saveNeeded = true;
            }

            if (saveNeeded)
            {
                //serialize the credentials to a file
                XmlHelpers.SerializeObject(config.Deployment.Credentials, credsFilePath);

                //save the config to a file
                SaveConfigForProject(projectFolderPath, config);

                //reset any cached sp contexts
                ProvisioningService.ResetSPContexts();
            }
        }
Exemple #2
0
        private ProvisioningTemplateToolsConfiguration GetProvisioningTemplateToolsConfiguration(string projectFolderPath, bool createIfNotExists = false)
        {
            ProvisioningTemplateToolsConfiguration config = null;
            ProvisioningCredentials creds = null;

            var configFileCredsPath = Path.Combine(projectFolderPath, Resources.FileNameProvisioningUserCreds);
            var configFilePath      = Path.Combine(projectFolderPath, Resources.FileNameProvisioningTemplate);
            var pnpTemplateFilePath = Path.Combine(projectFolderPath, Resources.DefaultFileNamePnPTemplate);

            try
            {
                //get the config from file
                config = Helpers.ProjectHelpers.GetConfigFile <ProvisioningTemplateToolsConfiguration>(configFilePath);
                creds  = Helpers.ProjectHelpers.GetConfigFile <ProvisioningCredentials>(configFileCredsPath, false);

                if (config == null && createIfNotExists)
                {
                    var resourcesFolder = Resources.DefaultResourcesRelativePath;
                    EnsureResourcesFolder(projectFolderPath, resourcesFolder);
                    config = ProvisioningHelper.GenerateDefaultProvisioningConfig(Resources.DefaultFileNamePnPTemplate, resourcesFolder);
                }

                config.EnsureInitialState();

                //set the deserialized creds
                if (creds != null)
                {
                    config.Deployment.Credentials = creds;
                }

                //set paths
                config.FilePath    = configFilePath;
                config.ProjectPath = projectFolderPath;
                config.Deployment.Credentials.FilePath = configFileCredsPath;

                //ensure a default template exists if createIfNotExists is true
                if (config.Templates.Count == 0 && createIfNotExists)
                {
                    var resourcesFolder = Resources.DefaultResourcesRelativePath;
                    EnsureResourcesFolder(projectFolderPath, resourcesFolder);
                    var tempConfig = ProvisioningHelper.GenerateDefaultProvisioningConfig(Resources.DefaultFileNamePnPTemplate, resourcesFolder);
                    config.Templates = tempConfig.Templates;
                    XmlHelpers.SerializeObject(config, configFilePath);

                    //ensure pnp template files
                    foreach (var t in config.Templates)
                    {
                        string templatePath = System.IO.Path.Combine(projectFolderPath, t.Path);
                        if (!System.IO.File.Exists(templatePath))
                        {
                            string resourcesPath = System.IO.Path.Combine(projectFolderPath, Resources.DefaultResourcesRelativePath);
                            ProvisioningHelper.GenerateDefaultPnPTemplate(resourcesPath, templatePath);
                            AddTemplateToProject(templatePath);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ShowOutputPane();
                LogService.Exception("Error in GetProvisioningTemplateToolsConfiguration", ex);
            }

            return(config);
        }