void InitializeFormFields()
        {
            try
            {
                Guid siteGuid = string.IsNullOrEmpty(SPSiteSelector.CurrentId) ? Guid.Empty : new Guid(SPSiteSelector.CurrentId);
                AdministrationConfiguration configuration = SlkAdministration.LoadConfiguration(siteGuid);

                TxtDatabaseServer.Text       = configuration.DatabaseServer;
                TxtDatabaseName.Text         = configuration.DatabaseName;
                ChkCreateDatabase.Checked    = configuration.CreateDatabase;
                TxtInstructorPermission.Text = configuration.InstructorPermission;
                TxtLearnerPermission.Text    = configuration.LearnerPermission;
                TxtObserverPermission.Text   = configuration.ObserverPermission;
                ChkCreatePermissions.Checked = configuration.CreatePermissions;
            }
            catch (SafeToDisplayException ex)
            {
                // exception that's safe to show to browser user
                LabelErrorMessage.Text = Html(ex.Message);
            }
            catch (Exception ex)
            {
                // exception that may contain sensitive information -- since the user is an
                // administrator, we'll show them the error
                LabelErrorMessage.Text = Html(ex.ToString());
            }
        }
        void ProcessForm()
        {
            try
            {
                // if the user requested that a database be created, load the contents of the SLK
                // LearningStore schema file into <schemaFileContents>, otherwise set
                // <schemaFileContents> to null
                string schemaFileContents = null;
                if (ChkCreateDatabase.Checked)
                {
                    schemaFileContents = File.ReadAllText(Server.MapPath("SlkSchema.sql"));
                }

                // if an SLK Settings file was uploaded, set <settingsFileContents> to its contents,
                // otherwise set <settingsFileContents> to null
                string settingsFileContents = null;

                if (FileUploadSlkSettings.HasFile)
                {
                    using (StreamReader streamReader = new StreamReader(FileUploadSlkSettings.FileContent))
                    {
                        settingsFileContents = streamReader.ReadToEnd();
                    }
                }
                else
                {
                    if (FileUploadSlkSettings.FileName.Length > 0)
                    {
                        throw new SafeToDisplayException(culture.Resources.InvalidSlkSettingsFilePath);
                    }
                }

                // set <defaultSettingsFileContents> to the default SLK Settings file, which is stored
                // with a ".dat" extension to prevent it from being downloadable (for security reasons)
                string defaultSettingsFileContents = File.ReadAllText(Server.MapPath("SlkSettings.xml.dat"));

                // save the SLK configuration for this SPSite
                SlkAdministration.SaveConfiguration(new Guid(SPSiteSelector.CurrentId),
                                                    TxtDatabaseServer.Text, TxtDatabaseName.Text, schemaFileContents,
                                                    TxtInstructorPermission.Text, TxtLearnerPermission.Text, TxtObserverPermission.Text,
                                                    ChkCreatePermissions.Checked, settingsFileContents, defaultSettingsFileContents,
                                                    null);

                BtnOK.Visible = false;
                OperationCompletedPanel.Visible = true;
            }
            catch (SafeToDisplayException ex)
            {
                // exception that's safe to show to browser user
                LabelErrorMessage.Text = Html(ex.Message);
            }
            catch (Exception ex)
            {
                // exception that may contain sensitive information -- since the user is an
                // administrator, we'll show them the error
                EnableUi(false, true);
                LabelErrorMessage.Text = Html(ex.ToString());
            }
        }
    /// <summary>
    /// Configures a SharePoint site collection for use with SLK.
    /// </summary>
    ///
    /// <param name="siteCollectionUrl">The URL of the site collection to configure.</param>
    ///
    static void ConfigureSiteCollectionForSlk(string siteCollectionUrl, string databaseServer,
                                              string databaseName)
    {
        Console.WriteLine("Configuring site collection \"{0}\" for use with SLK",
                          siteCollectionUrl);

        // set <sharePointLocation> to the path to the installation location of SharePoint
        string sharePointLocation;

        using (RegistryKey registryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
                   @"Software\Microsoft\Shared Tools\Web Server Extensions\12.0"))
        {
            object value;
            if ((registryKey == null) || ((value = registryKey.GetValue("Location")) == null))
            {
                throw new Exception("SharePoint is not installed");
            }
            sharePointLocation = value.ToString();
        }

        // set <slkAdminLocation> to the path where SLK administration files are located
        string slkAdminLocation = Path.Combine(sharePointLocation,
                                               @"Template\Admin\SharePointLearningKit");

        if (!Directory.Exists(slkAdminLocation))
        {
            throw new Exception("SharePoint Learning Kit is not installed");
        }

        using (SPSite spSite = new SPSite(siteCollectionUrl))
        {
            AdministrationConfiguration configuration = SlkAdministration.LoadConfiguration(spSite.ID);

            if (databaseServer == "CurrentOrDefaultServer")
            {
                databaseServer = configuration.DatabaseServer;
            }
            else
            if (databaseServer == "localhost")
            {
                databaseServer = spSite.HostName;
            }
            if (databaseName == "CurrentOrDefaultDatabase")
            {
                databaseName = configuration.DatabaseName;
            }

            // load SlkSchema.sql, the SQL Server schema for SLK
            string schemaFileContents;
            if (configuration.CreateDatabase)
            {
                schemaFileContents = File.ReadAllText(Path.Combine(slkAdminLocation, "SlkSchema.sql"));
                Console.WriteLine("...database doesn't exist -- creating it");
            }
            else
            {
                schemaFileContents = null; // don't create a database -- it already exists
                Console.WriteLine("...database exists already");
            }

            // load SlkSettings.xml, the default SLK Settings
            string settingsFileContents = File.ReadAllText(
                Path.Combine(slkAdminLocation, "SlkSettings.xml.dat"));

            // save the SLK configuration for this SPSite
            SlkAdministration.SaveConfiguration(spSite.ID, configuration.DatabaseServer, configuration.DatabaseName,
                                                schemaFileContents, configuration.InstructorPermission, configuration.LearnerPermission, configuration.ObserverPermission, configuration.CreatePermissions,
                                                null, settingsFileContents, spSite.WebApplication.ApplicationPool.Username);
        }
    }
Esempio n. 4
0
        void DownloadSettingsPage_Load(object sender, EventArgs e)
        {
            try
            {
                try
                {
                    // The page URL is of the following two forms:
                    //   1. http://.../DownloadSettings.aspx/<guid>/SlkSettings.xml
                    //   2. http://.../DownloadSettings.aspx/Default/SlkSettings.xml
                    // The following code parses <guid> into <siteGuid> (for case 1), or sets
                    // <siteGuid> to null (for case 2).
                    Uri uri = Request.Url;
                    if ((uri.Segments.Length < 3) ||
                        !String.Equals(uri.Segments[uri.Segments.Length - 1], "SlkSettings.xml",
                                       StringComparison.OrdinalIgnoreCase))
                    {
                        throw new SafeToDisplayException(culture.Resources.DownloadSettingsIncorrectUrl);
                    }
                    string siteGuidOrDefault = uri.Segments[uri.Segments.Length - 2];
                    siteGuidOrDefault = siteGuidOrDefault.Substring(0, siteGuidOrDefault.Length - 1);
                    Guid?spSiteGuid;
                    if (String.Equals(siteGuidOrDefault, "Default",
                                      StringComparison.OrdinalIgnoreCase))
                    {
                        spSiteGuid = null;
                    }
                    else
                    {
                        try
                        {
                            spSiteGuid = new Guid(siteGuidOrDefault);
                        }
                        catch (FormatException)
                        {
                            throw new SafeToDisplayException(
                                      culture.Resources.DownloadSettingsIncorrectUrl);
                        }
                        catch (OverflowException)
                        {
                            throw new SafeToDisplayException(
                                      culture.Resources.DownloadSettingsIncorrectUrl);
                        }
                    }

                    // set <settingXml> to the SLK Settings XML for <spSiteGuid> -- use the default
                    // SLK Settings XML if <spSiteGuid> is null or <spSiteGuid> is not configured for
                    // use with SLK
                    string settingsXml = null;
                    if (spSiteGuid != null)
                    {
                        settingsXml = SlkAdministration.GetSettingsXml(spSiteGuid.Value);
                    }
                    if (settingsXml == null)
                    {
                        // load the default SLK Settings
                        settingsXml = File.ReadAllText(Server.MapPath("SlkSettings.xml.dat"));
                    }

                    // write the XML to the browser
                    Response.ContentType = "text/xml";
                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    // if the following line is un-commented, clicking "Open" in the IE File Download
                    // dialog gives an error: "Cannot find 'C:\Documents and Settings\<user>\Local
                    // Settings\Temporary Internet Files\Content.IE5\<path>\SlkSettings[1].xml'"
                    //Response.AddHeader("content-disposition", "attachment");
                    Response.Write(settingsXml);
                }
                catch (SafeToDisplayException ex)
                {
                    // an expected exception occurred
                    Response.Clear();
                    Response.ContentType = "text/html";
                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    Response.Write(culture.Format(culture.Resources.AdminErrorPageHtml, ex.Message));
                    Response.End();
                }
            }
            catch (System.Threading.ThreadAbortException)
            {
                // thrown by Response.End above
                throw;
            }
            catch (Exception ex)
            {
                // an unexpected exception occurred
                Response.Clear();
                Response.ContentType = "text/html";
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.Write(culture.Format(culture.Resources.AdminErrorPageHtml, Server.HtmlEncode(string.Format(CultureInfo.CurrentUICulture, culture.Resources.SeriousErrorDownloadSettings, ex))));
                Response.End();
            }
        }