You are viewing limited content. For full access, please sign in.

Discussion

Discussion

Create fields with 'SDK Script' Workflow activity

posted on October 12, 2021

Hello,

we need a way to mass create templates in a repository. We have a spreadsheet (see attached snippet) with a few hundred different template names and about twenty different fields. We're going to create the fields manually, but we're thinking that we have to run a workflow that has a custom script, or rule, which would read the template names and add the appropriate fields to them.

We're not very experienced with coding, so we're looking for some help. Does anyone have some code that they can share that will auto create templates?

Any other ideas or resources on how to get this done would be appreciated as well.

We're importing these into a Cloud repository, but don't care if it's easier to get this done with on-prem.

AXS Templates & Fields.JPG
0 0
replied on October 18, 2021 Show version history

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:

 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.