Sitecore 9.2 (no SXA)
The code below is the Sitecore.Pipelines.GetRenderingDatasource.GetDatasourceLocation
pipeline. The issue I am having is that if I set the datasource field, in the rendering. It always just shows the children of the current page as the datasource for the rendering.
The main issue is that the args.ContextItemId
is set to the ID of the current page. This is what causes the issue. In my other Sitecore instances args.ContextItemId
is always null.
I have decompiled a lot of code and I can't understand what is setting the ContextItemId. Anyone seen this issue before?
public void Process(GetRenderingDatasourceArgs args)
{
Assert.IsNotNull((object)args, nameof(args));
foreach (string str in new ListString(args.RenderingItem["Datasource Location"]))
{
if (str.StartsWith("query:", StringComparison.InvariantCulture))
{
this.AddRootsFromQuery(str.Substring("query:".Length), args);
}
else
{
string path = str;
string contextItemPath = args.ContextItemPath;
string contextItemId = args.ContextItemId;
if (str.StartsWith("./", StringComparison.InvariantCulture) && !string.IsNullOrEmpty(args.ContextItemPath))
path = args.ContextItemPath + str.Remove(0, 1);
Item obj1 = args.ContentDatabase.GetItem(path);
if (obj1 != null)
{
if (ID.IsID(contextItemId) && obj1.ParentID.ToString() != contextItemId)
{
Item obj2 = args.ContentDatabase.GetItem(contextItemId);
if (obj2 != null)
{
foreach (Item child in obj2.Children)
{
if (child.ParentID.ToString() == contextItemId)
{
obj1 = args.ContentDatabase.GetItem(child.ID);
break;
}
}
}
}
args.DatasourceRoots.Add(obj1);
}
}
}
}
UPDATE:
I over road the webedit:addrendering
command and args.Parameters["contextitemid"]
has data. This is why the pipeline has it set. Now why does ClientPipelineArgs have args.Parameters["contextitemid"]
set.
GetRenderingDatasourceArgs renderingDatasourceArgs = new GetRenderingDatasourceArgs(itemNotNull)
{
ContextItemPath = args.Parameters["contextitempath"],
ContextItemId = args.Parameters["contextitemid"],
ContentLanguage = WebEditUtil.GetClientContentLanguage()
};
Update 2:
I think this might be a Sitecore issue. This use the 1st child node is new in 9.1 and 9.2
8.2 & 9.1 public class GetDatasourceLocation
{
/// <summary>Runs the processor.</summary>
/// <param name="args">The arguments.</param>
public void Process(GetRenderingDatasourceArgs args)
{
Assert.IsNotNull((object) args, nameof(args));
foreach (string str in new ListString(args.RenderingItem["Datasource Location"]))
{
if (str.StartsWith("query:", StringComparison.InvariantCulture))
{
this.AddRootsFromQuery(str.Substring("query:".Length), args);
}
else
{
string path = str;
if (str.StartsWith("./", StringComparison.InvariantCulture) &&
!string.IsNullOrEmpty(args.ContextItemPath))
path = args.ContextItemPath + str.Remove(0, 1);
Item obj = args.ContentDatabase.GetItem(path);
if (obj != null)
args.DatasourceRoots.Add(obj);
}
}
}
}
9.2 & 9.3
public void Process(GetRenderingDatasourceArgs args)
{
Assert.IsNotNull((object) args, nameof (args));
foreach (string str in new ListString(args.RenderingItem["Datasource Location"]))
{
if (str.StartsWith("query:", StringComparison.InvariantCulture))
{
this.AddRootsFromQuery(str.Substring("query:".Length), args);
}
else
{
string path = str;
string contextItemPath = args.ContextItemPath;
string contextItemId = args.ContextItemId;
if (str.StartsWith("./", StringComparison.InvariantCulture) && !string.IsNullOrEmpty(args.ContextItemPath))
path = args.ContextItemPath + str.Remove(0, 1);
Item obj1 = args.ContentDatabase.GetItem(path);
if (obj1 != null)
{
if (ID.IsID(contextItemId) && obj1.ParentID.ToString() != contextItemId)
{
Item obj2 = args.ContentDatabase.GetItem(contextItemId);
if (obj2 != null)
{
foreach (Item child in obj2.Children)
{
if (child.ParentID.ToString() == contextItemId)
{
obj1 = args.ContentDatabase.GetItem(child.ID);
break;
}
}
}
}
args.DatasourceRoots.Add(obj1);
}
}
}
}