hierarchical dynamic content not relating
Using module builder, I created dynamic content Clients (parent) with a child type of Subaccounts. I am trying to use the code below. I already have all the Clients in the module builder. I am importing from an excel spreadsheet all the subaccounts. I am attempting to create the subaccount type, find the parent id, then relate the two. If I go into the backend, It is publishing the subaccount items (visible in the dashboard), but not relating them to their parent item.
protected
void
Button1_Click1(
object
sender, EventArgs e)
Type clientType = TypeResolutionService.ResolveType(
"Telerik.Sitefinity.DynamicTypes.Model.Clients.Client"
);
Type subaccountType = TypeResolutionService.ResolveType(
"Telerik.Sitefinity.DynamicTypes.Model.Clients.Subaccount"
);
var providerName =
"OpenAccessProvider"
;
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
foreach
(DataRow row
in
ClientInfo.Rows)
var myFilteredCollection = dynamicModuleManager.GetDataItems(subaccountType).Any(p => p.GetValue<
string
>(
"SubAccountCode"
) == row[
"Subcode"
].ToString());
if
(!myFilteredCollection)
DynamicContent subaccountItem = dynamicModuleManager.CreateDataItem(subaccountType);
// This is how values for the properties are set
subaccountItem.SetValue(
"SubAccountCode"
, row[
"Subcode"
]);
subaccountItem.SetValue(
"Title"
, row[
"Subname"
]);
subaccountItem.SetString(
"UrlName"
, Regex.Replace(row[
"Subname"
].ToString().ToLower(), @
"[^\w\-\!\$\'\(\)\=\@\d_]+"
,
"-"
));
subaccountItem.SetValue(
"Owner"
, SecurityManager.GetCurrentUserId());
subaccountItem.SetValue(
"PublicationDate"
, DateTime.Now);
// Set item parent
Guid parentId = GetParentId(dynamicModuleManager, clientType, row[
"Code"
].ToString());
subaccountItem.SetParent(parentId, clientType.FullName);
// You need to call SaveChanges() in order for the items to be actually persisted to data store
if
(parentId !=
null
)
dynamicModuleManager.Lifecycle.Publish(subaccountItem);
subaccountItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName,
"Published"
);
dynamicModuleManager.SaveChanges();
else
lblmsg.Text = String.Format(
"Error importing 0"
, row[
"Subcode"
]);
private
Guid GetParentId(DynamicModuleManager dynamicModuleManager, Type parentType, String AccountCode)
DynamicContent parent = dynamicModuleManager.GetDataItems(parentType)
.Where(i => i.Status == ContentLifecycleStatus.Master && i.GetValue<
string
>(
"AccountCode"
) == AccountCode).First();
return
parent.Id;
Hello Amanda,
As the subaccount items are created without errors and as your code is almost identical to the one offered by Sitefinity`s code reference, the most likely reason for this unexpected behaviour could be GetParentID method, which most likely provides wrong parent guid.
You can test this by modifying the code as follows:
private
Guid GetParentId(DynamicModuleManager dynamicModuleManager, Type parentType)
DynamicContent parent = dynamicModuleManager.GetDataItems(parentType)
.Where(i=>i.Status == ContentLifecycleStatus.Master).First();
return
parent.Id;
private
Guid GetParentId(DynamicModuleManager dynamicModuleManager, Type parentType, String AccountCode)
DynamicContent parent = dynamicModuleManager.GetDataItems(parentType)
.Where(i => i.Status == ContentLifecycleStatus.Master && i.GetValue<
string
>(
"AccountCode"
) == AccountCode).First();
return
parent.Id;
I have manually created the parent dynamic content and in my widget i use a dropdown to select the parent content and upload the CSV to create the data a child of that particular Parent.
Please find the code i have used for creating dynamic content through API's.
public void CreateChildContentFromCSV(Record csvDataObj,Guid parentId)
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(Constants.ConstantVariables.ProviderName);
DynamicContent dataContentItem = dynamicModuleManager.CreateDataItem(Constants.ModuleTypes.dataContentItemType);
// Set item parent
Type admissionType = Constants.ModuleTypes.admissionType;
dataContentItem .SetParent(parentId, admissionType.FullName);
// This is how values for the properties are set
dataContentItem .SetValue("Title", licensure.LicensureTitle);
licensureItem.SetValue("Description", licensure.Description);
string UrlName = Regex.Replace(licensure.LicensureTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
licensureItem.SetString("UrlName", UrlName);
licensureItem.SetValue("Owner", SecurityManager.GetCurrentUserId());
licensureItem.SetValue("PublicationDate", DateTime.Now.ToUniversalTime());
ILifecycleDataItem publishedItem = dynamicModuleManager.Lifecycle.Publish(dataContentItem);
licensureItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Published");
dynamicModuleManager.SaveChanges();
I bind all parent items to a dropdown and get the id of the selected parent using the following method :
public IEnumerable<DynamicContent> GetAllAdmissions()
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(Constants.ConstantVariables.ProviderName);
dynamicModuleManager.Provider.SuppressSecurityChecks = true;
var admissions = dynamicModuleManager.GetDataItems(Constants.ModuleTypes.admissionType).Where(i => i.Visible == true && i.Status == ContentLifecycleStatus.Live);
return admissions;
I am getting the content created but only the parent is not set. When i set it manually it works. Can somebody help me figure out what am i doing wrong?