How to restore a version programmaticaly? | Community
Skip to main content
mikezooz
New Participant
October 16, 2015
Solved

How to restore a version programmaticaly?

  • October 16, 2015
  • 3 replies
  • 1906 views

Hi,

I want to restore the version programmatically, so went through the PageManager API and found restore method.

PageManager pageManager = resourceResolver.adaptTo(PageManager.class);

Collection<Revision> revisions = pageManager.getRevisions(currentPage.getPath(), null);
                        for(Revision r :revisions){
                            currentId=r.getId();
                            break;
                        }

                try{

                         pageManager.restore(currentPage.getPath(), currentId);

                    }catch(Exception e)

 

The above code snippet restoring the version and crash my page too. Please help me to proceed

It shows NullPointerException exception, The path and Id is not null :(

Error Log:

28.04.2015 18:54:50.591 *ERROR* [127.0.0.1 [1430227490410] GET /libs/wcm/core/content/pageinfo.json HTTP/1.1] org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Uncaught Throwable java.lang.NullPointerException
    at com.day.cq.wcm.core.impl.AnnotationsInfoProvider.updatePageInfo(AnnotationsInfoProvider.java:78)
    at com.day.cq.wcm.core.impl.servlets.PageInfoServlet.doGet(PageInfoServlet.java:187)
    at org.apache.sling.api.servlets.SlingSafeMethodsServlet.mayService(SlingSafeMethodsServlet.java:268)
    at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:344)
    at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:375)
    at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:508)
  
28.04.2015 18:54:50.669 *ERROR* [127.0.0.1 [1430227490662] GET /content/km/15259/15288.html HTTP/1.1] org.apache.jsp.libs.foundation.components.primary.cq.Page.Page_jsp resource has no content. path=/content/test/15259/15288.html referrer=null

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Runal_Trivedi

michaelvino86 wrote...

 

                        for(Revision r :revisions){
                            currentId=r.getId();
                            break;
                        }

                try{

                         pageManager.restore(currentPage.getPath(), currentId);

                    }catch(Exception e)

 

When you are using revision to restore, make sure it has content resource (pagecontent) attached with that revision id. Simply make following check before break and see if it solves the problem:

Check to make -

for(Revision r :revisions){ //selecting version only if it has content attached if(r.hasContent()){ log.info('selected version id - '+r.getId()); currentId=r.getId(); break; } }

More on revision API - https://docs.adobe.com/docs/en/cq/5-6-1/javadoc/com/day/cq/wcm/api/Revision.html#hasContent%28%29

If above doesn't solve the issue, then can there be a case that page's location has been moved after the version was created, never evaluated what will be the impact of restoring version then.

- Runal

3 replies

joerghoh
Employee
October 16, 2015

Hi,

the only chance I see is that your page does not have a "jcr:content" node. Can you check?

kind regards,
Jörg

Runal_Trivedi
Runal_TrivediAccepted solution
New Participant
October 16, 2015

michaelvino86 wrote...

 

                        for(Revision r :revisions){
                            currentId=r.getId();
                            break;
                        }

                try{

                         pageManager.restore(currentPage.getPath(), currentId);

                    }catch(Exception e)

 

When you are using revision to restore, make sure it has content resource (pagecontent) attached with that revision id. Simply make following check before break and see if it solves the problem:

Check to make -

for(Revision r :revisions){ //selecting version only if it has content attached if(r.hasContent()){ log.info('selected version id - '+r.getId()); currentId=r.getId(); break; } }

More on revision API - https://docs.adobe.com/docs/en/cq/5-6-1/javadoc/com/day/cq/wcm/api/Revision.html#hasContent%28%29

If above doesn't solve the issue, then can there be a case that page's location has been moved after the version was created, never evaluated what will be the impact of restoring version then.

- Runal

mikezooz
mikezoozAuthor
New Participant
October 16, 2015

Thanks for your reply.

I have noticed that its restore the version but the problem is its required a refresh for restore the contents.

How to reload the page when the workflow current step completed?