I am writing a C# application that needs to locate all documents that have been modified in the last 2 weeks...something like {LF:Modified>=%(Date)-14}. Is there a way to do this in the Laserfiche SDK? I am using RA (version 10.x)
Question
Question
Searching for documents modified in last 2 weeks in SDK
Answer
If you need that sort of flexibility, it sounds like you'll need to design your own micro-DSL for the search string that you can compile to the desired search syntax. Laserfiche doesn't currently have one, though this use case has come up before in the context of saved searches.
I assume you're not expecting the users to enter those strings in a UI? So changing behavior will require deploying updated configuration files anyway.
Replies
Here is how you do that in the SDK:
string timestamp = DateTime.Now.Subtract(TimeSpan.FromDays(14)).ToString("MM/dd/yyyy"); string searchCommand = String.Format("{{LF:Modified>={0}}}", timestamp); using (Search search = new Search(session)) { search.Command = searchCommand; search.Run(); SearchListingSettings settings = new SearchListingSettings(); settings.AddColumn(SystemColumn.Id); using (SearchResultListing listing = search.GetResultListing(settings)) { foreach (EntryListingRow row in listing) { int Id = (int)row[SystemColumn.Id]; EntryInfo entry = Entry.GetEntryInfo(Id, session); // Process the entry here } } }
Well, I was hoping to have a more generic solution so that I could setup the "tokens" in a config file so I could change them later without changing code. Thanks for the solution.
Would the TokenSubstituter be something I could use instead of your solution above? If so, anywhere I can get code examples of how to use the TokenSubstituter?
TokenSubstituter would not be able to handle the date subtraction. Instead of using a hard coded 14 you could use a config file and update the first line of the sample code to pull the number of days from there.
I understand what you are saying but this does not completely solve my problem. I want the flexibility to change the search expression to anything without publishing a new version of the application. For example, what if the business decided next month that they want to to not search for documents where LastModified is within 2 weeks but also where the Created is within a year ({LF:LastModified > %(Date) - 14} & {LF:Created > %(Date) - 365}). Now I suddenly have 2 variables that I have to substitute so I have to change my application.
If you need that sort of flexibility, it sounds like you'll need to design your own micro-DSL for the search string that you can compile to the desired search syntax. Laserfiche doesn't currently have one, though this use case has come up before in the context of saved searches.
I assume you're not expecting the users to enter those strings in a UI? So changing behavior will require deploying updated configuration files anyway.