Skip to main content
sesmic
Level 4
Oct 17, 2024 4:30 PM UTC
Solved

How to get username in createdBy property of contentfragment/folder when workflow is started by that user?

Hi All,

I have business requirement where I'm creating folders & content fragment using the workflow.

So, for this I created one custom process step where I'm getting ResourceResolver after execute method as below:

try(ResourceResolver rr = workflowSession.adaptTo(ResourceResolver.class)){ Resource cfModel = rr.getReource(cfmpath); FragmentTemplate ft = cfModel.adaptTo(FragmentTemplate.class); ContentFragment cf = ft.createFragment(folder, cfName, cfTitle); }

This is working fine, but when I check CF's property jcr:createdBy="admin" is coming.

Instead, I want the user/account name who has triggered this workflow.

 

Could anyone please advise me as to how I get the desired result?

 

Many Thanks,

sesmic 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
SureshDhulipudi
SureshDhulipudiAccepted solution
Community Advisor
Oct 20, 2024 11:10 PM UTC

You cannot directly modify the jcr:createdBy property as it is protected. Instead, you should create the content fragment using a ResourceResolver that is authenticated as the user who started the workflow. This way, the jcr:createdBy property will be set correctly.

 

String userId = workflowSession.getUser().getID();

// Create a ResourceResolver with the user ID
Map<String, Object> authInfo = new HashMap<>();
authInfo.put(ResourceResolverFactory.USER, userId);

 

can you please provide complete code here

5 replies

SureshDhulipudi
Community Advisor
Community Advisor
Oct 18, 2024 2:33 AM UTC

First get the user id from workflow session , then set the jcr:createdBy with the userId

 

String userId = workflowSession.getUser().getID();
ValueMap properties = cf.adaptTo(Resource.class).adaptTo(ValueMap.class);
properties.put("jcr:createdBy", userId);
Translate
sesmic
sesmicAuthor
Level 4
Oct 18, 2024 6:01 AM UTC

Hi @sureshdhulipudi,

Thanks for the reply.

Correct, we can do that by using ModifiableValueMap, putting userID.

But is there other way such that by default any asset/folder is created via workflow, it should be in name of initiator's ID.

There is more than one asset that I'm creating via workflow so for each I need to modify valuemap.

Please let me know if we any alternate like using jcrSession or something, I will go with this option only.


Thanks Again,

Translate
sesmic
sesmicAuthor
Level 4
Oct 20, 2024 6:21 AM UTC

Hi @sureshdhulipudi,

I tried the approach of valuemap but it's not working.

As createdBy is protected property, its giving error.

 

Translate
SureshDhulipudi
Community Advisor
SureshDhulipudiCommunity AdvisorAccepted solution
Community Advisor
Oct 20, 2024 11:10 PM UTC

You cannot directly modify the jcr:createdBy property as it is protected. Instead, you should create the content fragment using a ResourceResolver that is authenticated as the user who started the workflow. This way, the jcr:createdBy property will be set correctly.

 

String userId = workflowSession.getUser().getID();

// Create a ResourceResolver with the user ID
Map<String, Object> authInfo = new HashMap<>();
authInfo.put(ResourceResolverFactory.USER, userId);

 

can you please provide complete code here

Translate
kautuk_sahni
Level 9
Nov 5, 2024 2:30 PM UTC

@sesmic Did you find the suggestion 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!

Translate
Kautuk Sahni