Expand my Community achievements bar.

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

Avatar

Level 4

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 

Topics

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

5 Replies

Avatar

Community Advisor

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);

Avatar

Level 4

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,

Avatar

Level 4

Hi @SureshDhulipudi,

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

As createdBy is protected property, its giving error.

 

Avatar

Community Advisor

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

Avatar

Administrator

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



Kautuk Sahni