Expand my Community achievements bar.

Global Content replace using groovy console

Avatar

Level 5

Hi Team,

 we are in AEMasCS, we are trying the update a Text content across our global pages, for that we are using sling pipes,i downloaded the package of groovy script from below git hub Github link and installed in my local. here is my groovy script,when i tried to execute the script im getting user permisson issue though groovy-console-system-user have all access please find below screenshot. Kindly help on this.

rajat168_0-1723470522199.png

rajat168_2-1723470568351.png

 

groovyscript

=====

import org.apache.sling.api.resource.ResourceResolver
import org.apache.sling.api.resource.ResourceResolverFactory
import org.apache.sling.pipes.Plumber
import org.apache.sling.pipes.PipeBindings

// Get the ResourceResolverFactory service
def resourceResolverFactory = getService(ResourceResolverFactory)

def plumber = getService("org.apache.sling.pipes.Plumber");



// Define the path to the content you want to update
def contentPath = "/content/wknd"

// Define the text to find and replace
def findText = "helloworld"
def replaceText = "updated helloworld"


Map<String, Object> param = new HashMap<>()
param.put(ResourceResolverFactory.SUBSERVICE, "sling-pipes-user")
ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(param)
try {
    // Create a new pipe
  println  resourceResolver.getUserID()
    def pipe = plumber.newPipe(resourceResolver)
        .echo(contentPath) // Start at the content path
        .xpath("/jcr:root${contentPath}//element(*, nt:base)[jcr:contains(., '${findText}')]") // Find nodes containing the text
        .write("jcr:title", replaceText) // Replace the text in the jcr:title property
        .run()

    // Execute the pipe
    plumber.execute(resourceResolver, pipe, null)
    println "Text replacement completed successfully."
} catch (Exception e) {
    e.printStackTrace()
    println "An error occurred: ${e.message}"
} finally {
    resourceResolver.close()
}
Topics

Topics help categorize Community content and increase your ability to discover relevant content.

13 Replies

Avatar

Community Advisor

hi @rajat168 ,

Check if that user is having the required permission. Also cross check if there is any typo in user name.

You may have a look osgi config section of https://satyamblogs.medium.com/groovy-script-in-adobe-experience-manager-aem-d8071223627d

MukeshYadav__0-1723471917734.png

 

Thanks

Avatar

Level 5

Hi @MukeshYadav_ ,

 pfb i have added the user to admin group, but still have same issue,

rajat168_0-1723476638485.pngrajat168_1-1723476669728.png

 

Avatar

Community Advisor

Hi @rajat168 ,

Can you try administrators instead of admin

There we need to add group name not user.

Thanks

Avatar

Community Advisor

@rajat168 Script execution access is allowed only to specific user groups. Be default it is the administrators group. make sure the current logged in user is part of the administrators group and you should be able to execute the script. If you want to have some other user group that should be allowed access to groovy script execution, then you can update the groovy console OSGI config property - Script execution allowed groups.

Hope this helps.

Avatar

Level 5

Hi @Harwinder-singh , i have added groovy-console-system-user into administrator group as well. but still have same issue.

rajat168_0-1723476411359.png

rajat168_1-1723476469103.png

 

 

Avatar

Community Advisor

@rajat168 Can you add the user that you are using to log in to your local instance to the administrators group? The script is being executed by the logged in user here. If you are logging in with admin/admin , then just add the Administrator user to the administrators group. Due to some weird reason the admin user is not added to the administrators group by default.

 

Also update the Script execution allowed groups property to administrators as well.

Avatar

Community Advisor

Hi @rajat168 

resourceResolver is already have binding to current user session, you use that session if current user has all the permission.

 



Arun Patidar

Avatar

Level 5

Hi @arunpatidar , im able to use currect user session but still facing same issue.PFB,

rajat168_0-1723521614411.png

 

 

import org.apache.sling.api.resource.ResourceResolver
import org.apache.sling.api.resource.ResourceResolverFactory
import org.apache.sling.pipes.Plumber
import org.apache.sling.pipes.PipeBindings

def resourceResolver = getService("org.apache.sling.api.resource.ResourceResolverFactory").getServiceResourceResolver(null)

def plumber = getService("org.apache.sling.pipes.Plumber")

def contentPath = "/content/wknd"

// Define the text to find and replace
def findText = "helloworld"
def replaceText = "updated helloworld"

try {
    // Create a new pipe
   println "current User ::${resourceResolver.getUserID()}"
   
    def pipe = plumber.newPipe(resourceResolver)
        .echo(contentPath) // Start at the content path
        .xpath("/jcr:root${contentPath}//element(*, nt:unstructured)[jcr:contains(., '${findText}')]") // Find nodes containing the text
        .write("jcr:title", replaceText) // Replace the text in the jcr:title property
        .run()

    // Execute the pipe
    plumber.execute(resourceResolver, pipe, null)
    println "Text replacement completed successfully."
} catch (Exception e) {
    e.printStackTrace()
    println "An error occurred: ${e.message}"
} finally {
    resourceResolver.close()
}

 

Avatar

Level 5

Hi All,

 i got through this post AEMasCS  , above groovy console package will not support in AEMasCS  kindly pls confirm.

Avatar

Community Advisor

It is available, if does not work then, please check alternate at https://github.com/orbinson/aem-groovy-console 



Arun Patidar

Avatar

Community Advisor

Hi @rajat168 
Please use already available resourceResolver (currentUser).

Don't create explicit resourceResolver for groovy service user(This user might not have permission on content).

 

explicit resourceResolver for groovy service user

Screenshot 2024-08-13 at 10.02.08.png

 

resourceResolver (currentUser)

Screenshot 2024-08-13 at 09.56.57.png

 

available bindings

Screenshot 2024-08-13 at 09.57.07.png



Arun Patidar

Avatar

Level 5

Hi @arunpatidar , kindly please gothrough my above screenshot, i have used exisiting user account. also im currently using the same groovy console version which is not compatable with aemascs.

Avatar

Administrator

@rajat168 

Did you find the suggestions from users helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!



Kautuk Sahni