Created

Mar 26, 2013

Changing resources in AEM5.6

Posted by Feike Visser

In this blogpost I want to show you some code around a new way you can change resources via the ResourceResolver

In the new ResourceResolver there are methods available to create/update/delete your Resources.

This is now quite a straightforward way, I will demonstrate this via some basic code samples.

Changing a resource

Here a code sample how to add / change a property on an existing resource. Via the ModifiableValueMap you can modify the properties.

ResourceResolver resourceResolver = getResourceResolver();
Resource resource = getSlingRequest().getResource();
		
ModifiableValueMap mvp = resource.adaptTo(ModifiableValueMap.class);

// adding and changing a property
if ( mvp.containsKey("test")) {
	String propValue = mvp.get("test", String.class);
	mvp.put("test", propValue + "1");
} else {
	mvp.put("test", "test");
}

Create / Delete a resource

Via the methods create() / delete() you can create and delete a new resource.

ResourceResolver resourceResolver = getResourceResolver();
Resource resource = getSlingRequest().getResource();

// adding and removing a child node
if ( resource.getChild("person") == null ) {
	Map props = new HashMap();
	props.put("firstname", "feike");
	props.put("lastname", "visser");
			
	Resource newResource = resourceResolver.create(resource, "person", props);
} else {
	resourceResolver.delete(resource.getChild("person"));
}

Saving / Reverting changes

Via the methods .commit() and .revert() you can either save or rollback the changes.

if ( resourceResolver.hasChanges() ) { 
	resourceResolver.commit();
}

COMMENTS

  • By APK - 10:30 AM on May 31, 2014   Reply
    Many Thanks.
  • By tidung.net - 5:04 AM on Jun 02, 2014   Reply
    Thanks.
    • By hair color ideas - 5:10 PM on Jun 04, 2014   Reply
      Your good knowledge and kindness in playing with all the pieces was very useful. I don’t know what I would have done if I had not encountered such a step like this.
      • By apk - 11:20 AM on Jun 06, 2014   Reply
        Thanks.
        • By Find Out More - 3:40 PM on Jun 07, 2014   Reply
          He used the cash from his work from house company to pay for the provides he required to fix the various oral issues that came to his workplace.
          • By Jasa SEO - 11:34 AM on Jun 08, 2014   Reply
            Many Thanks.
            • By Tattoos - 4:40 PM on Jun 08, 2014   Reply
              Many Thanks.
              • By SEO - 7:38 AM on Jun 21, 2014   Reply
                Thanks.
                • By Obat Keputihan - 9:35 AM on Jun 22, 2014   Reply
                  Many Thanks.
                  • By airul - 10:53 AM on Jul 06, 2014   Reply
                    Thanks.
                    • By model baju lebaran - 4:55 PM on Jul 12, 2014   Reply
                      i really to say nice tools from adobe. i like it.
                      • By url opener - 6:23 PM on Jul 15, 2014   Reply
                        This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work...
                        • By Crystal X Asli - 2:58 AM on Jul 17, 2014   Reply
                          i really to say nice tools from adobe. i like it.
                          • By duplichecker - 8:00 PM on Jul 18, 2014   Reply
                            woah! I’m really enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that perfect balance between superb usability and visual appeal. I must say you’ve done a very good job with this.
                            • By Crystal X Asli - 8:31 PM on Jul 22, 2014   Reply
                              i really to say nice tools from adobe. i like it.
                              • By office refurbishment - 11:08 AM on Aug 15, 2014   Reply
                                woah! I’m really enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that perfect balance between superb usability and visual appeal. I must say you’ve done a very good job with this.

                                ADD A COMMENT