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.
[toc]
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, implement the IScriptedContentFragmentContextProvider interface (which requires a reference to Telligent.Evolution.ScriptedContentFragments.dll). The IScriptedContentFragmentContextProvider interface extends IPlugin to add support for widget context providers.
First we need to identify which contexts we are defining. 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".
IEnumerable<ContextItem> IScriptedContentFragmentContextProvider.GetSupportedContextItems() { return new List<ContextItem>() { new ContextItem("Blog Example", BlogExampleContextItemId) }; }
Next we implement the HasContextItem method. This method should return true if the provided context identifier exists on the provided page. For unrecognized context identifiers, the provider should always return false. For recognized context identifiers, the provider should only return true if a recognized and valid object described by the context exists on the page.
bool IScriptedContentFragmentContextProvider.HasContextItem(System.Web.UI.Page page, Guid contextItemId) { return contextItemId == BlogExampleContextItemId && PublicApi.Url.CurrentContext != null && PublicApi.Url.CurrentContext.ContextItems.GetAllContextItems() .Any(item => item.ContentTypeId == PublicApi.Blogs.ApplicationTypeId); }
Here is the completed source code for our widget context provider:
using System; using System.Collections.Generic; using System.Linq; using Telligent.Evolution.Extensibility.Api.Version1; using Telligent.Evolution.Extensibility.UI.Version1; using Telligent.Evolution.Extensibility.Version1; namespace Samples { public class BlogExampleWidgetContext : IScriptedContentFragmentContextProvider { private readonly Guid BlogExampleContextItemId = new Guid("dd61aa2a-2595-4ced-a851-0871502911a0"); IEnumerable<ContextItem> IScriptedContentFragmentContextProvider.GetSupportedContextItems() { return new List<ContextItem>() { new ContextItem("Blog Example", BlogExampleContextItemId) }; } bool IScriptedContentFragmentContextProvider.HasContextItem(System.Web.UI.Page page, Guid contextItemId) { return contextItemId == BlogExampleContextItemId && PublicApi.Url.CurrentContext != null && PublicApi.Url.CurrentContext.ContextItems.GetAllContextItems() .Any(item => item.ContentTypeId == PublicApi.Blogs.ApplicationTypeId); } string IPlugin.Description { get { return "Enables studio widgets to be limited to pages with an Blog Application in context"; } } void IPlugin.Initialize() { } string IPlugin.Name { get { return "Blog Example Widget Context"; } } } }
Once this sample is deployed 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.