Import data and publish?

Posted by Community Admin on 04-Aug-2018 21:56

Import data and publish?

All Replies

Posted by Community Admin on 21-Apr-2012 00:00

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.

Posted by Community Admin on 23-Apr-2012 00:00

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

Posted by Community Admin on 23-Apr-2012 00:00

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();

Posted by Community Admin on 25-Apr-2012 00:00

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

This thread is closed