+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / hierarchical dynamic content not relating

hierarchical dynamic content not relating

3 posts, 0 answered
  1. Amanda Shafer
    Amanda Shafer avatar
    198 posts
    Registered:
    12 Oct 2012
    31 Jan 2014
    Link to this post
    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;
     }
  2. Vassil Vassilev
     Vassil Vassilev avatar
    308 posts
    Registered:
    21 Jan 2015
    05 Feb 2014
    Link to this post
    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
  3. Anju
    Anju avatar
    19 posts
    Registered:
    22 Jul 2014
    08 Oct 2015
    Link to this post

    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? 

3 posts, 0 answered