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.
Solved! Go to Solution.
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>
Open the form add a field named userID:
And set the field to be bound to the schema (this is important, the prefill seems to only work on bound fields):
On the adaptive form container, select the new prefill service:
When you preview you will see the username is populated:
Thanks,
James
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>
Open the form add a field named userID:
And set the field to be bound to the schema (this is important, the prefill seems to only work on bound fields):
On the adaptive form container, select the new prefill service:
When you preview you will see the username is populated:
Thanks,
James
Views
Likes
Replies
Views
Likes
Replies