Global Content replace using groovy console | Community
Skip to main content
Level 4
August 12, 2024

Global Content replace using groovy console

  • August 12, 2024
  • 4 replies
  • 2205 views

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.

 

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() }
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

4 replies

MukeshYadav_
Community Advisor
Community Advisor
August 12, 2024

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

 

Thanks

rajat168Author
Level 4
August 12, 2024

Hi @mukeshyadav_ ,

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

 

MukeshYadav_
Community Advisor
Community Advisor
August 12, 2024

Hi @rajat168 ,

Can you try administrators instead of admin

There we need to add group name not user.

Thanks

Harwinder-singh
Community Advisor
Community Advisor
August 12, 2024

@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.

rajat168Author
Level 4
August 12, 2024

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

 

 

Harwinder-singh
Community Advisor
Community Advisor
August 12, 2024

@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.

arunpatidar
Community Advisor
Community Advisor
August 12, 2024

Hi @rajat168 

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

 

Arun Patidar
rajat168Author
Level 4
August 13, 2024

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

 

 

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() }

 

rajat168Author
Level 4
August 13, 2024

Hi All,

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

kautuk_sahni
Community Manager
Community Manager
August 21, 2024

@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