IExternalAction code example: Enterprise Search document details

Smartsite 7.4 - ...

using Smartsite.Base; using Smartsite.Data; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Smartsite.Manager.Actions { internal class ESItemDetailsAction : BaseAction, IExternalAction { public bool AcceptsMultipleKeys { get { return false; } } public override ActionResult Execute(MgrContext context) { // Capture sub action result and pass it on. Should always be CloseAction. var sar = context.Action.GetSubactionResult(); if (sar != null) { return sar; } // Get the primary key list from the action parameters. object par = null; context.Action.Parameters.TryGetValue(ParamPrimaryKeys, out par); List keys = par as List; if (keys == null || keys.Count != 1) throw new ArgumentException("Test action expects one pimary key"); var esKey = GetEsKey(keys[0]); if (!esKey.HasValue || esKey.IsDbNull) throw new SmartsiteException("No es document found for primary key " + keys[0]); // Start enterprise search document details as sub action. ActionDefinition details = Site.Current.Actions["mgr-ES_DOCUMENTDETAILS"]; Dictionaryobject> parameters = new Dictionaryobject>(); parameters.Add("nr", esKey.Value.ToString()); return ActionResult.StartSubAction(details, parameters); } public ExternalActionButton GetButton(MgrContext context, ActionDefinition actionDefinition, List primaryKeys) { ExternalActionButton b = new ExternalActionButton(); b.Caption = "ES details"; b.CssClass = "icon-sys22-es-documents"; // Don't show the button if the Enterprise search document details action does not exist, or the primarykeys doesn't have exactly 1 value. if (!Site.Current.Actions.HasAccess("mgr-ES_DOCUMENTDETAILS") || primaryKeys == null || primaryKeys.Count != 1) { b.Visible = false; return b; } b.Enabled = IsButtonEnabled(context, actionDefinition, primaryKeys); return b; } public bool IsButtonEnabled(MgrContext context, ActionDefinition actionDefinition, List selection) { /* Button is enabled if: * there is eaxactly 1 primary key in the selection * there is an enterprise search document associated with the smartsite item */ if (selection == null || selection.Count != 1) return false; string primaryKey = selection[0]; var esKey = GetEsKey(primaryKey); return esKey.HasValue && !esKey.IsDbNull; } private DbNullable GetEsKey(string key) { return Database.Current.ExecuteScalar(@"select d.nr from esDocuments d join esSources s on s.nr=d.nrSource join esProviderDefinitions pd on pd.nr=s.nrProviderDefinition where systemlocation like ? and pd.code='SMARTSITEPROVIDER'", key + ":%"); } } }