Expand my Community achievements bar.

SOLVED

update metadata through workflow.

Avatar

Level 1

I'm trying to update metadata with user id through workflow, when I was run the workflow on the particular that asset metadata should be update with the user id.

Write a workflow to save the name/id of the user on the metadata of the asset on which the workflow was run on in the field- "user id".

I need java code for this.

1 Accepted Solution

Avatar

Correct answer by
Level 8

@Nandigama_Vamsi 

I hope this article might help you.

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-do-we-get-current-aem-...

 

I hope you are aware of wring custom work flow process (Java which implements WorkflowProcess).  Here, you need to write the actual logic.

View solution in original post

4 Replies

Avatar

Correct answer by
Level 8

@Nandigama_Vamsi 

I hope this article might help you.

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-do-we-get-current-aem-...

 

I hope you are aware of wring custom work flow process (Java which implements WorkflowProcess).  Here, you need to write the actual logic.

Avatar

Community Advisor

Hi @Nandigama_Vamsi   
how to update the metadata of an asset with the user ID using a workflow

import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import org.apache.sling.api.resource.ResourceResolver;

public class UpdateMetadataWorkflowProcess {

    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) {
        try {
            // Get the user ID
            String userId = workItem.getWorkflow().getInitiator();

            // Get the asset path
            String assetPath = workItem.getWorkflowData().getPayload().toString();

            // Get the resource resolver
            ResourceResolver resourceResolver = workflowSession.adaptTo(ResourceResolver.class);

            // Update the metadata of the asset
            Resource assetResource = resourceResolver.getResource(assetPath);
            ModifiableValueMap metadata = assetResource.adaptTo(ModifiableValueMap.class);
            metadata.put("user id", userId);
            resourceResolver.commit();

            // Log the success message
            workflowSession.getWorkflowData().getMetaDataMap().put("success", "Metadata updated successfully");
        } catch (Exception e) {
            // Log the error message
            workflowSession.getWorkflowData().getMetaDataMap().put("error", "Failed to update metadata: " + e.getMessage());
        }
    }
}


 Ref :
https://experienceleague.adobe.com/docs/experience-manager-65/content/implementing/developing/extend... 



Avatar

Level 8

Hi @Raja_Reddy   @Nandigama_Vamsi 

I have checked above logic by implementing WorkflowProcess .

 

public class UpdateMetadataWorkflowProcess implements WorkflowProcess { ...}

 

String userId = workitem.getWorkflow().getInitiator();
// value of userId: workflow-service

Session session = worksession.adaptTo(Session.class);
String userId = session.getUserID();
// value of userId: workflow-process-service

 

@Nandigama_Vamsi  are you expecting this user ID?  or the logged in user ID (the guy has uploaded the DAM asset).

 

Avatar

Level 1

Thank you for the reply.

@Raja_Reddy 

@Mahesh_Gunaje 

 

when I'm run the workflow the asset metadata property user id field as to create and fill with my userid that logging.

Example if I login with admin, when I run the workflow the metadata property of user id field updated with admin.