Saving a Generic Form Resource to the LiveCycle Repository

I found myself suggesting this as a possible solution on another thread today and thought it could be generally useful.

Here’s how you would use the CM API to create a new generic form resource, stuff your form template content into it and save it to the LiveCycle Repository using the currently-posted release:

import cm.model.CmObject;
import cm.model.CmSession;
import cm.model.repository.CmResourceType;
import cm.model.repository.CmRepositoryEvent;
import cm.model.repository.CmRepositoryFactory;
import cm.model.repository.CmResourceCollection;
import cm.model.repository.ICmRepository;

private function onBtnClick(event:Event):void
{
// create a new generic form template resource object
var xdp:CmObject = new CmObject();
xdp.resourceType = CmResourceType.FORM_TEMPLATE;
xdp.path = "/cm/content/myForm.xdp";
xdp.contentType = "application/vnd.adobe.xdp+xml";
xdp.contentData = '<xdp><template><subform name="root"><subform name="page"><field/></subform></subform></template></xdp>';

// make sure there's an active, authenticated session
// (note that username, password, server and port are variables you would supply)
if (CmSession.getInstance() == null || !CmSession.getInstance().authenticated)
CmSession.createInstance(username, password, server, port);

// get an instance of the LC Repository
var rep:ICmRepository = CmRepositoryFactory.getInstance();

// save it in the LC Repository
rep.save(xdp, true, onSave, onSaveFault);
}

private function onSave(event:CmRepositoryEvent):void
{
Alert.show("Saved XDP to LC Repository");
}

private function onSaveFault(event:FaultEvent):void
{
Alert.show("Error occurred while attempting to save: " + event.fault.toString());
}