namespace Common.CustomSitecore.DataExchange.Repositories
{
public class CustomInProcItemModelRepository : Sitecore.DataExchange.Local.Repositories.InProcItemModelRepository
{
protected override Guid CreateItem(ItemModel itemModel)
{
if (itemModel == null || !itemModel.ContainsKey("ParentID"))
return Guid.Empty;
var result1 = Guid.Empty;
var obj1 = itemModel["ParentID"];
if (!Guid.TryParse(obj1?.ToString(), out result1) || result1 == Guid.Empty)
return Guid.Empty;
var obj2 = itemModel["TemplateID"];
Guid result2;
if (!Guid.TryParse(obj2?.ToString(), out result2) || result2 == Guid.Empty)
return Guid.Empty;
itemModel["TemplateID"] = (object)result2.ToString();
if (string.IsNullOrWhiteSpace((string)itemModel["ItemName"]))
return Guid.Empty;
var itemModel1 = Get(result1, (string)null, 0);
if (itemModel1 == null)
return Guid.Empty;
string empty = string.Empty;
if (itemModel.ContainsKey("ItemLanguage") && itemModel["ItemLanguage"] != null)
empty = itemModel["ItemLanguage"].ToString();
itemModel.Remove("ParentID");
itemModel.Remove("ItemLanguage");
var createdItemModel = this.CreateItemModelWithStringFieldValues(itemModel);
//custom check to get the template to add's template. We will later check if
//this is a normal template or a branch template
Guid templateOrBranchId;
ItemModel templateOrBranch = null;
if (Guid.TryParse(createdItemModel?["TemplateID"]?.ToString(), out templateOrBranchId))
{
templateOrBranch = Get(templateOrBranchId);
}
//custom
check to see if we are trying to add //if a branch template add via the branch template add and update otherwise do the default
Guid responseId = Guid.Empty;
if (createdItemModel != null && templateOrBranch?["TemplateID"]?.ToString() == Sitecore.TemplateIDs.BranchTemplate.Guid.ToString())
{
var db = Factory.GetDatabase(DatabaseName);
Language branchLanguage;
var parentvalidLanguage = db.GetItem(itemModel1["ItemPath"]Language.ToStringTryParse()empty, out branchLanguage);
var branch = validLanguage ? db.GetItem(BranchItemnew ID(templateOrBranchId), branchLanguage)
: db.GetItem(new ID(templateOrBranchId));
var parent = validLanguage ? db.GetItem(itemModel1["ItemPath"].ToString(), branchLanguage)
: db.GetItem(itemModel1["ItemPath"].ToString());
var newItem = parent.Add(createdItemModel["ItemName"].ToString(), (BranchItem)branch);
if (newItem == null)
{
Log.Error("Could not create item", this);
}
else
{
//Assuming branch's main item is $name, otherwise use the first that can be found
var mainItem = branch.Children["$name"] ?? branch.Children.FirstOrDefault();
if (mainItem == null)
{
Context.Logger.Error($"Could not find a main template in a branch item: {branch.ID}");
}
else
{
createdItemModel[ItemModel.TemplateID] = mainItem.TemplateID;
Update(newItem.ID.Guid, createdItemModel, newItem.Language.Name, newItem.Version.Number);
}
responseId = newItem.ID.Guid;
}
}
else
{
//this part is not custom. It is the default functionality that adds from a template
responseId = (this.HandlerProvider.GetHandler<CreateItemHandler>().Handle(
(HandlerRequest) new CreateItemCommand()
{
Database = this.DatabaseName,
ItemModel = createdItemModel,
Path = itemModel1["ItemPath"].ToString(),
Language = empty
}) as CreateItemResponse).ItemId;
}
return responseId;
}
}
}
namespace Common.CustomSitecore.DataExchange.Repositories
{
public class CustomInProcItemModelRepository : Sitecore.DataExchange.Local.Repositories.InProcItemModelRepository
{
protected override Guid CreateItem(ItemModel itemModel)
{
if (itemModel == null || !itemModel.ContainsKey("ParentID"))
return Guid.Empty;
var result1 = Guid.Empty;
var obj1 = itemModel["ParentID"];
if (!Guid.TryParse(obj1?.ToString(), out result1) || result1 == Guid.Empty)
return Guid.Empty;
var obj2 = itemModel["TemplateID"];
Guid result2;
if (!Guid.TryParse(obj2?.ToString(), out result2) || result2 == Guid.Empty)
return Guid.Empty;
itemModel["TemplateID"] = (object)result2.ToString();
if (string.IsNullOrWhiteSpace((string)itemModel["ItemName"]))
return Guid.Empty;
var itemModel1 = Get(result1, (string)null, 0);
if (itemModel1 == null)
return Guid.Empty;
string empty = string.Empty;
if (itemModel.ContainsKey("ItemLanguage") && itemModel["ItemLanguage"] != null)
empty = itemModel["ItemLanguage"].ToString();
itemModel.Remove("ParentID");
itemModel.Remove("ItemLanguage");
var createdItemModel = this.CreateItemModelWithStringFieldValues(itemModel);
//custom check to get the template to add's template. We will later check if
//this is a normal template or a branch template
Guid templateOrBranchId;
ItemModel templateOrBranch = null;
if (Guid.TryParse(createdItemModel?["TemplateID"]?.ToString(), out templateOrBranchId))
{
templateOrBranch = Get(templateOrBranchId);
}
//custom check to see if we are trying to add a branch template otherwise do the default
Guid responseId = Guid.Empty;
if (createdItemModel != null && templateOrBranch?["TemplateID"]?.ToString() == Sitecore.TemplateIDs.BranchTemplate.Guid.ToString())
{
var db = Factory.GetDatabase(DatabaseName);
var parent = db.GetItem(itemModel1["ItemPath"].ToString());
var branch = (BranchItem)db.GetItem(new ID(templateOrBranchId));
var newItem = parent.Add(createdItemModel["ItemName"].ToString(), branch);
if (newItem == null)
{
Log.Error("Could not create item", this);
}
else
{
responseId = newItem.ID.Guid;
}
}
else
{
//this part is not custom. It is the default functionality that adds from a template
responseId = (this.HandlerProvider.GetHandler<CreateItemHandler>().Handle(
(HandlerRequest) new CreateItemCommand()
{
Database = this.DatabaseName,
ItemModel = createdItemModel,
Path = itemModel1["ItemPath"].ToString(),
Language = empty
}) as CreateItemResponse).ItemId;
}
return responseId;
}
}
}
namespace Common.CustomSitecore.DataExchange.Repositories
{
public class CustomInProcItemModelRepository : Sitecore.DataExchange.Local.Repositories.InProcItemModelRepository
{
protected override Guid CreateItem(ItemModel itemModel)
{
if (itemModel == null || !itemModel.ContainsKey("ParentID"))
return Guid.Empty;
var result1 = Guid.Empty;
var obj1 = itemModel["ParentID"];
if (!Guid.TryParse(obj1?.ToString(), out result1) || result1 == Guid.Empty)
return Guid.Empty;
var obj2 = itemModel["TemplateID"];
Guid result2;
if (!Guid.TryParse(obj2?.ToString(), out result2) || result2 == Guid.Empty)
return Guid.Empty;
itemModel["TemplateID"] = (object)result2.ToString();
if (string.IsNullOrWhiteSpace((string)itemModel["ItemName"]))
return Guid.Empty;
var itemModel1 = Get(result1, (string)null, 0);
if (itemModel1 == null)
return Guid.Empty;
string empty = string.Empty;
if (itemModel.ContainsKey("ItemLanguage") && itemModel["ItemLanguage"] != null)
empty = itemModel["ItemLanguage"].ToString();
itemModel.Remove("ParentID");
itemModel.Remove("ItemLanguage");
var createdItemModel = this.CreateItemModelWithStringFieldValues(itemModel);
//custom check to get the template to add's template. We will later check if
//this is a normal template or a branch template
Guid templateOrBranchId;
ItemModel templateOrBranch = null;
if (Guid.TryParse(createdItemModel?["TemplateID"]?.ToString(), out templateOrBranchId))
{
templateOrBranch = Get(templateOrBranchId);
}
//if a branch template add via the branch template add and update otherwise do the default
Guid responseId = Guid.Empty;
if (createdItemModel != null && templateOrBranch?["TemplateID"]?.ToString() == Sitecore.TemplateIDs.BranchTemplate.Guid.ToString())
{
var db = Factory.GetDatabase(DatabaseName);
Language branchLanguage;
var validLanguage = Language.TryParse(empty, out branchLanguage);
var branch = validLanguage ? db.GetItem(new ID(templateOrBranchId), branchLanguage)
: db.GetItem(new ID(templateOrBranchId));
var parent = validLanguage ? db.GetItem(itemModel1["ItemPath"].ToString(), branchLanguage)
: db.GetItem(itemModel1["ItemPath"].ToString());
var newItem = parent.Add(createdItemModel["ItemName"].ToString(), (BranchItem)branch);
if (newItem == null)
{
Log.Error("Could not create item", this);
}
else
{
//Assuming branch's main item is $name, otherwise use the first that can be found
var mainItem = branch.Children["$name"] ?? branch.Children.FirstOrDefault();
if (mainItem == null)
{
Context.Logger.Error($"Could not find a main template in a branch item: {branch.ID}");
}
else
{
createdItemModel[ItemModel.TemplateID] = mainItem.TemplateID;
Update(newItem.ID.Guid, createdItemModel, newItem.Language.Name, newItem.Version.Number);
}
responseId = newItem.ID.Guid;
}
}
else
{
//this part is not custom. It is the default functionality that adds from a template
responseId = (this.HandlerProvider.GetHandler<CreateItemHandler>().Handle(
(HandlerRequest) new CreateItemCommand()
{
Database = this.DatabaseName,
ItemModel = createdItemModel,
Path = itemModel1["ItemPath"].ToString(),
Language = empty
}) as CreateItemResponse).ItemId;
}
return responseId;
}
}
}
namespace Common.CustomSitecore.DataExchange.Repositories
{
public class CustomInProcItemModelRepository : Sitecore.DataExchange.Local.Repositories.InProcItemModelRepository
{
protected override Guid CreateItem(ItemModel itemModel)
{
if (itemModel == null || !itemModel.ContainsKey("ParentID"))
return Guid.Empty;
var result1 = Guid.Empty;
var obj1 = itemModel["ParentID"];
if (!Guid.TryParse(obj1?.ToString(), out result1) || result1 == Guid.Empty)
return Guid.Empty;
var obj2 = itemModel["TemplateID"];
Guid result2;
if (!Guid.TryParse(obj2?.ToString(), out result2) || result2 == Guid.Empty)
return Guid.Empty;
itemModel["TemplateID"] = (object)result2.ToString();
if (string.IsNullOrWhiteSpace((string)itemModel["ItemName"]))
return Guid.Empty;
var itemModel1 = Get(result1, (string)null, 0);
if (itemModel1 == null)
return Guid.Empty;
string empty = string.Empty;
if (itemModel.ContainsKey("ItemLanguage") && itemModel["ItemLanguage"] != null)
empty = itemModel["ItemLanguage"].ToString();
itemModel.Remove("ParentID");
itemModel.Remove("ItemLanguage");
var createdItemModel = this.CreateItemModelWithStringFieldValues(itemModel);
//custom check to get the template to add's template. We will later check if
//this is a normal template or a branch template
Guid templateOrBranchId;
ItemModel templateOrBranch = null;
if (Guid.TryParse(createdItemModel?["TemplateID"]?.ToString(), out templateOrBranchId))
{
templateOrBranch = Get(templateOrBranchId);
}
//custom check to see if we are trying to add a branch template otherwise do the default
Guid responseId = Guid.Empty;
if (createdItemModel != null && templateOrBranch?["TemplateID"]?.ToString() == Sitecore.TemplateIDs.BranchTemplate.Guid.ToString())
{
var db = Factory.GetDatabase(DatabaseName);
var parent = db.GetItem(itemModel1["ItemPath"].ToString());
var branch = (BranchItem)db.GetItem(new ID(templateOrBranchId));
var newItem = parent.Add(createdItemModel["ItemName"].ToString(), branch);
if (newItem == null)
{
Log.Error("Could not create item", this);
}
else
{
responseId = newItem.ID.Guid;
}
}
else
{
//this part is not custom. It is the default functionality that adds from a template
responseId = (this.HandlerProvider.GetHandler<CreateItemHandler>().Handle(
(HandlerRequest) new CreateItemCommand()
{
Database = this.DatabaseName,
ItemModel = createdItemModel,
Path = itemModel1["ItemPath"].ToString(),
Language = empty
}) as CreateItemResponse).ItemId;
}
return responseId;
}
}
}
namespace Common.CustomSitecore.DataExchange.Repositories
{
public class CustomInProcItemModelRepository : Sitecore.DataExchange.Local.Repositories.InProcItemModelRepository
{
protected override Guid CreateItem(ItemModel itemModel)
{
if (itemModel == null || !itemModel.ContainsKey("ParentID"))
return Guid.Empty;
var result1 = Guid.Empty;
var obj1 = itemModel["ParentID"];
if (!Guid.TryParse(obj1?.ToString(), out result1) || result1 == Guid.Empty)
return Guid.Empty;
var obj2 = itemModel["TemplateID"];
Guid result2;
if (!Guid.TryParse(obj2?.ToString(), out result2) || result2 == Guid.Empty)
return Guid.Empty;
itemModel["TemplateID"] = (object)result2.ToString();
if (string.IsNullOrWhiteSpace((string)itemModel["ItemName"]))
return Guid.Empty;
var itemModel1 = Get(result1, (string)null, 0);
if (itemModel1 == null)
return Guid.Empty;
string empty = string.Empty;
if (itemModel.ContainsKey("ItemLanguage") && itemModel["ItemLanguage"] != null)
empty = itemModel["ItemLanguage"].ToString();
itemModel.Remove("ParentID");
itemModel.Remove("ItemLanguage");
var createdItemModel = this.CreateItemModelWithStringFieldValues(itemModel);
//custom check to get the template to add's template. We will later check if
//this is a normal template or a branch template
Guid templateOrBranchId;
ItemModel templateOrBranch = null;
if (Guid.TryParse(createdItemModel?["TemplateID"]?.ToString(), out templateOrBranchId))
{
templateOrBranch = Get(templateOrBranchId);
}
//custom check to see if we are trying to add a branch template otherwise do the default
Guid responseId = Guid.Empty;
if (createdItemModel != null && templateOrBranch?["TemplateID"]?.ToString() == Sitecore.TemplateIDs.BranchTemplate.Guid.ToString())
{
var db = Factory.GetDatabase(DatabaseName);
var parent = db.GetItem(itemModel1["ItemPath"].ToString());
var branch = (BranchItem)db.GetItem(new ID(templateOrBranchId));
var newItem = parent.Add(createdItemModel["ItemName"].ToString(), branch);
if (newItem == null)
{
Log.Error("Could not create item", this);
}
else
{
responseId = newItem.ID.Guid;
}
}
else
{
responseId = (this.HandlerProvider.GetHandler<CreateItemHandler>().Handle(
(HandlerRequest) new CreateItemCommand()
{
Database = this.DatabaseName,
ItemModel = createdItemModel,
Path = itemModel1["ItemPath"].ToString(),
Language = empty
}) as CreateItemResponse).ItemId;
}
return responseId;
}
}
}
namespace Common.CustomSitecore.DataExchange.Repositories
{
public class CustomInProcItemModelRepository : Sitecore.DataExchange.Local.Repositories.InProcItemModelRepository
{
protected override Guid CreateItem(ItemModel itemModel)
{
if (itemModel == null || !itemModel.ContainsKey("ParentID"))
return Guid.Empty;
var result1 = Guid.Empty;
var obj1 = itemModel["ParentID"];
if (!Guid.TryParse(obj1?.ToString(), out result1) || result1 == Guid.Empty)
return Guid.Empty;
var obj2 = itemModel["TemplateID"];
Guid result2;
if (!Guid.TryParse(obj2?.ToString(), out result2) || result2 == Guid.Empty)
return Guid.Empty;
itemModel["TemplateID"] = (object)result2.ToString();
if (string.IsNullOrWhiteSpace((string)itemModel["ItemName"]))
return Guid.Empty;
var itemModel1 = Get(result1, (string)null, 0);
if (itemModel1 == null)
return Guid.Empty;
string empty = string.Empty;
if (itemModel.ContainsKey("ItemLanguage") && itemModel["ItemLanguage"] != null)
empty = itemModel["ItemLanguage"].ToString();
itemModel.Remove("ParentID");
itemModel.Remove("ItemLanguage");
var createdItemModel = this.CreateItemModelWithStringFieldValues(itemModel);
//custom check to get the template to add's template. We will later check if
//this is a normal template or a branch template
Guid templateOrBranchId;
ItemModel templateOrBranch = null;
if (Guid.TryParse(createdItemModel?["TemplateID"]?.ToString(), out templateOrBranchId))
{
templateOrBranch = Get(templateOrBranchId);
}
//custom check to see if we are trying to add a branch template otherwise do the default
Guid responseId = Guid.Empty;
if (createdItemModel != null && templateOrBranch?["TemplateID"]?.ToString() == Sitecore.TemplateIDs.BranchTemplate.Guid.ToString())
{
var db = Factory.GetDatabase(DatabaseName);
var parent = db.GetItem(itemModel1["ItemPath"].ToString());
var branch = (BranchItem)db.GetItem(new ID(templateOrBranchId));
var newItem = parent.Add(createdItemModel["ItemName"].ToString(), branch);
if (newItem == null)
{
Log.Error("Could not create item", this);
}
else
{
responseId = newItem.ID.Guid;
}
}
else
{
//this part is not custom. It is the default functionality that adds from a template
responseId = (this.HandlerProvider.GetHandler<CreateItemHandler>().Handle(
(HandlerRequest) new CreateItemCommand()
{
Database = this.DatabaseName,
ItemModel = createdItemModel,
Path = itemModel1["ItemPath"].ToString(),
Language = empty
}) as CreateItemResponse).ItemId;
}
return responseId;
}
}
}
I didn't see an Add
method in the InProcItemModelRepository
when overriding the class mentioned above (overriding Add
is mentioned in the linked question). I overrode the CreateItem
method to have the default code with a few checks marked with comments as custom code as follows:
I potentially could have tried to override the default config entry for my instance shown herein the following config snippet. Don't make this config change if you are using the code from above (CustomInProcItemModelRepository
) as it is not directly related. If using CustomInProcItemModelRepository
patch the config mentioned in Vlad's post:
I didn't see an Add
method in the InProcItemModelRepository
when overriding the class mentioned above. I overrode the CreateItem
method to have the default code with a few checks marked with comments as custom code as follows:
I potentially could have tried to override the default config entry for my instance shown here:
I didn't see an Add
method in the InProcItemModelRepository
when overriding the class mentioned above (overriding Add
is mentioned in the linked question). I overrode the CreateItem
method to have the default code with a few checks marked with comments as custom code as follows:
I potentially could have tried to override the default config entry for my instance shown in the following config snippet. Don't make this config change if you are using the code from above (CustomInProcItemModelRepository
) as it is not directly related. If using CustomInProcItemModelRepository
patch the config mentioned in Vlad's post: