Clearing Images

Clearing Images

Clearing Images

The Sitecore.Configuration.Factory class encapsulates the general information available in the web.config file and contains methods that provide access to this information and the means to override it

  1. Create an object for the database where the item is located - replace SitecoreDB with the name of the Sitecore database where you want to perform the update, typically “master”
  2. Create an object for the target item in the database- replace itempath with the relative path to the item in the Content Tree, such as “home/about/item1” or start the path with a backslash (/) to indicate an absolute path, such as “/sitecore/content/home/about/item1”– a null value will be set in either case if the item does not exist - Sitecore does compare case when evaluating item paths
  3. Create an object of type imagefield for the image field – replace fieldname with the name of the image field
  4. Open the edit state for the item
  5. Use the Clear method to remove the image
  6. Close the edit state for the item


C# Web Control Code

Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase("SitecoreDB");

Sitecore.Data.Items.Item item = db.GetItem("itempath");

Sitecore.Data.Fields.ImageField image = item.Fields["fieldname "];

item. ItemEditing.BeginEdit();

image.Clear();

item. ItemEditing.EndEdit();