Hi Kevin,
You don't need to CreateWriteableClone with using Delete(). Just pass the contentlink of the page in.
After you delete the page using your code, what appears when you click the content page inside 'Manage Content' ? What appears in the right hand pane?
Hi Surjit, thanks for the reply.
The CreateWritableClone didn't make sense to me, but I saw a post somewhere that suggested it was needed. Either way produced the same result.
In Manage Content, clicking on these pages produces mixed results. Sometimes it simply selects it as expected, ready to use the action buttons. Other times, it it throws a 404-Not Found. I have not found a pattern as to why/when this occurs. A browser refresh seems temporarily fix the 404.
Before the job is run, I can manually delete any of these pages. After it is run, in Manage Content I can also manually delete a page (when no 404).
Thanks in advance for any ideas!
Hi Kevin,
Did you inspect the logs? On local they are in the App_Data folder. There should be an indication if the page delete was not executed successfully.
Did you run your code from within a scheduled job? Try deleting a page programmatically from within a controller instead and see if it produces the same result.
An event is triggered when content is being deleted, so you can also hook into this event to get some more details.
[InitializableModule]
[ModuleDependency(typeof(InitializationModule))]
public class ContentDeleteInitialization : IInitializableModule
{
public void Initialize(InitializationEngine context)
{
var events = context.Locate.ContentEvents();
events.DeletedContent += EventsOnDeletedContent;
}
private void EventsOnDeletedContent(object sender, DeleteContentEventArgs e)
{
//check 'e' for details about the deleted content
throw new NotImplementedException();
}
}
Hope this helps 😄
Hello Kevin, This could be a piece of your scheduled job -
var allPages = ContentLoader.GetChildren<WebPage>();
OnOutputResultChanged($"Found {allPages.Count()} pages");
OnStatusChanged($"Found {allPages.Count()} pages");
foreach (var page in allPages)
{
try
{
IContentRepositoryExtension.Delete(_contentRepository, page.ContentLink, true);
}
catch (Exception ex)
{
OnOutputResultChanged($"Delete page failed {page.DisplayName} msg: {ex.Message}");
}
}
Thanks Manoj. I was hopeful this different approach would work for me, especially after Praful's comment. Unfortunately, it produced the same results as the other things I've tried.
On a side note, I wasn't able to use the ContentLoader.GetChildren method in my app. This is the error I was presented with:
So I just used my original code to derive the page instead. That shouldn't make a difference though.
Try this code, I have tested following code. Please let me know if that work
var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
var contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
var contentModelUsage = ServiceLocator.Current.GetInstance<IContentModelUsage>();
var contentType = contentTypeRepository.Load<ProductPage>();
var usages = contentModelUsage.ListContentOfContentType(contentType);
foreach (var contentUses in usages)
{
try
{
contentRepository.Delete(contentUses.ContentLink, true);
}
catch (Exception ex)
{
return $"Delete page failed {contentUses.Name} msg: {ex.Message}";
}
}
Thank you everyone for your help. Unfortunately none of the solutions presented got me past my issue. I suspect the pages I am trying to delete were improperly constructed (programmatically by another job a consultant implemented, but not here for that), and are in conflict with others, preventing deletion.
Again, lots of good information! Thanks, but I guess I'll have to close the issue, and call it unresolved.
We have a job that creates pages programmatically, resulting in thousands of pages of a specific type. There is an issue, and we need to clear these pages, but the CMS UI/Admin isn't able to handle it. Instead, I have been trying to delete these pages programmatically. It removes them visually in the CMS content editor, but they still show in Admin Manage Content.
What am I missing? I've tried many variations, but here is my latest code:
var contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
var contentType = contentTypeRepository.Load<MyTestPage>();
var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
var contentList = ServiceLocator.Current.GetInstance<ContentStore>().ListContentOfContentType(contentType, false).ToList();
foreach (var course in contentList)
{
var page = contentRepository.Get<MyTestPage>(course.ContentLink);
var pageClone = page.CreateWritableClone() as MyTestPage;
contentRepository.Delete(clonePage.ContentLink, true, AccessLevel.NoAccess);
}
Any help would be greatly appreciated!