Example #1
0
        public void AddAndRemove()
        {
            // Arrange
            SPFarmPropertyBag.ClearCache();
            var f     = new BSPConfiguredFarm();
            var fssPO = new MSPPersistedObject(f.SettingStore);

            fssPO.Update = () =>
            {
            };

            string key = "foo";

            //  create a farm with property 'key'
            f.SettingStore.Settings[key] = "fred";

            // Act
            var target = new SPFarmPropertyBag();
            var containsBeforeCondition = target.Contains(key);

            target.Remove(key);
            var result = target.Contains(key);

            // Assert
            Assert.IsFalse(result);
            Assert.IsTrue(containsBeforeCondition);
        }
Example #2
0
        /// <summary>
        /// Reads the configuration settings from the farm.  This is stored in the
        /// default property bag for farm, which uses a persisted object in the farm.  Avoid storing this
        /// in SPFarm.Properties, since updating SPFarm will result in the entire farm cache being flushed.
        /// </summary>
        /// <returns>the setting if it exists, otherwise throws a configuration exception</returns>
        private static string GetConfig()
        {
            var bag = new SPFarmPropertyBag(SPFarm.Local);

            if (bag.Contains(defaultCentralUrlKey))
            {
                return(bag[defaultCentralUrlKey] as string);
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        /// Implements the operation for determining if a key exists in a web application
        /// </summary>
        /// <param name="args">The arguments for the contains key operation.  This must be an instance of ContainsKeyDataArgs.</param>
        /// <returns>true if the key found, false if the key is not found, or an exception representing an error if an error occurred</returns>
        public override object Execute(SPProxyOperationArgs args)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }

            var containsKeyArgs = args as ContainsKeyDataArgs;

            if (containsKeyArgs == null)
            {
                string message = string.Format(CultureInfo.CurrentCulture, Resources.InvalidProxyArgumentType,
                                               typeof(ContainsKeyDataArgs).FullName, args.GetType().FullName);
                var ex = new ConfigurationException(message);
                return(ex);
            }
            else if (containsKeyArgs.Key == null)
            {
                return(new ArgumentNullException("ContainsKeyDataArgs.Key"));
            }

            try
            {
                ConfigLevel level = (ConfigLevel)containsKeyArgs.Level;

                if (containsKeyArgs.Key.StartsWith(ConfigManager.PnPKeyNamespace) == false)
                {
                    string message            = string.Format(CultureInfo.CurrentCulture, Resources.InvalidKeyName, containsKeyArgs.Key);
                    ConfigurationException ex = new ConfigurationException(message);
                    return(ex);
                }
                else
                {
                    bool contains = false;

                    if (level == ConfigLevel.CurrentSPWebApplication)
                    {
                        if (containsKeyArgs.SiteId == Guid.Empty)
                        {
                            return(new ConfigurationException(Resources.EmptySiteGuid));
                        }
                        using (SPSite site = new SPSite(containsKeyArgs.SiteId))
                        {
                            SPWebAppPropertyBag bag = new SPWebAppPropertyBag(site.WebApplication);
                            contains = bag.Contains(containsKeyArgs.Key);
                        }
                    }
                    else if (level == ConfigLevel.CurrentSPFarm)
                    {
                        var bag = new SPFarmPropertyBag(SPFarm.Local);
                        contains = bag.Contains(containsKeyArgs.Key);
                    }
                    else
                    {
                        string message            = string.Format(CultureInfo.CurrentCulture, Resources.InvalidConfigLevel, level.ToString());
                        ConfigurationException ex = new ConfigurationException(message);
                        return(ex);
                    }
                    return(contains);
                }
            }
            catch (Exception excpt)
            {
                return(excpt);
            }
        }