November 29, 2010

How to display popup messages in the admin

If you need to interact with the sitecore interface with a custom field or a custom sitecore admin screen you will probably need some input from the user, alert, ...

This operation is possible with only a few lines of code:

Show a popup:
Sitecore.Context.ClientPage.ClientResponse.Alert("My message");

Download a file
Sitecore.Context.ClientPage.ClientResponse.Download(@"c:\MyPath\Myfile.xxx");

To create an input, use this function:
Input(string text, string defaultValue, string validation, string validationText, int maxLength)

A complete example of code for the input:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sitecore.Data.Items;
using Sitecore.Data.Fields;
using Sitecore;
using Sitecore.Diagnostics;

namespace MyNamespace
{
    class MyClass : Sitecore.Shell.Framework.Commands.Command
    {
        public override void Execute(Sitecore.Shell.Framework.Commands.CommandContext context)
        {
            if (context.Items.Length == 1)
            {
                Sitecore.Data.Items.Item item = context.Items[0];
                System.Collections.Specialized.NameValueCollection parameters = new System.Collections.Specialized.NameValueCollection();
                parameters["id"] = item.ID.ToString();
                parameters["language"] = item.Language.ToString();
                parameters["database"] = item.Database.Name;
                Sitecore.Context.ClientPage.Start(this, "Run", parameters);
            }
        }

        protected void Run(Sitecore.Web.UI.Sheer.ClientPipelineArgs args)
        {
            if (args.IsPostBack)
            {
                if ((!String.IsNullOrEmpty(args.Result)) && args.Result != "undefined" && args.Result != "null")
                {
                    Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase(args.Parameters["database"]);
                    Sitecore.Data.Items.Item myItem = db.GetItem(new Sitecore.Data.ID(args.Parameters["id"]));                        
                }
            }
            else
            {

                Sitecore.Context.ClientPage.ClientResponse.Input("My Message:"
                    , "My Default Value"
                    , Sitecore.Configuration.Settings.ItemNameValidation
                    , "'$Input' is not a valid name.", MaxLenght);
                args.WaitForPostBack();
            }
        }
    }
}

3 comments:

  1. This is an easy to use jQuery Plugin to integrate YouTube videos on your page by displaying a dialog box pop-up.

    Trade Show Flooring

    Pop Up Display

    Trade Show Displays

    ReplyDelete
  2. Impressed alot.I really like your blog.Thanks for the post. Pop up System Services in Malaysia

    ReplyDelete
  3. Impressed alot.I really like your blog.Thanks for the post. Pop up System Services in Malaysia

    ReplyDelete