How to break inheritance during live copy creation for each page programmatically? | Community
Skip to main content
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 lukasz-m

Hi @bkkothari2255, you can try to reuse below code, this is not a full solution but it shows how msm api can be used programmatically - the result will be canceled inheritance (suspend) for given page resource.

In general LiveRelationshipManager allows you to full manage relationships (including, cancel inheritance, detach etc) - here you can find link to java docs - [1]

import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import com.day.cq.wcm.msm.api.LiveRelationship;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.component.annotations.Reference;

// place for your class definition

@Reference
private LiveRelationshipManager liveRelationshipManager;

// place for other code

public void cancelPageInheritance(Resource pageResource, ResourceResolver resourceResolver) {
    if (liveRelationshipManager.hasLiveRelationship(pageResource)) {
        LiveRelationship liveRelationship = liveRelationshipManager.getLiveRelationship(pageResource, true);
        if (liveRelationship != null) {
            liveRelationshipManager.cancelRelationship(resourceResolver, liveRelationship, false, true);
        }
    }
}

[1] - https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/wcm/msm/api/LiveRelationshipManager.html

1 reply

lukasz-m
Community Advisor
lukasz-mCommunity AdvisorAccepted solution
Community Advisor
February 15, 2022

Hi @bkkothari2255, you can try to reuse below code, this is not a full solution but it shows how msm api can be used programmatically - the result will be canceled inheritance (suspend) for given page resource.

In general LiveRelationshipManager allows you to full manage relationships (including, cancel inheritance, detach etc) - here you can find link to java docs - [1]

import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import com.day.cq.wcm.msm.api.LiveRelationship;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.component.annotations.Reference;

// place for your class definition

@Reference
private LiveRelationshipManager liveRelationshipManager;

// place for other code

public void cancelPageInheritance(Resource pageResource, ResourceResolver resourceResolver) {
    if (liveRelationshipManager.hasLiveRelationship(pageResource)) {
        LiveRelationship liveRelationship = liveRelationshipManager.getLiveRelationship(pageResource, true);
        if (liveRelationship != null) {
            liveRelationshipManager.cancelRelationship(resourceResolver, liveRelationship, false, true);
        }
    }
}

[1] - https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/wcm/msm/api/LiveRelationshipManager.html