Skip to content

IDocumentView

A document view publishes the documents of a model at a URL, for example as a feed or as JSON. IDocumentView decides which documents a request gets, and the endpoints on the view decide the format. Veva's Generic HTTP View covers most needs, so implement IDocumentView only when you need your own way of choosing the documents.

Interface declaration

	namespace Veva.Modules.Documents.Domain.DocumentViews;

	public interface IDocumentView
	{
		string Alias { get; set; }

		IEnumerable<IDocumentViewEndpoint> Endpoints { get; set; }

		Task<DocumentQueryResult> GetData(Guid modelId, DocumentViewContext context);
	}

Example

This view returns the documents that carry the tag given in the URL, such as ?tag=news, sorted on a field the editor picks. Without a tag it returns no documents rather than all of them.

	using System.ComponentModel;
	using Veva.Core.Domain.Components;
	using Veva.Core.Domain.Paging;
	using Veva.Core.Domain.Tagging;
	using Veva.Modules.Documents.Domain;
	using Veva.Modules.Documents.Domain.DocumentViews;
	using Veva.Modules.Documents.Domain.Services;

	[DisplayName("Tagged documents view")]
	[Description("Returns the documents that carry the tag given in the URL, such as ?tag=news")]
	public class TaggedDocumentsView(IDocumentQueryService queryService) : IDocumentView
	{
		// Keep the names Alias and Endpoints: the Views tab reads them to show the URLs.
		[EditorBrowsable]
		[DisplayName("Alias")]
		[Description("Becomes part of the view's URL. Use letters, digits and dashes.")]
		public string Alias { get; set; }

		[EditorBrowsable]
		[DisplayName("Count")]
		[Description("The most documents to return")]
		[DefaultValue(20)]
		public int Count { get; set; } = 20;

		[EditorBrowsable]
		[DisplayName("Sort by")]
		[Description("The field to sort by, newest or highest first")]
		[Editor(PropertyEditors.SortFieldSelector, "")]
		public string SortByField { get; set; }

		[EditorBrowsable]
		[Category("Endpoints")]
		[DisplayName("Endpoints")]
		[Description("The formats this view returns")]
		public IEnumerable<IDocumentViewEndpoint> Endpoints { get; set; }

		public async Task<DocumentQueryResult> GetData(Guid modelId, DocumentViewContext context)
		{
			var tag = context.QueryParams?["tag"].ToString();

			if (string.IsNullOrWhiteSpace(tag))
			{
				return new DocumentQueryResult { ModelId = modelId, Documents = [] };
			}

			// Only active documents: the query leaves out disabled ones and those outside their display dates.
			return await queryService.GetDocuments(
				modelId,
				new Paging { Skip = 0, Count = Count },
				new OrderBy { Column = SortByField, Descending = true },
				folderIds: null,
				fieldFilter: null,
				tagFilter: new TagFilter { Tags = [tag.Trim()], FilterType = TagFilterType.Any }
			);
		}
	}

Registration

This is how the implementation is registered into the DI container in Veva, this should be done in your module config file (The class which implements IModule).

	using Veva.Core.Application.DependencyInjection;
	using Veva.Modules.Documents.Domain.DocumentViews;

	public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
	{
		// The key is the class name; the model stores it when the view is added.
		services.AddNamed<IDocumentView, TaggedDocumentsView>(ServiceLifetime.Transient);
	}

Views and endpoints. A view chooses the documents; an endpoint, an IDocumentViewEndpoint, turns them into one format. A view holds a list of endpoints, each with its own alias, so one set of documents can be served as RSS, JSON and more.

Turning one on. Registering a view only makes it available. On the model designer's Views tab, add the view, give it an Alias and add at least one endpoint with an alias of its own. A view can be switched off without removing it. The tab then shows the URLs: /api/documents/views/, then the model's id or alias, the view alias and the endpoint alias, separated by slashes. The list shows the class's [DisplayName] and [Description], and its [EditorBrowsable] properties become the view's settings. The model stores the view under its registration name, which is the class name.

Who can read it. The URL works without signing in, but Veva checks the Query permission that the view's Permissions for this view panel sets, and answers 401 Unauthorized to a caller without it. The permission covers the whole view. If some documents are private to their owner, filter on context.CurrentUser in GetData, as the Generic HTTP View's Restrict to user setting does.

How a request runs. Veva finds the model by id or alias, then the enabled view with the alias in the URL that has an endpoint with the requested alias. It creates the view from the service container with its stored settings, so constructor injection works, and checks the permission. It calls GetData, passes the result to the endpoint, and returns the endpoint's bytes with its MimeType. Veva does not cache the response, so GetData runs on every request. IDocumentQueryService keeps a query's results for a short time, up to a minute, so a change can take that long to show.

What you can read. DocumentViewContext gives RootUrl, the site's absolute root URL, CurrentUser, and QueryParams, the request's query string. Veva's Generic HTTP View passes the query string to its filters as data binding values, so a URL can narrow the documents. Build the result with IDocumentQueryService (Veva.Modules.Documents.Domain.Services), as the example does. With its default arguments it returns only active documents: not disabled, and inside their display dates.

Pitfalls. Aliases must be unique. If two views share an alias and both have the requested endpoint, or a view has two endpoints with one alias, the request fails with an error. A URL without an endpoint alias gets 404. Keep the result small with a count: the endpoint formats every document it gets.

Built-in. Veva's Generic HTTP View (GenericHTTPDocumentView) has settings for filters, Restrict to user, count, skip and sorting, and holds the endpoints. The standard endpoints are RSS, XML, JSON and iCalendar.

Type to search the docs, components and API.

↑↓ to move ↵ to open Esc to close Search by Veva