0

We're currently using XM Cloud and we have a requirement to migrate media library from Sitecore to AEM (Adobe Experience Manager), so my approach is to leverage Sitecore.Shell.Applications.ContentEditor; and using Sitecore.Web.UI.Sheer. We managed to redirect user to assetpicker from AEM when they clicked on "Browse", but currently the AEM Dam landing is hardcoded and we want to make it dynamic (meaning user should be routed to specific folder based on their site name/property).

I have tried to get the current template path, id (from where the user click on Browse on Content Editor) in Sitecore.Context.Item; but its not helpful

Sitecore.Data.Items.Item myItem = Sitecore.Context.Item;
string language = myItem.Language.Name;
string parentId = myItem.Parent.ID.ToString();
string contextId = myItem.ID.ToString();

This is my function looks like

public override void HandleMessage(Message message)
{
    Assert.ArgumentNotNull(message, "message");
    Sitecore.Diagnostics.Log.Info("AEM Message","AEMMediaService");
    Sitecore.Diagnostics.Log.Info(message["id"],"AEMMediaService");
    base.HandleMessage(message);

    if (message["id"] != ID)
    {
        return;
    }

    switch (message.Name)
    {
        case "aemimage:add":
            Sitecore.Diagnostics.Log.Info("AEMImage Adding", "AEMMediaService");
            Sitecore.Data.Items.Item myItem = Sitecore.Context.Item;
            string language = myItem.Language.Name;
            string parentId = myItem.Parent.ID.ToString();
            string contextId = myItem.ID.ToString();
            Sitecore.Diagnostics.Log.Info(language,"AEMMediaService");
            Sitecore.Diagnostics.Log.Info(parentId,"AEMMediaService");
            Sitecore.Diagnostics.Log.Info(contextId,"AEMMediaService");

And this is how I pass the trigger the function and pass ID in the custom image field:

Value: "aemimage:add(id=$Target)"

Would love to receive any advice to help me solve this. Thank you so much

1

1 Answer 1

1

It is generally not recommended to make any .NET code customization while working on XM Cloud. In case it is necessary for you to show this feature on Content Editor browse button, you can use below code to fetch current item on the Image field.

On the codebeside for the custom image control, add the ItemID Field as below.

using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Shell.Applications.ContentEditor;
using Sitecore.Web.UI.Sheer;

public class CustomImage : Image
{
   public string ItemID
   {
      get
      {
         return this.GetViewStateString("ItemID");
      }
      set
      {
         Assert.ArgumentNotNull((object)value, nameof(value));
         this.SetViewStateString("ItemID", value);
      }
   }
   public override void HandleMessage(Message message)
   {
      string itemID = this.ItemID;
      if (!string.IsNullOrEmpty(itemID))
      {
         Item item = Sitecore.Client.ContentDatabase.GetItem(itemID);
         /* logic to interact with AEM */
      }
   }
}

Using this ItemID, context item is fetched inside HandleMessage function on which you are browsing image field.

Note: ItemID is set in view state so that it can be read inside the HandleMessage function.

Update:

You can use below generic method to get Site from the item.

public SiteInfo GetSiteInfo(Item item)
{
    var siteInfoList = Factory.GetSiteInfoList().Where(x => !string.IsNullOrEmpty(x.HostName))?.ToList();
    return siteInfoList?.FirstOrDefault(info => item.Paths.FullPath.ToLower().StartsWith(info.RootPath.ToLower()));
}

Hope this helps!!!

2
  • 1
    spot on! thanks so much. another question is if the Item path is like this /sitecore/content/ABC/XYZ/Presentation/AEMTest. So If I want to get the site name (in this case its XYZ) then I just need to split the string based on / right? Is there any better way to do it?
    – rayray
    Commented Jul 11 at 4:39
  • Glad to hear that @rayray....I have added the generic function above, you can use to get Site from the item...Hope this helps!!!
    – ckhanna
    Commented Jul 11 at 6:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.