Click or drag to resize
Escaping Special Characters

This example shows how you have to escape some symbols on search.

When the search operation is applied some symbols have to be escaped because they are spacial symbols for DocuWare system or the environment. If they are not escaped then the platform will throw an exception with appropriate message.

Escaping special characters in search criteria by backslash.

The symbols that have to be escaped by backslash are ( ) "

C#
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using DocuWare.Platform.ServerClient;

namespace DocuWare.PlatformClientExamples
{
    static partial class ExamplesAsync
    {
        /// <summary>
        /// Escaping special symbols by adding backslash before the symbol.
        /// </summary>
        /// <remarks>Some of the special symbols that have to be escaped by backslash are ( ) "</remarks>
        public static void SearchEscapingByBackslash(Organization org)
        {
            FileCabinets fileCabinets = org.GetFileCabinetsFromFilecabinetsRelation();
            FileCabinet cabinet = fileCabinets.FileCabinet.FirstOrDefault((fc) => fc.Name == "Green FC");
            cabinet = cabinet.GetFileCabinetFromSelfRelation();

            string inputValue = "\"Invoice\" (In)"; // "Invoice" (In)

            // store a document with a field which contains some symbols that have to be escaped
            Document doc = StoreDocument(cabinet, inputValue);

            string searchCriteria = inputValue;

            try
            {
                // searching for a value without escaping with backslash symbol will throw an exception
                SearchForDocuments(cabinet, searchCriteria);
            }
            catch
            {
            }

            // create search criteria by escaping the symbols with backslash
            searchCriteria = EscapeSpecialSymbols(inputValue);

            // get the documents
            DocumentsQueryResult result = SearchForDocuments(cabinet, searchCriteria);
        }

        /// <summary>
        /// Stores a document in a file cabinet.
        /// </summary>
        private static Document StoreDocument(FileCabinet cabinet, string fieldValue)
        {
            var doc = new Document();
            doc.Fields = new List<DocumentIndexField>();
            doc.Fields.Add(new DocumentIndexField() { FieldName = "COMPANY", Item = fieldValue, ItemElementName = ItemChoiceType.String });

            doc = cabinet.UploadDocument(doc, new[] { new FileInfo("DOC NATIVE.doc") });
            doc = doc.GetDocumentFromSelfRelation();

            return doc;
        }

        /// <summary>
        /// Searches for documents in a file cabinet by search criteria.
        /// </summary>
        private static DocumentsQueryResult SearchForDocuments(FileCabinet cabinet, string searchCriteria)
        {
            DialogInfo cusomDialogInfo = cabinet.GetDialogFromCustomSearchRelation();
            Dialog customDialog = cusomDialogInfo.GetDialogFromSelfRelation();
            DocumentsQueryResult result = customDialog.Query.PostToDialogExpressionRelationForDocumentsQueryResult(new DialogExpression()
            {
                Condition = new List<DialogExpressionCondition>()
                {
                    new DialogExpressionCondition()
                    {
                        DBName = "COMPANY",
                        Value = new List<string>() { searchCriteria },
                    }
                }
            });

            return result;
        }

        /// <summary>
        /// Searches escaping symbols by backslash.
        /// </summary>
        /// <remarks>Some symbols that could be escaped by backslash are ( ) "</remarks>
        private static string EscapeSpecialSymbols(string searchCriteria)
        {
            string nonEscapedOpenBracket = "(?<!\\\\)[(]";  // match non escaped (
            string nonEscapedCloseBracket = "(?<!\\\\)[)]"; // match non escaped )
            string nonEscapedQuote = "(?<!\\\\)[\\\"]"; // match non escaped "

            var regex = new Regex(nonEscapedOpenBracket);
            var m = regex.Matches(searchCriteria);
            searchCriteria = regex.Replace(searchCriteria, @"\(");
            regex = new Regex(nonEscapedCloseBracket);
            searchCriteria = regex.Replace(searchCriteria, @"\)");
            regex = new Regex(nonEscapedQuote);
            searchCriteria = regex.Replace(searchCriteria, "\\\"");

            return searchCriteria;
        }
    }
}
Escaping search criteria by quoting the search criteria.

The symbols that have to be escaped by quoting the search criteria are ( ) < > =

C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocuWare.Platform.ServerClient;

namespace DocuWare.PlatformClientExamples
{
    public class SearchEscapingByQuates
    {
        /// <summary>
        /// Escaping special symbols by closing the search criteria by quotes.
        /// </summary>
        /// <remarks>
        /// Some of the special symbols that could be escaped are ( ) &lt; &gt; = 
        /// </remarks>
        public void SearchEscapeByQuotes(Organization org)
        {
            FileCabinets fileCabinets = org.GetFileCabinetsFromFilecabinetsRelation();
            FileCabinet cabinet = fileCabinets.FileCabinet.FirstOrDefault((fc) => fc.Name == "Green FC");
            cabinet = cabinet.GetFileCabinetFromSelfRelation();

            string inputValue = "(1) One is = to one, one is < then two, one is > then zero.";

            // store a document with a field which contains some symbols that have to be escaped
            Document doc = StoreDocument(cabinet, inputValue);

            string searchCriteria = inputValue;

            try
            {
                // searching for a value without escaping with backslash symbols will throw an exception
                SearchForDocuments(cabinet, searchCriteria);
            }
            catch
            {
            }

            // create search criteria by escaping the symbols with backslash
            searchCriteria = EscapeSpecialSymbols(inputValue);

            // get the documents
            DocumentsQueryResult result = SearchForDocuments(cabinet, searchCriteria);
        }

        private static Document StoreDocument(FileCabinet cabinet, string fieldValue)
        {
            var doc = new Document();
            doc.Fields = new List<DocumentIndexField>();
            doc.Fields.Add(new DocumentIndexField() { FieldName = "COMPANY", Item = fieldValue, ItemElementName = ItemChoiceType.String });

            doc = cabinet.UploadDocument(doc, new[] { new FileInfo("DOC NATIVE.doc") });
            doc = doc.GetDocumentFromSelfRelation();

            return doc;
        }

        private DocumentsQueryResult SearchForDocuments(FileCabinet cabinet, string searchCriteria)
        {
            DialogInfo cusomDialogInfo = cabinet.GetDialogFromCustomSearchRelation();
            Dialog customDialog = cusomDialogInfo.GetDialogFromSelfRelation();
            DocumentsQueryResult result = customDialog.Query.PostToDialogExpressionRelationForDocumentsQueryResult(new DialogExpression()
            {
                Condition = new List<DialogExpressionCondition>()
                {
                    new DialogExpressionCondition()
                    {
                        DBName = "COMPANY",
                        Value = new List<string>() { searchCriteria },
                    }
                }
            });

            return result;
        }

        /// <summary>
        /// Searches escaping symbols by backslash.
        /// </summary>
        /// <remarks>Some symbols that could be escaped by backslash are  ( ) &lt; &gt; = </remarks>
        /// <param name="searchCriteria"></param>
        public static string EscapeSpecialSymbols(string searchCriteria)
        {
            searchCriteria = string.Format("\"{0}\"", searchCriteria);

            return searchCriteria;
        }
    }
}