Class SettingUpdate
- Namespace
- Dynamicweb.Updates
- Assembly
- Dynamicweb.Core.dll
Represents an update that sets the value of a system configuration setting.
using System.Reflection;
namespace Dynamicweb.Updates.Examples
{
/// <summary>
/// Example UpdateProvider demonstrating usage of SqlUpdate, SettingUpdate, MethodUpdate, and FileUpdate.
/// </summary>
public class SqlUpdateSampleProvider : UpdateProvider
{
public override IEnumerable<Update> GetUpdates()
{
// SQL Update: Add a column to a table if it does not exist
yield return SqlUpdate.AddColumn(
id: "d50467a2-0e9b-4c70-a490-7ab4af88851b",
provider: this,
tableName: "MyCustomTable",
columnName: "MyCustomColumn",
columnDefinition: "NVARCHAR(50) NULL"
);
// SettingUpdate: Set a configuration value, only if it does not already exist
yield return new SettingUpdate(
id: "99fd48e3-f364-4108-bb19-6355e8b31896",
provider: this,
name: "/Globalsettings/System/Url/HighVolumeProductUrls/Enable",
value: "True"
);
// SettingUpdate: Overwrite a configuration value even if it exists
yield return new SettingUpdate(
id: "89c68710-c2c1-4e02-8ea5-f712adeea24f",
provider: this,
name: "/Globalsettings/System/Url/HighVolumeProductUrls/Enable",
value: "True",
overwrite: true
);
// MethodUpdate: Execute custom logic during update
yield return new MethodUpdate(
id: "30c561dd-5509-4636-b81a-146be8a51e40",
provider: this,
method: context =>
{
// Custom logic here
Console.WriteLine("Custom method executed as part of update.");
}
);
// FileUpdate: Write a file to disk, only if it does not already exist
yield return new FileUpdate(
id: "efe5952c-bf3c-400d-a654-6fe2eb79ca44",
provider: this,
filePath: "/Files/Templates/Feeds/CustomFeedTemplate.cshtml",
fileStreamProvider: () => GetFileStreamFromEmbeddedResource("CustomFeedTemplate.cshtml"),
overwrite: false
);
// FileUpdate: Overwrite an existing file
yield return new FileUpdate(
id: "27c24328-285b-4cae-b62b-23130b6fcde5",
provider: this,
filePath: "/Files/Templates/Feeds/CustomFeedTemplate.cshtml",
fileStreamProvider: () => GetFileStreamFromEmbeddedResource("CustomFeedTemplate.cshtml"),
overwrite: true
);
}
private Stream GetFileStreamFromEmbeddedResource(string resourceName)
{
string resourceFullName = $"CustomAssembly.{resourceName}";
var assembly = Assembly.GetAssembly(this.GetType());
var stream = assembly?.GetManifestResourceStream(resourceName);
if (stream is null)
throw new FileNotFoundException($"Embedded resource '{resourceFullName}' not found.");
return stream;
}
}
}
public class SettingUpdate : Update
- Inheritance
-
SettingUpdate
- Inherited Members
Constructors
SettingUpdate(string, UpdateProvider, string, string)
Initializes a new instance of the SettingUpdate class. Sets the specified setting to the given value if it does not already exist, or if overwrite is enabled.
public SettingUpdate(string id, UpdateProvider provider, string name, string value)
Parameters
idstringThe unique identifier for the update.
providerUpdateProviderThe update provider that owns this update.
namestringThe name of the setting to update.
valuestringThe value to assign to the setting.
SettingUpdate(string, UpdateProvider, string, string, bool)
Initializes a new instance of the SettingUpdate class. Sets the specified setting to the given value, optionally overwriting an existing value.
public SettingUpdate(string id, UpdateProvider provider, string name, string value, bool overwrite)
Parameters
idstringThe unique identifier for the update.
providerUpdateProviderThe update provider that owns this update.
namestringThe name of the setting to update.
valuestringThe value to assign to the setting.
overwriteboolIf
true, the setting value will be overwritten even if it already exists; iffalse, the value will only be set if the setting does not exist.
Methods
Execute(UpdateContext)
Executes the update, setting the specified configuration value.
If Dynamicweb.Updates.SettingUpdate.Overwrite is false and the setting already exists, the update is skipped.
public override void Execute(UpdateContext context)
Parameters
contextUpdateContextThe context for the update execution.