Article Widget Contexts

Widgets define the context they require to render.  For example, the Blog - Post widget always displays the current contextual blog post.  It is dependent on having a blog post available to render.  The Blog - Post widget, therefore, defines "Blog Post" as a required context.

Why Should You Use Widget Contexts

Whether you are creating a new application or adding widgets to existing applications, it is important to use widget contexts.  If your widgets require certain items to be in context to function properly, widget contexts allow you to enforce this logic and prevent end users from adding widgets to pages where they would not function.

Creating a Widget Context Provider

To add a widget context,  that implements the IScriptedContentFragmentContextProvider  interface. The IScriptedContentFragmentContextProvider interface extends IPlugin  to add support for widget context providers:

using System;
using System.Collections.Generic;
using Telligent.Evolution.Extensibility.UI.Version2;
using Telligent.Evolution.Extensibility.Urls.Version1;
using Telligent.Evolution.Extensibility;
using Telligent.Evolution.Extensibility.Api.Version1;

namespace Samples
{
	public class BlogExampleWidgetContext : IScriptedContentFragmentContextProvider
	{
		private readonly Guid BlogExampleContextItemId = new Guid("dd61aa2a-2595-4ced-a851-0871502911a0");

		#region IPlugin

		public string Name
		{
			get { return "Blog Example Widget Context"; }
		}

		public string Description
		{
			get { return "Enables widgets to be limited to pages with a Blog Application in context"; }
		}

		public void Initialize()
		{
		}

		#endregion

		#region IScriptedContentFragmentContextProvider

		public IEnumerable<Telligent.Evolution.Extensibility.UI.Version1.ContextItem> GetSupportedContextItems()
		{
			return new List<Telligent.Evolution.Extensibility.UI.Version1.ContextItem>()
			{
					new Telligent.Evolution.Extensibility.UI.Version1.ContextItem("Blog Example", BlogExampleContextItemId)
			};
		}

		public bool HasContextItem(PageContext page, Guid contextItemId)
		{
			var blogContentTypeId = Apis.Get<IBlogs>()?.ContentTypeId;
			return blogContentTypeId.HasValue && page.ContextItems.Find(item => item.ContentTypeId == blogContentTypeId) != null;
		}

		#endregion
	}
}

First we need to identify which contexts we are defining within the GetSupportedContextItems() method. This method should return all contexts managed by this context provider.  Each ContextItem is defined using a Name and an Id.  The Name is shown in the widget editing user interface and the Id is used internally to identify this context.  In our sample we will define a single widget context with a name "Blog Example"

When Verint Community needs to know if a context is valid, it will call HasContextItem() with the current PageContext and the ID of the context to check. In the sample, the page's ContextItems collection is reviewed for any item with a ContentTypeId matching the blog ContentTypeId. If one exists, the context is available.

Once this   to Verint Community and the plugin has been enabled, you will be able to select the context provider when developing a widget in Widget Studio.

When laying out a page in the site, any widget that requires the "Blog Example" context will only be available to add to pages where a blog is in context.