hierarchical dynamic content not relating

Posted by Community Admin on 04-Aug-2018 15:50

hierarchical dynamic content not relating

All Replies

Posted by Community Admin on 31-Jan-2014 00:00

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;
 

Posted by Community Admin on 05-Feb-2014 00:00

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;

It should create a subaccount item in the first client item you have. If this appears to be correct, then the mistake is somewhere in:
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;
 

Either the account code does not match the parent`s code or probably there are several account codes that match this LINQ criteria and by taking first, it does not match your desired parent.

Of course there could be other reasons for not passing the correct parent guid, but those are some of the possibilities that did cross my mind.

I hope this information helps.

Regards,
Vassil Vassilev
Telerik
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items

Posted by Community Admin on 08-Oct-2015 00:00

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? 

This thread is closed