Hi Nareg -
Set up two tables, one with a distinct list of template names, and the other with the contents above.
Create the templates first:
Use a query task to get the list of templates and create them using:
TemplateInfo myTemplate = new TemplateInfo(this.RASession);
string NewTemplateName = Convert.ToString(GetTokenValue("ForEachTemplate_TemplateName");
myTemplate.Name = NewTemplateName;
bool TemplateOverwrite = Convert.ToBoolean("False");
Template.Create(myTemplate, TemplateOverwrite, myTemplate.Session);
using a for each row task.
Note: Make sure your DB datatype is varchar or nvarchar, otherwise you may come across the dreaded 10.2 error, "Invalid object Name" due to the trailing spaces.
See: https://answers.laserfiche.com/questions/153995/Possible-SDK-Bug-Could-someone-test-for-me-Searching-and-spaces-in-the-document-name
Then add fields to the templates:
From the second table, loop through the first table for the names and query the second table the fields for that template. Loop through the fields with:
protected override void Execute()
{
try
{
FieldInfo NewField;
int nFieldType = Convert.ToInt16(GetTokenValue("ForEachRow_KeywordDataType"));
switch (nFieldType)
{case 1:
NewField = new FieldInfo(FieldType.String);
NewField.Length = Convert.ToInt16 (GetTokenValue("ForEachRow_Length"));
//sFieldType = "FieldType.String";
break;
case 2:
NewField = new FieldInfo(FieldType.Number);
break;
case 3:
NewField = new FieldInfo(FieldType.Date);
break;
default:
NewField = new FieldInfo(FieldType.String);
break;
}
string NewFieldName = Convert.ToString(GetTokenValue("ForEachRow_LF_Field"));
NewField.Name = NewFieldName;
bool Overwrite = false;
Laserfiche.RepositoryAccess.Field.Create(NewField, Overwrite, this.RASession);
SetTokenValue("FieldResult", "True");
}
catch (Exception e)
{
// This error is caught and we throw the exception to the WorkFlow
SetTokenValue("FieldResult", "False");
//throw new LaserficheRepositoryException("Error in script", e);
}
}
}
}
So you do all Templates and then all the fields, using Select Distinct [field name] for the fields.
Next, we'll assign the fields to the correct templates...here is the heart of it:
protected override void Execute()
{
try
{
string CurrentTemplate = Convert.ToString(GetTokenValue("CurrentTemplate"));
TemplateInfo myTemplate = Template.GetInfo(CurrentTemplate, this.RASession);
int TemplateID = myTemplate.Id;
string CurrentField = Convert.ToString(GetTokenValue("ForEachRow_LF_Field"));
if (myTemplate.ContainsField (CurrentField) == false)
{
myTemplate.AddField(CurrentField);
myTemplate.Refresh(true);
}
myTemplate.Save();
}
catch (Exception e)
{
// This error is caught and we throw the exception to the WorkFlow
SetTokenValue("FieldResult", "False");
//throw new LaserficheRepositoryException("Error in script", e);
}
}
}
Here we get the templates from one table and a list of fields for that template from a different table, with tracking information at the end: