コード例 #1
0
        /// <summary>Get the anonymous store.</summary>
        /// <param name="site">The site to get the settings for.</param>
        /// <returns></returns>
        public static AnonymousSlkStore GetStore(SPSite site)
        {
            Guid siteId = site.ID;
            // set <httpContext> to the current HttpContext (null if none)
            HttpContext httpContext = HttpContext.Current;

            // if an AnonymousSlkStore corresponding to <spSiteGuid> is cached, retrieve it, otherwise
            // create one
            string            cacheItemName = null;
            AnonymousSlkStore anonymousSlkStore;

            if (httpContext != null)
            {
                cacheItemName     = String.Format(CultureInfo.InvariantCulture, "SlkStore_{0}", siteId);
                anonymousSlkStore = (AnonymousSlkStore)httpContext.Cache.Get(cacheItemName);
                if (anonymousSlkStore != null)
                {
                    return(anonymousSlkStore);
                }
            }

            // set <mapping> to the SlkSPSiteMapping corresponding to <siteId>; if no such
            // mapping, exists, a SafeToDisplayException is thrown
            SlkSPSiteMapping mapping = SlkSPSiteMapping.GetRequiredMapping(site);

            // load "SlkSettings.xsd" from a resource into <xmlSchema>
            XmlSchema xmlSchema;

            using (StringReader schemaStringReader = new StringReader(SlkCulture.GetDefaultResources().SlkSettingsSchema))
            {
                xmlSchema = XmlSchema.Read(schemaStringReader,
                                           delegate(object sender2, ValidationEventArgs e2)
                {
                    // ignore warnings (already displayed when SLK Settings file was uploaded)
                });
            }

            SlkSettings settings = null;

            try
            {
                settings = LoadSettings(mapping, site, xmlSchema);
            }
            catch (SqlException)
            {
                // Try again in case temporary error
                try
                {
                    settings = LoadSettings(mapping, site, xmlSchema);
                }
                catch (SqlException e)
                {
                    SlkCulture culture = new SlkCulture(CultureInfo.InvariantCulture);
                    SlkStore.LogError(culture.Resources.SlkSettingsSqlErrorLoad + " {0}", culture, e);
                    throw new SafeToDisplayException(culture.Resources.SlkSettingsSqlErrorLoad);
                }
            }

            // create and (if possible) cache the new AnonymousSlkStore object
            anonymousSlkStore = new AnonymousSlkStore(siteId, mapping, settings);
            DateTime cacheExpirationTime = DateTime.Now.AddSeconds(HttpContextCacheTime);

            if (httpContext != null)
            {
                httpContext.Cache.Add(cacheItemName, anonymousSlkStore, null, cacheExpirationTime,
                                      System.Web.Caching.Cache.NoSlidingExpiration,
                                      System.Web.Caching.CacheItemPriority.Normal, null);
            }

            return(anonymousSlkStore);
        }
コード例 #2
0
        static void UpdateSlkSettings(string connectionString, Guid spSiteGuid, string settingsFileContents, string defaultSettingsFileContents)
        {
            // make sure we can access LearningStore; while we're at it, find out if there's a row
            // corresponding to this SPSite in the SiteSettingsItem table
            LearningStore      learningStore = new LearningStore(connectionString, "", true);
            LearningStoreJob   job           = learningStore.CreateJob();
            LearningStoreQuery query         = learningStore.CreateQuery(Schema.SiteSettingsItem.ItemTypeName);

            query.AddColumn(Schema.SiteSettingsItem.SettingsXml);
            query.AddCondition(Schema.SiteSettingsItem.SiteGuid, LearningStoreConditionOperator.Equal, spSiteGuid);
            job.PerformQuery(query);
            DataRowCollection results = job.Execute <DataTable>().Rows;

            if (results.Count == 0)
            {
                // this SPSite isn't listed in the SiteSettingsItem table, so we need to add a row
                if (settingsFileContents == null)
                {
                    settingsFileContents = defaultSettingsFileContents;
                }
            }
            else
            {
                object currentSettingsFileContents = results[0][Schema.SiteSettingsItem.SettingsXml];
                if ((currentSettingsFileContents == null) ||
                    (currentSettingsFileContents is DBNull) ||
                    (((string)currentSettingsFileContents).Length == 0))
                {
                    // the SLK Settings for this SPSite are missing, so we need to add them
                    if (settingsFileContents == null)
                    {
                        settingsFileContents = defaultSettingsFileContents;
                    }
                }
            }

            // upload the SLK Settings file if needed
            if (settingsFileContents != null)
            {
                // load "SlkSettings.xsd" from a resource into <xmlSchema>
                XmlSchema xmlSchema;
                using (StringReader schemaStringReader = new StringReader(SlkCulture.GetDefaultResources().SlkSettingsSchema))
                {
                    xmlSchema = XmlSchema.Read(schemaStringReader,
                                               delegate(object sender2, ValidationEventArgs e2)
                    {
                        // ignore warnings (already displayed when SLK Settings file was uploaded)
                    });
                }

                // validate <settingsFileContents>
                using (StringReader stringReader = new StringReader(settingsFileContents))
                {
                    XmlReaderSettings xmlSettings = new XmlReaderSettings();
                    xmlSettings.Schemas.Add(xmlSchema);
                    xmlSettings.ValidationType = ValidationType.Schema;
                    using (XmlReader xmlReader = XmlReader.Create(stringReader, xmlSettings))
                    {
                        try
                        {
                            SlkSettings settings = new SlkSettings(xmlReader, DateTime.MinValue);
                        }
                        catch (SlkSettingsException ex)
                        {
                            throw new SafeToDisplayException(LoadCulture(spSiteGuid).Resources.SlkSettingsFileError, ex.Message);
                        }
                    }
                }

                // store <settingsFileContents> in the database
                job = learningStore.CreateJob();
                Dictionary <string, object> uniqueProperties = new Dictionary <string, object>();
                uniqueProperties.Add(Schema.SiteSettingsItem.SiteGuid, spSiteGuid);
                Dictionary <string, object> updateProperties = new Dictionary <string, object>();
                updateProperties.Add(Schema.SiteSettingsItem.SettingsXml, settingsFileContents);
                updateProperties.Add(Schema.SiteSettingsItem.SettingsXmlLastModified,
                                     DateTime.Now.ToUniversalTime());
                job.AddOrUpdateItem(Schema.SiteSettingsItem.ItemTypeName, uniqueProperties,
                                    updateProperties);
                job.Execute();
            }
        }