Friday, November 4, 2022

Sitecore Branch template to create page with datasource structure

 The following code augments the functionality of Branch Templates by making any rendering data sources set in the layout on the branch that points to the newly created branch items.

This allows for templating including data source items using branches.


public class AddFromBranchPreset : AddFromTemplateProcessor

{

public override void Process(AddFromTemplateArgs args)

{

Assert.ArgumentNotNull(args, nameof(args));


if (AddFromTemplatePresetDisabler.IsActive)

{

return;

}


if (args.Destination.Database.Name != "master") return;


var templateItem = args.Destination.Database.GetItem(args.TemplateId);


Assert.IsNotNull(templateItem, "Template did not exist!");


// if this isn't a branch template, we can use the stock behavior

if (templateItem.TemplateID != TemplateIDs.BranchTemplate) return;


Assert.HasAccess((args.Destination.Access.CanCreate() ? 1 : 0) != 0, "AddFromTemplate - Add access required (destination: {0}, template: {1})", args.Destination.ID, args.TemplateId);


// Create the branch template instance

var newItem = args.Destination.Database.Engines.DataEngine.AddFromTemplate(args.ItemName, args.TemplateId, args.Destination, args.NewId);


// find all rendering data sources on the branch root item that points to an item under the branch template,

// and repoint them to the equivalent subitem under the branch instance

RewriteBranchRenderingDataSources(newItem, templateItem, newItem.Paths.FullPath);


args.Result = newItem;

}


protected virtual void RewriteBranchRenderingDataSources(Item item, BranchItem branchTemplateItem, string branchRoot)

{

var branchBasePath = branchTemplateItem.InnerItem.Paths.FullPath;


LayoutHelper.ApplyActionToAllRenderings(item, rendering =>

{

if (string.IsNullOrWhiteSpace(rendering.Datasource))

return RenderingActionResult.None;


// note: queries and multiple item datasources are not supported

var renderingTargetItem = item.Database.GetItem(rendering.Datasource);


if (renderingTargetItem == null)

Log.Warn("Error while expanding branch template rendering datasources: data source {0} was not resolvable.".FormatWith(rendering.Datasource), this);


// if there was no valid target item OR the target item is not a child of the branch template we skip out

if (renderingTargetItem == null || !renderingTargetItem.Paths.FullPath.StartsWith(branchBasePath, StringComparison.OrdinalIgnoreCase))

return RenderingActionResult.None;


var relativeRenderingPath = renderingTargetItem.Paths.FullPath.Substring(branchBasePath.Length).TrimStart('/');

relativeRenderingPath = relativeRenderingPath.Substring(relativeRenderingPath.IndexOf('/')); // we need to skip the "/$name" at the root of the branch children


var newTargetPath = item.Paths.FullPath + relativeRenderingPath;


var newTargetItem = item.Database.GetItem(newTargetPath);


// if the target item was valid under branch item, but the same relative path does not exist under the branch instance

// we set the datasource to something invalid to avoid any potential unintentional edits of a shared data source item

if (newTargetItem == null)

{

rendering.Datasource = "INVALID_BRANCH_SUBITEM_ID";

return RenderingActionResult.None;

}


rendering.Datasource = newTargetItem.ID.ToString();

return RenderingActionResult.None;

});


if (!item.HasChildren) return;

item.Children.ToList().ForEach(x => RewriteBranchRenderingDataSources(x, branchTemplateItem, branchRoot));

}

}

How to configure Remote debugger in Sitecore 10.2 docker containers

 We may face challenges when we debug the code in the docker container the very first time. Follow the below steps to enable the Remote debugger in Sitecore 10.2 docker containers.


1. .env file changes:

Make sure the Build_Configuration value is set to 'debug'

Also, set the path for REMOTEDEBUGGER_PATH.




2. Add the following configuration to the file - docker-compose.override.yml

 - ${REMOTEDEBUGGER_PATH}:C:\remote_debugger:ro
    # entrypoint: powershell -Command "& C:\tools\entrypoints\iis\Development.ps1"

    entrypoint: powershell -Command "& Write-Host 'Starting Debugger Service...'; & Start-Process -FilePath 'C:\remote_debugger\msvsmon.exe' -ArgumentList '/nostatus', '/silent', '/noauth', '/anyuser', '/nosecuritywarn', '/wow64port 4026'; & C:\tools\entrypoints\iis\Development.ps1"



3.  Run the below commands

   docker compose down
   docker-compose up -d --build
   docker-compose up -d


4. Open Powershell in administrator mode and run the below commands to see whether the 'msvsmon' process is present or not.


PS> Docker exec -it (Your CM container name) Powershell
PS> Get-Process



5) Hit the page or API in Chrome.

6) Open your solution in Visual Studio and then the containers window(View-->Other Windows-->Containers) and attach the debug points 

7) Right-click on the CM container on Container window-->Attach Process-->Check Show all processors-->Search w3wp and select-->Attach




8) Some Times you have to restart your IIS. Run the below command on Power Shell to Restart IIS on your docker instance. 

PS> Docker exec -it sitecore-xp0_cm_1 powershell
PS> Get-Process
PS> iisreset /stop if traffic container is unhealthy


                                                  
                                                     *Happy learning*

How to Create a Public Link Using the Sitecore Content Hub REST API

Creating a public link using the Sitecore Content Hub REST API is a straightforward process that enables you to share content externally whi...