How can I programmatically delete / remove Umbraco Media ( Umbraco.Models.Core.IMedia)
Hi all,
I am trying to delete programmatically delete / remove Umbraco Media ( Umbraco.Models.Core.IMedia) but not sure how to do it. I have tried
tTMedia.DisposeIfDisposable();
as shown at // <<<<<<< in code below but that does not work. Other methods as shown in Visual Studio Intellisence do not seems appropriate ?
Any suggestions would be appreciated.
Thanks
Terry Clancy
ClanceZ
public void TTDelMedia(TT_UCAdmin.Request_TTDelMedia request_TTDelMedia)
{
// Log Progress
TT_UCAdminStatic.RequestStatusStatic.EventLogIn += "About to Delete TTCatalogMedia <br/>";
if (ApplicationContext.Current != null)
{
// Instantiate the MediaService so we can do Media Operations
var ms = ApplicationContext.Current.Services.MediaService;
var rootMediaList = ms.GetRootMedia();
int tTImagesFolderIndex = -1;
tTImagesFolderIndex = rootMediaList.FindIndex(x => x.Name == "TTImages");
if (tTImagesFolderIndex >= 0) // I think null list returns -1
{
int tTImagesFolderID = rootMediaList.ElementAt(tTImagesFolderIndex).Id;
var ChildMediaList = ms.GetChildren(tTImagesFolderID);
// int matchingImageIndex = -1;
// matchingImageIndex = ChildMediaList.FindIndex(x => x.Name == productImage_Thumb_FileName);
foreach (IMedia tTMedia in ChildMediaList)
{
TT_UCAdminStatic.RequestStatusStatic.EventLogIn += "About to Delete " + tTMedia.Name.ToString() + "<br/>";
tTMedia.DisposeIfDisposable(); // <<<<<<<
}
}
}
}
Never mind - I solved the problem - clearly I just needed a good nights sleep :-)
The solution is to use the "Delete" method of my instance of the ApplicationContext.Current.Services.MediaService object. That line of code now flagged by // <<<<<<<<<< in my updated code below:
// =================================================================================(80)
// Delete TTCatalogMedia
// =================================================================================(80)
public void TTDelMedia(TT_UCAdmin.Request_TTDelMedia request_TTDelMedia)
{
// Log Progress
TT_UCAdminStatic.RequestStatusStatic.EventLogIn += "About to Delete TTCatalogMedia <br/>";
if (ApplicationContext.Current != null)
{
// Instantiate the MediaService so we can do Media Operations
var ms = ApplicationContext.Current.Services.MediaService;
var rootMediaList = ms.GetRootMedia();
int tTImagesFolderIndex = -1;
tTImagesFolderIndex = rootMediaList.FindIndex(x => x.Name == "TTImages");
if (tTImagesFolderIndex >= 0) // I think null list returns -1
{
int tTImagesFolderID = rootMediaList.ElementAt(tTImagesFolderIndex).Id;
var ChildMediaList = ms.GetChildren(tTImagesFolderID);
foreach (IMedia tTMedia in ChildMediaList)
{
TT_UCAdminStatic.RequestStatusStatic.EventLogIn += "About to Delete " + tTMedia.Name.ToString() + "<br/>";
ms.Delete(tTMedia); // <<<<<<<<<<
}
}
}
}
How can I programmatically delete / remove Umbraco Media ( Umbraco.Models.Core.IMedia)
Hi all,
I am trying to delete programmatically delete / remove Umbraco Media ( Umbraco.Models.Core.IMedia) but not sure how to do it. I have tried
as shown at // <<<<<<< in code below but that does not work. Other methods as shown in Visual Studio Intellisence do not seems appropriate ?
Any suggestions would be appreciated.
Thanks Terry Clancy ClanceZ
Hi again,
Never mind - I solved the problem - clearly I just needed a good nights sleep :-)
The solution is to use the "Delete" method of my instance of the ApplicationContext.Current.Services.MediaService object. That line of code now flagged by // <<<<<<<<<< in my updated code below:
Thanks
Terry Clancy
ClanceZ
is working on a reply...