---
title: TemplateSearchService
summary: API reference for TemplateSearchService.
status: draft
owner: SAIF Doc Gen
last_reviewed: 2026-09-24
library: SAIF.Services
reference_type: package
---

# TemplateSearchService

Implements `ITemplateService`, providing the full document-template administration and search surface used by the Doc Center: code-table lookups, OnBase document type/subtype lookups (via an `ImagingClient` built from the `imagingapiurl` config setting), template search/browse by group or recent-use, and full template CRUD including output-profile and option assignment. It depends on `IUnitOfWork`, `IUserProfileService`, and `IEnvironmentService`, restricting non-admin users (those without the `DocCenterDevAdmin`/`DocCenterProdAdmin` roles) from seeing templates in the `Corporate`/`Admin` business group and subgroup.

## Purpose
- Exposes document-related code tables (run types, context types, output profile codes, option codes, template output profiles/options) for UI population.
- Looks up OnBase document type groups, types, and subtypes via the imaging API client.
- Searches and browses templates by keyword, business group/subgroup, or recent request activity, applying role-based visibility filtering.
- Creates, updates, disables, and deletes document templates, including their output-profile and option assignments and cascading cleanup of dependent requests/distributions.
- Generates the raw SQL statements needed to recreate a template's data (`GetDocumentSqlLines`).

## Context
- Namespace: `SAIF.Services.Documents`
- Assembly/package: `SAIF.Services` (packaged inside the `SAIF.SAIFCode.Core` NuGet package)

## Public members
| Member | Signature | Purpose |
| --- | --- | --- |
| TemplateSearchService(...) | `public TemplateSearchService(IUnitOfWork uow, IUserProfileService ups, IEnvironmentService env)` | Stores the unit of work and user-profile collaborators (requiring both non-null) and constructs an `ImagingClient` from the `imagingapiurl` config setting. |
| GetRunTypes | `public IEnumerable<Contract.RunTypeCode> GetRunTypes()` | Returns all run-type code table entries, mapped to contracts. |
| GetContextTypes | `public IEnumerable<Contract.ContextCode> GetContextTypes()` | Returns all context-type code table entries, mapped to contracts. |
| GetDocumentOutputProfileCodes | `public IEnumerable<Contract.DocumentOutputProfileCode> GetDocumentOutputProfileCodes()` | Returns all document output-profile code table entries, mapped to contracts. |
| GetDocumentOptionCodes | `public IEnumerable<Contract.DocumentOptionCode> GetDocumentOptionCodes()` | Returns all document option code table entries, mapped to contracts. |
| GetAllTemplateOutputProfiles | `public IEnumerable<Contract.DocumentTemplateOutputProfile> GetAllTemplateOutputProfiles()` | Returns every template-to-output-profile assignment across all templates. |
| GetOuputProfilesForOneTemplate | `public IEnumerable<Contract.DocumentTemplateOutputProfile> GetOuputProfilesForOneTemplate(long inId)` | Returns the output-profile assignments for template `inId`. |
| GetAllTemplateOptions | `public IEnumerable<Contract.DocumentTemplateOption> GetAllTemplateOptions()` | Returns every template-to-option assignment across all templates. |
| GetOptionsForOneTemplate | `public IEnumerable<Contract.DocumentTemplateOption> GetOptionsForOneTemplate(long inId)` | Returns the option assignments for template `inId`. |
| GetOnBaseTypeGroups | `public IEnumerable<Contract.OnBaseTypeGroup> GetOnBaseTypeGroups()` | Returns the OnBase document type groups retrieved from the imaging API client. |
| GetOnBaseTypes | `public IEnumerable<Contract.OnBaseType> GetOnBaseTypes(string inDocumentTypeGroup)` | Returns the OnBase document types belonging to `inDocumentTypeGroup`. |
| GetOnBaseType | `public Contract.OnBaseType GetOnBaseType(string inDocumentTypeGroupCode, string inDocumentTypeCode)` | Returns the single OnBase document type matching both the group and type code. |
| GetOnBaseSubTypes | `public IEnumerable<Contract.OnBaseSubType> GetOnBaseSubTypes(string inDocumentTypeGroupCode, string inDocumentTypeCode)` | Returns the OnBase document subtypes matching the given group and type code. |
| GetOnBaseSubType | `public Contract.OnBaseSubType GetOnBaseSubType(string inDocumentTypeGroupCode, string inDocumentTypeCode, string inDocumentSubTypeCode)` | Returns the single OnBase document subtype matching the group, type, and subtype codes. |
| GetRecentRequests | `public IEnumerable<Contract.TemplateSearchResult> GetRecentRequests(int resultCount, string requestedSort, IApplicationUserProfile currentUser)` | Returns up to `resultCount` distinct templates the current user requested in the last 3 months (excluding disabled/expired/admin-only templates unless the user is an admin), each annotated with its request count and most recent request date, sorted per `requestedSort`. |
| GetSearchResults | `public IEnumerable<Contract.TemplateSearchResult> GetSearchResults(string searchTerm)` | Returns enabled, currently-effective, visibility-filtered templates whose name or title contains `searchTerm` (case-insensitive). |
| GetTemplatesByGroups | `public IEnumerable<Contract.TemplateSearchResult> GetTemplatesByGroups(string businessGroups, string subgroups)` | Returns enabled, visibility-filtered templates whose business group and/or subgroup match any of the supplied comma-separated `businessGroups`/`subgroups` values, unioning matches across each list. |
| GetTemplateOrDefault | `public Contract.Template GetTemplateOrDefault(long templateId)` | Returns the full template record for `templateId`, mapped to the contract type, or a default mapping if not found. |
| GetTemplateAdminList | `public IEnumerable<Contract.TemplatesList> GetTemplateAdminList()` | Returns every template (regardless of enabled/visibility status) as an admin-list summary contract. |
| CreateTemplate | `public long CreateTemplate(Contract.Template createdTemplate)` | Validates `createdTemplate`, creates the template record (defaulting business group/subgroup and normalizing MACRO routine/template names to uppercase), commits it, then adds its output-profile and option assignments, returning the new template's ID; throws if validation fails. |
| UpdateTemplate | `public void UpdateTemplate(Contract.Template updatedTemplate)` | Validates `updatedTemplate`, overwrites the matching template record's fields in one transaction, then replaces its output-profile and option assignments (delete-then-recreate); throws if validation fails. |
| AddTemplateOutputProfiles | `public void AddTemplateOutputProfiles(long templateId, string opListIn)` | Adds a `DocumentTemplateOutputProfile` assignment to `templateId` for each comma-separated code in `opListIn`, committed in one transaction. |
| DeleteTemplateOutputProfiles | `public void DeleteTemplateOutputProfiles(long templateId)` | Deletes all output-profile assignments currently on `templateId`. |
| AddTemplateOptions | `public void AddTemplateOptions(long templateId, string opListIn)` | Adds a `DocumentTemplateOption` assignment to `templateId` for each comma-separated code in `opListIn`, committed in one transaction. |
| DeleteTemplateOptions | `public void DeleteTemplateOptions(long templateId)` | Deletes all option assignments currently on `templateId`. |
| DeleteRequestDistributions | `public void DeleteRequestDistributions(long requestId)` | Deletes all distribution log entries belonging to request `requestId`. |
| DeleteTemplate | `public void DeleteTemplate(long inId)` | Cascades deletion of `inId`'s output-profile assignments, option assignments, and all its requests' distributions and request logs, then deletes the template itself; throws if the template does not exist. |
| DisableTemplate | `public void DisableTemplate(long inId)` | Appends `" disabled"` to the template's title and sets `IsEnabled` to false rather than deleting it, stamping update user/date/number; throws if the template does not exist. |
| GetDocumentSqlLines | `public IList<string> GetDocumentSqlLines(long templateId)` | Generates a list of SQL statement strings capturing the current state of template `templateId` (and, per the calling code base, its related tables) for scripting/migration purposes. |

## Behavioral notes
- Read paths funnel through two private query properties: `TemplateQueryFiltered` (applies enabled/effective-date and role-based Corporate/Admin visibility filters) versus `TemplateQuery` (unfiltered, used by admin-facing and ID-based lookups).
- `CreateTemplate` and `UpdateTemplate` both call a shared `TemplateErrorMessage` validator before writing and throw a plain `Exception` (not a custom exception type) with the first validation error message.
- `UpdateTemplate` always deletes and recreates output-profile and option assignments rather than diffing them, so any assignment metadata beyond the code list is lost and rebuilt on every update*.
- `userCanSeeAll` gates visibility on the `DocCenterDevAdmin`/`DocCenterProdAdmin` roles specifically (not the general `DocCenterAdmin` role constant also defined on this type), which determines whether `Corporate`/`Admin`-scoped templates are included in search results*.
- Static constructor registers the Documents AutoMapper maps (`CreateDocumentsCodeTableMaps`, `CreateTemplateSearchResultMap`, `CreateTemplateMap`, `CreateTemplateAdminMap`, `CreateRequestLogSearchResultMap`) once per AppDomain via `DocumentsMappingHelper`.

> **Note:** Statements marked with * were inferred from code structure, naming conventions, or
> contextual evidence where direct confirmation was not available in source artifacts. Statements
> marked with ** could not be confirmed; treat as unknown until verified.

## Sources
- Services/SAIF.Services/Documents/TemplateSearchService.cs
