Skip to content

Instantly share code, notes, and snippets.

@moo2u2 moo2u2/EditImage.cs
Created Sep 26, 2016

Embed
What would you like to do?
Sitecore command for opening the image editor app from Experience Editor
namespace SitecoreAndMore
{
using System;
using Sitecore;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Exceptions;
using Sitecore.Globalization;
using Sitecore.Shell.Applications.ContentEditor;
using Sitecore.Shell.Applications.WebEdit.Commands;
using Sitecore.Shell.Framework.Commands;
using Sitecore.Text;
using Sitecore.Web;
using Sitecore.Web.UI.Sheer;
[Serializable]
public class EditImage : WebEditImageCommand
{
public override void Execute(CommandContext context)
{
Assert.ArgumentNotNull(context, "context");
ExplodeParameters(context);
string formValue = WebUtil.GetFormValue("scPlainValue");
context.Parameters.Add("fieldValue", formValue);
Context.ClientPage.Start(this, "Run", context.Parameters);
}
protected static void Run(ClientPipelineArgs args)
{
Assert.ArgumentNotNull(args, "args");
Item itemNotNull = Client.GetItemNotNull(args.Parameters["itemid"], Language.Parse(args.Parameters["language"]));
itemNotNull.Fields.ReadAll();
Field field = itemNotNull.Fields[args.Parameters["fieldid"]];
Assert.IsNotNull(field, "field");
string xml = args.Parameters["fieldValue"];
if (!args.IsPostBack)
{
string str = StringUtil.GetString(new string[2] { field.Source, "/sitecore/media library" });
if (xml.Length > 0) {
xml = new XmlValue(xml, "image").GetAttribute("mediaid");
}
string path = xml;
if (str.StartsWith("~", StringComparison.InvariantCulture))
{
if (string.IsNullOrEmpty(path)) {
path = StringUtil.Mid(str, 1);
}
str = "/sitecore/media library";
}
Language language = itemNotNull.Language;
Item obj1 = Client.ContentDatabase.GetItem(str, language);
if (obj1 == null)
throw new ClientAlertException("The source of this Image field points to an item that does not exist.");
Item image = !string.IsNullOrEmpty(path) ? Client.ContentDatabase.GetItem(path, language) : null;
if (image == null)
throw new ClientAlertException("Please select an image before opening the edit function.");
UrlString urlString = new UrlString();
urlString.Append("sc_content", image.Database.Name);
urlString.Append("id", image.ID.ToString());
urlString.Append("la", image.Language.ToString());
urlString.Append("vs", image.Version.ToString());
if (!string.IsNullOrEmpty(args.Parameters["frameName"]))
urlString.Add("pfn", args.Parameters["frameName"]);
SheerResponse.ShowModalDialog("/sitecore/shell/Applications/Media/Imager?" + urlString, "1200px", "700px", string.Empty, IsDebugging);
args.WaitForPostBack();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.