Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Removing values from the cq:tags property using values entered in the text Field inside a Dialog

Avatar

Level 2

Hi,

I have a requirement to remove values from cq:tags property of the asset. The value to be removed will be entered through text field present in a dialog panel.

I am trying to achieve this using SlingPostServlet and specifying the suffix for the property as 'Patch' like below

./jcr:content/metadata/cq:tags@Patch found from the below thread

http://dev.day.com/docs/en/cq/5-5/javadoc/org/apache/sling/servlets/post/SlingPostConstants.html#SUF...

On click of Ok of the dialog, I am adding minus(-) sign at the start of the value entered in the textfield. But this is not working. Can you please let me know where I am making a mistake.

Thanks in advance

1 Accepted Solution

Avatar

Correct answer by
Level 6

That is exactly what the Sling Post Servlet can do.

Attached is a small package I've done for test and education purposes (change the ext to zip). It adds a node /content/geometrixx-media/en/propertytest and 8 subpages that all has the tag Music but only half of the pages has the "wrongly set" Movies (named hastag#). The acceptance criteria is to use a POST to remove the Movies tag. I used curl to post my content, but you can use whatever POST-tool you want.

The curl command to remove a tag is:

curl -u admin:admin --data "_charset_=utf-8&%3Astatus=browser&.%2Fjcr%3Atitle=RemovedTagMovies1&%3Aignore=&.%2Fcq%3Atags%40TypeHint=String%5B%5D&.%2Fcq%3Atags%40Patch=true&.%2Fcq%3Atags=-geometrixx-media%3Aentertainment%2Fmovies"  http://localhost:4502/content/geometrixx-media/en/propertytest/hastag1/jcr:content

If we break it down it consist of (decoded):

  • -u = the authorization
  • --data - our POST data, URL-encoded
  •     _charset_=utf8 - what is the encoding
  • :status=browser - one of the "hidden" Sling Post variables. Aways returns 200, and gives any error in the reply.
  • ./jcr:title=RemovedTagMovies1 - Added this just for fun so we get a faster feedback.
  • :ignore - Ignore some of the errors
  • ./cq:tags@TypeHint=String[] - Tells the POST servlet how to handle the request. VERY IMPORTANT! Without it, it treats it like a String and removes everything.
  • ./cq:tags@Patch=true - I will modify on the values of this value. This allows us to use + and -
  • ./cq:tags=--geometrixx-media:entertainment/movies - The tag to remove.

Now, if I try to do the same thing on a "notag#" node like this:

curl -u admin:admin --data "_charset_=utf-8&%3Astatus=browser&.%2Fjcr%3Atitle=StillNoTagMovies1&%3Aignore=&.%2Fcq%3Atags%40TypeHint=String%5B%5D&.%2Fcq%3Atags%40Patch=true&.%2Fcq%3Atags=-geometrixx-media%3Aentertainment%2Fmovies"  http://localhost:4502/content/geometrixx-media/en/propertytest/notag1/jcr:content

the only thing that happens is that we change the title.

The "whole" bunch of curl-commands then is:

curl -u admin:admin --data "_charset_=utf-8&%3Astatus=browser&.%2Fjcr%3Atitle=RemovedTagMovies1&%3Aignore=&.%2Fcq%3Atags%40TypeHint=String%5B%5D&.%2Fcq%3Atags%40Patch=true&.%2Fcq%3Atags=-geometrixx-media%3Aentertainment%2Fmovies"  http://localhost:4502/content/geometrixx-media/en/propertytest/hastag1/jcr:content curl -u admin:admin --data "_charset_=utf-8&%3Astatus=browser&.%2Fjcr%3Atitle=StillNoTagMovies1&%3Aignore=&.%2Fcq%3Atags%40TypeHint=String%5B%5D&.%2Fcq%3Atags%40Patch=true&.%2Fcq%3Atags=-geometrixx-media%3Aentertainment%2Fmovies"  http://localhost:4502/content/geometrixx-media/en/propertytest/notag1/jcr:content curl -u admin:admin --data "_charset_=utf-8&%3Astatus=browser&.%2Fjcr%3Atitle=RemovedTagMovies1&%3Aignore=&.%2Fcq%3Atags%40TypeHint=String%5B%5D&.%2Fcq%3Atags%40Patch=true&.%2Fcq%3Atags=-geometrixx-media%3Aentertainment%2Fmovies"  http://localhost:4502/content/geometrixx-media/en/propertytest/hastag11/jcr:content curl -u admin:admin --data "_charset_=utf-8&%3Astatus=browser&.%2Fjcr%3Atitle=StillNoTagMovies1&%3Aignore=&.%2Fcq%3Atags%40TypeHint=String%5B%5D&.%2Fcq%3Atags%40Patch=true&.%2Fcq%3Atags=-geometrixx-media%3Aentertainment%2Fmovies"  http://localhost:4502/content/geometrixx-media/en/propertytest/notag11/jcr:content curl -u admin:admin --data "_charset_=utf-8&%3Astatus=browser&.%2Fjcr%3Atitle=RemovedTagMovies1&%3Aignore=&.%2Fcq%3Atags%40TypeHint=String%5B%5D&.%2Fcq%3Atags%40Patch=true&.%2Fcq%3Atags=-geometrixx-media%3Aentertainment%2Fmovies"  http://localhost:4502/content/geometrixx-media/en/propertytest/hastag12/jcr:content curl -u admin:admin --data "_charset_=utf-8&%3Astatus=browser&.%2Fjcr%3Atitle=StillNoTagMovies1&%3Aignore=&.%2Fcq%3Atags%40TypeHint=String%5B%5D&.%2Fcq%3Atags%40Patch=true&.%2Fcq%3Atags=-geometrixx-media%3Aentertainment%2Fmovies"  http://localhost:4502/content/geometrixx-media/en/propertytest/notag12/jcr:content curl -u admin:admin --data "_charset_=utf-8&%3Astatus=browser&.%2Fjcr%3Atitle=RemovedTagMovies1&%3Aignore=&.%2Fcq%3Atags%40TypeHint=String%5B%5D&.%2Fcq%3Atags%40Patch=true&.%2Fcq%3Atags=-geometrixx-media%3Aentertainment%2Fmovies"  http://localhost:4502/content/geometrixx-media/en/propertytest/hastag13/jcr:content curl -u admin:admin --data "_charset_=utf-8&%3Astatus=browser&.%2Fjcr%3Atitle=StillNoTagMovies1&%3Aignore=&.%2Fcq%3Atags%40TypeHint=String%5B%5D&.%2Fcq%3Atags%40Patch=true&.%2Fcq%3Atags=-geometrixx-media%3Aentertainment%2Fmovies"  http://localhost:4502/content/geometrixx-media/en/propertytest/notag13/jcr:content

/Ove

View solution in original post

3 Replies

Avatar

Level 6

What you need to do is sending the following information in the request to the metadata to remove is, here removing the tag year/2011:

./cq:tags             -year/2011 ./cq:tags@Patch       true ./cq:tags@TypeHint    String[]

The magic is in the @TypeHint String[] that tells the SlingPost servlet that it should handle the value as a String array. Without it, it tries to default to just String, since that is what you provide for it.

/Ove

Avatar

Level 1

Our specific use case is bulk-editing of multiple resources, where you want to say " remove valueX to/from the set", regardless of whether the certain property has the value or not. That should be automatically done by the POST servlet.An example would be tags. In this case, having to rewrite the entire array in the POST is not optimal, sometimes you just want to say "add valueX" or "remove valueX".

Specifically for the Bulk Editor functionality, where the requirement is deletion of specific tags from certain DAM Assets, we have tried two approaches -

1. Using the @Patch Suffix , but is not working correctly.

2. Even a generic

Values[] v = prop.getValues(); for each (Value val in v) { v.remove(val); }

is not working. Is there any specific thing we need to handling while considering remove of selected Tags?

Avatar

Correct answer by
Level 6

That is exactly what the Sling Post Servlet can do.

Attached is a small package I've done for test and education purposes (change the ext to zip). It adds a node /content/geometrixx-media/en/propertytest and 8 subpages that all has the tag Music but only half of the pages has the "wrongly set" Movies (named hastag#). The acceptance criteria is to use a POST to remove the Movies tag. I used curl to post my content, but you can use whatever POST-tool you want.

The curl command to remove a tag is:

curl -u admin:admin --data "_charset_=utf-8&%3Astatus=browser&.%2Fjcr%3Atitle=RemovedTagMovies1&%3Aignore=&.%2Fcq%3Atags%40TypeHint=String%5B%5D&.%2Fcq%3Atags%40Patch=true&.%2Fcq%3Atags=-geometrixx-media%3Aentertainment%2Fmovies"  http://localhost:4502/content/geometrixx-media/en/propertytest/hastag1/jcr:content

If we break it down it consist of (decoded):

  • -u = the authorization
  • --data - our POST data, URL-encoded
  •     _charset_=utf8 - what is the encoding
  • :status=browser - one of the "hidden" Sling Post variables. Aways returns 200, and gives any error in the reply.
  • ./jcr:title=RemovedTagMovies1 - Added this just for fun so we get a faster feedback.
  • :ignore - Ignore some of the errors
  • ./cq:tags@TypeHint=String[] - Tells the POST servlet how to handle the request. VERY IMPORTANT! Without it, it treats it like a String and removes everything.
  • ./cq:tags@Patch=true - I will modify on the values of this value. This allows us to use + and -
  • ./cq:tags=--geometrixx-media:entertainment/movies - The tag to remove.

Now, if I try to do the same thing on a "notag#" node like this:

curl -u admin:admin --data "_charset_=utf-8&%3Astatus=browser&.%2Fjcr%3Atitle=StillNoTagMovies1&%3Aignore=&.%2Fcq%3Atags%40TypeHint=String%5B%5D&.%2Fcq%3Atags%40Patch=true&.%2Fcq%3Atags=-geometrixx-media%3Aentertainment%2Fmovies"  http://localhost:4502/content/geometrixx-media/en/propertytest/notag1/jcr:content

the only thing that happens is that we change the title.

The "whole" bunch of curl-commands then is:

curl -u admin:admin --data "_charset_=utf-8&%3Astatus=browser&.%2Fjcr%3Atitle=RemovedTagMovies1&%3Aignore=&.%2Fcq%3Atags%40TypeHint=String%5B%5D&.%2Fcq%3Atags%40Patch=true&.%2Fcq%3Atags=-geometrixx-media%3Aentertainment%2Fmovies"  http://localhost:4502/content/geometrixx-media/en/propertytest/hastag1/jcr:content curl -u admin:admin --data "_charset_=utf-8&%3Astatus=browser&.%2Fjcr%3Atitle=StillNoTagMovies1&%3Aignore=&.%2Fcq%3Atags%40TypeHint=String%5B%5D&.%2Fcq%3Atags%40Patch=true&.%2Fcq%3Atags=-geometrixx-media%3Aentertainment%2Fmovies"  http://localhost:4502/content/geometrixx-media/en/propertytest/notag1/jcr:content curl -u admin:admin --data "_charset_=utf-8&%3Astatus=browser&.%2Fjcr%3Atitle=RemovedTagMovies1&%3Aignore=&.%2Fcq%3Atags%40TypeHint=String%5B%5D&.%2Fcq%3Atags%40Patch=true&.%2Fcq%3Atags=-geometrixx-media%3Aentertainment%2Fmovies"  http://localhost:4502/content/geometrixx-media/en/propertytest/hastag11/jcr:content curl -u admin:admin --data "_charset_=utf-8&%3Astatus=browser&.%2Fjcr%3Atitle=StillNoTagMovies1&%3Aignore=&.%2Fcq%3Atags%40TypeHint=String%5B%5D&.%2Fcq%3Atags%40Patch=true&.%2Fcq%3Atags=-geometrixx-media%3Aentertainment%2Fmovies"  http://localhost:4502/content/geometrixx-media/en/propertytest/notag11/jcr:content curl -u admin:admin --data "_charset_=utf-8&%3Astatus=browser&.%2Fjcr%3Atitle=RemovedTagMovies1&%3Aignore=&.%2Fcq%3Atags%40TypeHint=String%5B%5D&.%2Fcq%3Atags%40Patch=true&.%2Fcq%3Atags=-geometrixx-media%3Aentertainment%2Fmovies"  http://localhost:4502/content/geometrixx-media/en/propertytest/hastag12/jcr:content curl -u admin:admin --data "_charset_=utf-8&%3Astatus=browser&.%2Fjcr%3Atitle=StillNoTagMovies1&%3Aignore=&.%2Fcq%3Atags%40TypeHint=String%5B%5D&.%2Fcq%3Atags%40Patch=true&.%2Fcq%3Atags=-geometrixx-media%3Aentertainment%2Fmovies"  http://localhost:4502/content/geometrixx-media/en/propertytest/notag12/jcr:content curl -u admin:admin --data "_charset_=utf-8&%3Astatus=browser&.%2Fjcr%3Atitle=RemovedTagMovies1&%3Aignore=&.%2Fcq%3Atags%40TypeHint=String%5B%5D&.%2Fcq%3Atags%40Patch=true&.%2Fcq%3Atags=-geometrixx-media%3Aentertainment%2Fmovies"  http://localhost:4502/content/geometrixx-media/en/propertytest/hastag13/jcr:content curl -u admin:admin --data "_charset_=utf-8&%3Astatus=browser&.%2Fjcr%3Atitle=StillNoTagMovies1&%3Aignore=&.%2Fcq%3Atags%40TypeHint=String%5B%5D&.%2Fcq%3Atags%40Patch=true&.%2Fcq%3Atags=-geometrixx-media%3Aentertainment%2Fmovies"  http://localhost:4502/content/geometrixx-media/en/propertytest/notag13/jcr:content

/Ove