Publishing Page Drafts Systematically

Posted by Community Admin on 04-Aug-2018 15:01

Publishing Page Drafts Systematically

All Replies

Posted by Community Admin on 02-Nov-2011 00:00

Hi,
I have a question about systematically publishing page drafts. I have the following code (which runs only when the app is first started):

PageManager pageManager = PageManager.GetManager();
 
App.WorkWith().Pages().LocatedIn(Telerik.Sitefinity.Fluent.Pages.PageLocation.Frontend)
    .ThatAreDrafts()
    .ForEach(dr =>
    
        
List<PageData> drafts = pageManager
            .GetPageDataList()
            .Where(
                pd => pd.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live)
                .ToList();
 
        drafts.ForEach(dp =>
        
            pageManager.PublishPageDraft(dp.Id, true);
            pageManager.SaveChanges();
        );
//ends ForEeach body on Page Drafts
);


I'm trying to publish all frontend pages that are saved as drafts, since I have several methods which I call to systematically set other page data (require ssl on pages, set head tag content, page title i.e.), so I wanted to ensure that all pages were published before making those changes.

However, I get the following error:

You are trying to access item that no longer exists. The most probable reason is that it has been deleted by another user.
 
Exception Details: Telerik.Sitefinity.SitefinityExceptions.ItemNotFoundException: You are trying to access item that no longer exists. The most probable reason is that it has been deleted by another user.
 
Line 63:                         drafts.ForEach(dp =>
Line 64:                        
Line 65:                             pageManager.PublishPageDraft(dp.Id, true);
Line 66:                             pageManager.SaveChanges();
Line 67:                         );
 
[ItemNotFoundException: You are trying to access item that no longer exists. The most probable reason is that it has been deleted by another user.]
   DynamicModule.ns.Wrapped_OpenAccessPageProvider_1d5e01b9d17e4c38be1673cc0f9c55f3.GetDraft(Guid id) +293
   Telerik.Sitefinity.Modules.Pages.PageManager.GetDraft(Guid id) +87
   Telerik.Sitefinity.Modules.Pages.PageManager.PublishPageDraft(Guid draftId, Boolean makeVisible) +58

Posted by Community Admin on 02-Nov-2011 00:00

Just thought I would note, I did try this first:

App.WorkWith().Pages().LocatedIn(Telerik.Sitefinity.Fluent.Pages.PageLocation.Frontend)
.ThatAreDrafts().Publish();


Also, I am impersonating a user with:

User user = UserManager.GetManager().GetUser("admin");
SitefinityPrincipal principal = new SitefinityPrincipal(new UserIdentity(user));
Thread.CurrentPrincipal = principal;
HttpContext.Current.User = principal;

Posted by Community Admin on 07-Nov-2011 00:00

Hi Todd,

Please try using the below sample, it will take all draft pages and publish them accordingly:

var pageManager = PageManager.GetManager();
            var pages = pageManager.GetPageNodes().Where(p => p.Page != null && p.Page.Status != Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live).ToList();
            foreach (var pD in pages)
            
                 
                    var draft = pageManager.EditPage(pD.Page.Id);
                    var master = pageManager.PagesLifecycle.CheckOut(draft);
                    if (master != null)
                    
                        master = pageManager.PagesLifecycle.CheckIn(master);
                        pageManager.PagesLifecycle.Publish(master);
                        pageManager.SaveChanges();
                        WorkflowManager.MessageWorkflow(pD.Id, typeof(PageNode), null, "Publish", false, new Dictionary<string, string>());
                    
            

If there's anything else we can help you, please let us know.

All the best,
Boyan Barnev
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed