Expand my Community achievements bar.

SOLVED

Prepopulate logged in user using prefill service in adaptive form

Avatar

Level 3

Hi

How to prepopulate a field with logged in username in AEM adaptive form 6.3 using Prefill service?

Kindly point to documentation or sample.

1 Accepted Solution

Avatar

Correct answer by
Level 7

Hi prabuj201987

Here is an example:

Add the following code to a new service and deploy to your server:

package <your package>

import com.adobe.forms.common.service.DataXMLOptions;

import com.adobe.forms.common.service.DataXMLProvider;

import com.adobe.forms.common.service.FormsException;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.api.resource.ResourceResolver;

import org.osgi.service.component.annotations.Component;

import org.w3c.dom.Document;

import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.transform.*;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.InputStream;

import java.io.StringReader;

@Component(

  immediate = true,

  service = DataXMLProvider.class
)

public class FormsServicePrefillImpl implements DataXMLProvider

{

   public FormsServicePrefillImpl() {}

   public InputStream getDataXMLForDataRef(DataXMLOptions dataXmlOptions) throws FormsException

  {

  Resource aemFormContainer = dataXmlOptions.getFormResource();

  ResourceResolver resolver = aemFormContainer.getResourceResolver();

  String userID = resolver.getUserID();

   try
   {

  DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();

  DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

  Document doc = docBuilder.newDocument();

  TransformerFactory transformerFactory = TransformerFactory.newInstance();

  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

  Transformer transformer = transformerFactory.newTransformer();

  String xmlStr = "<data><userID>" + userID + "</userID></data>";

  DOMSource source = new DOMSource(convertStringToDocument(xmlStr));

  StreamResult outputTarget = new StreamResult(outputStream);

  TransformerFactory.newInstance().newTransformer().transform(source, outputTarget);

   return new ByteArrayInputStream(outputStream.toByteArray());

  }

   catch (ParserConfigurationException e)

  {

  e.printStackTrace();

  } catch (TransformerConfigurationException e) {

  e.printStackTrace();

  } catch (TransformerException e) {

  e.printStackTrace();

  } catch (TransformerFactoryConfigurationError e) {

  e.printStackTrace();

  }

   return null;

  }

   public String getServiceDescription()

  {

   return "Example Prefill Service";

  }

   public String getServiceName()

  {

   return "formsServicePrefillImpl";

  }

   private static Document convertStringToDocument(String xmlStr)

  {

  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

   try
   {

  DocumentBuilder builder = factory.newDocumentBuilder();

   return builder.parse(new InputSource(new StringReader(xmlStr)));

  }

   catch (Exception e) {

  e.printStackTrace();

  }

   return null;

  }

}

Create a new form and set the form model to use the following xml schema:

<?xml version="1.0" encoding="UTF-8" ?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="user">

  <xs:complexType>

    <xs:sequence>

      <xs:element name="userID" type="xs:string"/>

    </xs:sequence>

  </xs:complexType>

</xs:element>

</xs:schema>

Screen Shot 2018-03-03 at 19.30.20.png

Open the form add a field named userID:

Screen Shot 2018-03-03 at 19.23.14.png

And set the field to be bound to the schema (this is important, the prefill seems to only work on bound fields):

Screen Shot 2018-03-03 at 19.24.00.png

On the adaptive form container, select the new prefill service:

Screen Shot 2018-03-03 at 19.23.35.png

When you preview you will see the username is populated:

Screen Shot 2018-03-03 at 19.34.25.png

Thanks,

James

View solution in original post

1 Reply

Avatar

Correct answer by
Level 7

Hi prabuj201987

Here is an example:

Add the following code to a new service and deploy to your server:

package <your package>

import com.adobe.forms.common.service.DataXMLOptions;

import com.adobe.forms.common.service.DataXMLProvider;

import com.adobe.forms.common.service.FormsException;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.api.resource.ResourceResolver;

import org.osgi.service.component.annotations.Component;

import org.w3c.dom.Document;

import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.transform.*;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.InputStream;

import java.io.StringReader;

@Component(

  immediate = true,

  service = DataXMLProvider.class
)

public class FormsServicePrefillImpl implements DataXMLProvider

{

   public FormsServicePrefillImpl() {}

   public InputStream getDataXMLForDataRef(DataXMLOptions dataXmlOptions) throws FormsException

  {

  Resource aemFormContainer = dataXmlOptions.getFormResource();

  ResourceResolver resolver = aemFormContainer.getResourceResolver();

  String userID = resolver.getUserID();

   try
   {

  DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();

  DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

  Document doc = docBuilder.newDocument();

  TransformerFactory transformerFactory = TransformerFactory.newInstance();

  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

  Transformer transformer = transformerFactory.newTransformer();

  String xmlStr = "<data><userID>" + userID + "</userID></data>";

  DOMSource source = new DOMSource(convertStringToDocument(xmlStr));

  StreamResult outputTarget = new StreamResult(outputStream);

  TransformerFactory.newInstance().newTransformer().transform(source, outputTarget);

   return new ByteArrayInputStream(outputStream.toByteArray());

  }

   catch (ParserConfigurationException e)

  {

  e.printStackTrace();

  } catch (TransformerConfigurationException e) {

  e.printStackTrace();

  } catch (TransformerException e) {

  e.printStackTrace();

  } catch (TransformerFactoryConfigurationError e) {

  e.printStackTrace();

  }

   return null;

  }

   public String getServiceDescription()

  {

   return "Example Prefill Service";

  }

   public String getServiceName()

  {

   return "formsServicePrefillImpl";

  }

   private static Document convertStringToDocument(String xmlStr)

  {

  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

   try
   {

  DocumentBuilder builder = factory.newDocumentBuilder();

   return builder.parse(new InputSource(new StringReader(xmlStr)));

  }

   catch (Exception e) {

  e.printStackTrace();

  }

   return null;

  }

}

Create a new form and set the form model to use the following xml schema:

<?xml version="1.0" encoding="UTF-8" ?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="user">

  <xs:complexType>

    <xs:sequence>

      <xs:element name="userID" type="xs:string"/>

    </xs:sequence>

  </xs:complexType>

</xs:element>

</xs:schema>

Screen Shot 2018-03-03 at 19.30.20.png

Open the form add a field named userID:

Screen Shot 2018-03-03 at 19.23.14.png

And set the field to be bound to the schema (this is important, the prefill seems to only work on bound fields):

Screen Shot 2018-03-03 at 19.24.00.png

On the adaptive form container, select the new prefill service:

Screen Shot 2018-03-03 at 19.23.35.png

When you preview you will see the username is populated:

Screen Shot 2018-03-03 at 19.34.25.png

Thanks,

James