More in this section

Forums / Developing with Sitefinity / Import data and publish?

Import data and publish?

4 posts, 0 answered
  1. Jules
    Jules avatar
    65 posts
    Registered:
    20 Jul 2005
    21 Apr 2012
    Link to this post
    Is there a way to import data into my module built with the module builder and have it come in published?

    Here's a snippet of my code that executes for each item. 
    DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
    Type communityResourceType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.CommunityResources.CommunityResource");
    DynamicContent communityResourceItem = dynamicModuleManager.CreateDataItem(communityResourceType);
     
    // code that sets values for each item
     
    dynamicModuleManager.SaveChanges();

    I'm importing several hundred items and manually publishing... I'd like a better alternative.
  2. Lilia Messechkova
    Lilia Messechkova avatar
    42 posts
    Registered:
    13 Jul 2016
    23 Apr 2012
    Link to this post
    Hi Julia,

    You need to add a few more lines of code to your method in order to publish your items programatically: Here they are:

    //This is the additional line that needs to be executed
    // set the workflow UI status
    InitializeApprovalTrackingRecordMap((IApprovalWorkflowItem)communityResourceType, dynamicModuleManager.Provider.ApplicationName, "Published");
     
    // We can now call the following to publish the item
    dynamicModuleManager.Lifecycle.Publish(communityResourceType);
     
    // You need to call SaveChanges() in order for the items to be actually persisted to data store
    dynamicModuleManager.SaveChanges();


    Here is the new method definiton:


    public static void InitializeApprovalTrackingRecordMap(IApprovalWorkflowItem workflowItem, string applicationName, string status = "Published")
           {
               if (workflowItem.ApprovalTrackingRecordMap == null)
               {
                   var approvalTrackingMap = new ApprovalTrackingRecordMap() { Id = Guid.NewGuid() };
      
                   workflowItem.ApprovalTrackingRecordMap = approvalTrackingMap;
               }
      
               var trackingRecord = new ApprovalTrackingRecord()
               {
                   ApplicationName = applicationName,
                   Culture = CultureInfo.InvariantCulture.LCID,
                   DateCreated = DateTime.Now,
                   Id = Guid.NewGuid(),
                   LastModified = DateTime.Now,
                   Note = string.Empty,
                   Status = status,
                   UserId = SecurityManager.CurrentUserId,
                   WorkflowItemId = workflowItem.Id
               };
      
               workflowItem.ApprovalTrackingRecordMap.ApprovalRecords.Add(trackingRecord);
      
               workflowItem.ApprovalWorkflowState.SetString(CultureInfo.InvariantCulture, status);
           }

    Here are the usings that you need:

    using System;
    using System.Globalization;
    using Telerik.Sitefinity;
    using Telerik.Sitefinity.Model;
    using Telerik.Sitefinity.Web.UI.Fields;
    using Telerik.Sitefinity.DynamicModules;
    using Telerik.Sitefinity.DynamicModules.Model;
    using Telerik.Sitefinity.Utilities.TypeConverters;
    using Telerik.Sitefinity.Workflow.Model.Tracking;
    using Telerik.Sitefinity.Security;

    Please let me know if you have further issues with this code.

    Kind regards,
    Lilia Messechkova
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  3. Jules
    Jules avatar
    65 posts
    Registered:
    20 Jul 2005
    23 Apr 2012
    Link to this post
    Thanks. That worked, but with a couple of corrections (shown below). Where you referred to the communityResourceType, it needed to be the communityResourceItem.

    //This is the additional line that needs to be executed
    // set the workflow UI status
    InitializeApprovalTrackingRecordMap((IApprovalWorkflowItem)communityResourceItem, dynamicModuleManager.Provider.ApplicationName, "Published");
      
    // We can now call the following to publish the item
    dynamicModuleManager.Lifecycle.Publish(communityResourceItem);
      
    // You need to call SaveChanges() in order for the items to be actually persisted to data store
    dynamicModuleManager.SaveChanges();
  4. Lilia Messechkova
    Lilia Messechkova avatar
    42 posts
    Registered:
    13 Jul 2016
    25 Apr 2012
    Link to this post
    Hi Julia,

    You are absolutely right. I am sorry for misleading you.

    Regards,
    Lilia Messechkova
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
4 posts, 0 answered