posted on September 18, 2017

I'm looking for a way to validate field formatting without actually creating an entry in the repository.  From what I can tell, the EntryInfo.GetBadFieldValues() method only returns data when .Save() fails.

 

I also noticed that I'm not able to call .SetFieldValues() without doing .Create() first.  Is there a better way to do this?

 

Here's my little POC project:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Laserfiche.RepositoryAccess;

namespace FieldValidationPOC
{
    class Program
    {
        static void Main(string[] args)
        {
            using (Session sess = Session.Create("localhost", "EricDev"))
            {
                Dictionary<string, object> fields = new Dictionary<string, object>
                {
                    { "Due Date", DateTime.Parse("10/10/2017") },
                    { "Test Date", "10102017" },
                    { "Document Date", "10/10/2017" }
                };
                FieldValueCollection fvc = new FieldValueCollection();

                foreach (KeyValuePair<string, object> field in fields)
                {
                    fvc.Add(field.Key, field.Value);
                }

                FolderInfo folder = Folder.GetFolderInfo(2218, sess);
                DocumentInfo doc = new DocumentInfo(sess);

                doc.Create(folder, "TEST DOC", EntryNameOption.AutoRename);
                doc.SetFieldValues(fvc);
                try
                {
                    doc.Save();
                }
                catch (MultiStatusException ex)
                {
                    for (int i=0; i < ex.ExtraErrorCount; i++)
                    {
                        Console.WriteLine(ex.GetExtraError(i).TargetSite);
                        Console.WriteLine(ex.GetExtraError(i).Message);
                        Console.WriteLine(ex.GetExtraError(i).PropName);
                        Console.WriteLine("------------------------------");
                    }

                    var failedFields = doc.GetBadFieldValues();
                    Console.WriteLine(ex);
                    foreach (var field in failedFields)
                        Console.WriteLine($"Bad Fields: {field.ObjectName} {field.ToString()}");
                    Console.ReadKey();
                    doc.Delete();
                    doc.Save();                    
                }


                var badFields = doc.GetBadFieldValues();

                foreach (var field in badFields)
                    Console.WriteLine($"Bad Field Value: {field}");

            }
        }
    }
}

Thanks,

Eric

0 0