Thursday, May 9, 2024

How to Use External Images in Image Fields with Sitecore's Page Editor


Recently, while working on a Sitecore project that involved integrating Celum DAM (Digital Asset Management), I encountered a requirement to support external images within Sitecore's Image fields in the Page Editor.

The objective was to enable content authors to:

  1. Add external image URLs directly (e.g., public links from Celum).
  2. Ensure a seamless preview experience within the Page Editor, including dynamic components such as image carousels.

This approach allowed the use of Celum-hosted images while maintaining the intuitive Sitecore authoring experience.

This article outlines the steps, challenges, and solutions for implementing this feature.

The Requirement

The requirement was straightforward:

  1. Source Images Externally: Authors needed to add images via external URLs rather than uploading them to the Sitecore Media Library.
  2. Preview in Page Editor: The images, whether single or in a carousel, had to be visible within the Page Editor for a better authoring experience.

The Challenges

  1. Default Behavior of Image Fields: Sitecore's out-of-the-box (OOTB) Image field retrieves images from the Media Library, not external sources.
  2. Page Editor Compatibility: External image links needed to render correctly in the Page Editor, ensuring authors could see real-time previews.
  3. Handling Multiple Images: For components like carousels, the solution had to handle multiple external image URLs effectively.

Proposed Solutions

1. Using a General Link Field

  • One suggested approach was to use a General Link field instead of an Image field.
  • Authors could paste the external URL into the General Link field, and the rendering component would use this URL to display the image in an <img> tag.

Implementation:


<img src="@Model.Item["ExternalImageLinkField"]" alt="Carousel Image" />

While this approach worked for basic cases, it lacked the functionality of the OOTB Image field, such as alt text management or built-in media selection.

2. Cus
tomizing the Image Field
To provide the desired experience, I created a custom solution:

  • Step 1: Extend the Image Field
    Create a custom field type that allows authors to input external URLs or select an image from the Media Library.

  • Step 2: Update the Rendering Logic
    Modify the component's logic to handle both Media Library images and external URLs:

    csharp

    string imageUrl = item.Fields["ImageField"].Value; if (!imageUrl.StartsWith("http")) { imageUrl = MediaManager.GetMediaUrl(item.Fields["ImageField"].MediaItem); }
  • Step 3: Preview in Page Editor
    Use Sitecore's rendering parameters to enable real-time previews of external images in the Page Editor.

3. Custom Carousel Rendering For handling multiple external images in a carousel:

  • Use a Multilist field or a JSON field to store multiple external URLs.
  • The rendering component processes these URLs dynamically to generate the carousel.

Example rendering logic:

javascript

const images = @Html.Sitecore().Field("CarouselImageLinks", Model.Item); images.split(',').forEach(imageUrl => { <img src="@imageUrl" alt="Carousel Image" /> });

Key Takeaways

  1. Customization is Key: While Sitecore provides robust OOTB functionality, customizations are often necessary to meet specific requirements.
  2. Maintain Authoring Simplicity: Ensure that any solution integrates smoothly with the Page Editor, providing an intuitive authoring experience.
  3. Consider Scalability: Solutions should support single images, multiple images, and future enhancements with minimal rework.

This approach successfully enabled the use of external image URLs in Sitecore's Image fields, ensuring both functionality and usability for authors. By sharing this solution, I hope it helps others facing similar challenges. If you have additional insights or questions, feel free to reach out!

Wednesday, May 8, 2024

How to Revert Item Source from Database to Resources in Sitecore XM Cloud After Template Modification


In Sitecore XM Cloud, templates and items can be stored as resources, ensuring that they remain synchronized with deployments. However, modifying a template directly in the content editor changes its source from "resources" to "database." This presents challenges if you need to revert the source back to "resources," especially with multiple content items linked to the affected template.

Here’s how to revert the item source effectively:

Scenario

  • Problem: A template source was accidentally changed from "resources" to "database."
  • Goal: Revert the template source back to "resources" without disrupting existing content items or performing manual workarounds, like deleting and recreating templates.

Solution: Cleanup Command

Sitecore CLI provides a cleanup command to revert the item source back to resources. This method ensures synchronization during the next deployment.

  1. Run Cleanup Command Use the following command in the Sitecore CLI:


    dotnet sitecore itemres cleanup --path "{{itempath}}" -f -r
    • --path: Specify the path of the item or template to clean up.
    • -f: Forces the cleanup without additional prompts.
    • -r: Recursively applies cleanup to all child items within the path.

    Example:


    dotnet sitecore itemres cleanup --path "/sitecore/templates/Sample Template" -f -r
  2. What It Does

    • Removes database entries for items that exist in the Item-as-Resource (IAR) repository.
    • Reverts items to their original state in the resources folder.
  3. Validation

    • After running the command, verify the source by enabling "Source View" from the View menu in the content editor.
    • The source should now show "resources" instead of "database."

Alternative Approach

If only a few items are affected:

  • Delete the Database Entries:
    • Simply delete the affected items/templates in the content tree.
    • The system will revert to using the resources version of the item during the next deployment.

Considerations

  1. Backup Content:
    Before running cleanup commands or deleting items, ensure content backups are in place to avoid data loss.

  2. Use CLI for Bulk Operations:
    For larger trees, the CLI is the most efficient way to clean up multiple items.

  3. Official Documentation:
    Refer to the official documentation for more details on the cleanup command:
    Sitecore CLI Itemres Cleanup

Conclusion

The dotnet sitecore itemres cleanup command is the ideal solution to revert an item source from "database" to "resources" in a multi-item scenario. For isolated cases, manual deletion of the database entry may suffice. These approaches ensure minimal disruption and alignment with the deployment pipeline.

Thursday, May 2, 2024

How to cleanup the resource file


 Run below command to setup Extensibility

 dotnet sitecore plugin add -n Sitecore.DevEx.Extensibility.ResourcePackage --version 5.2.113

If you find any issue check your latest installed packages

dotnet sitecore plugin list
Cleanup command
dotnet sitecore itemres cleanup --what-if


Other Commands
dotnet sitecore ser push -n "Local"   
npm run start:connected
npm run lint -- --fix 

More details - https://doc.sitecore.com/xp/en/developers/104/developer-tools/the-cli-login-command.html#options