This is somewhat of a continuation of a prior post but because I had the import working correctly and the issue that I am now having is different I felt that it should be a new post. I have built a module that automates the creation of articles and although it is far from complete in respect to my project it is fully functional now with the exception that the article does not post automatically to the page upon insert instead you must go into the Engage Publish GUI and select the article to be placed on the page. With that said the article seems to be created properly in terms of the Article, its Settings and its Relationships. Any help in regards to how to get the article to populate a particular page on insert is appreciated. I will include my code for anyone out there to use as a jump start for there own migration project as well as possible help in troubleshooting why my articles are not publishing to the page specified automatically.
Thanks again.
Derek
Code:
using System;
using System.Web.UI;
using System.Text;
using System.Collections.Generic;
using System.Reflection;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Engage.Dnn.Publish;
using Engage.Dnn.Publish.ArticleControls;
using Engage.Dnn.Publish.Admin;
using Engage.Dnn.Publish.CategoryControls;
using Engage.Dnn.Publish.Controls;
using Engage.Dnn.Publish.Data;
using Engage.Dnn.Publish.Forum;
using Engage.Dnn.Publish.Portability;
using Engage.Dnn.Publish.Search;
using Engage.Dnn.Publish.Security;
using Engage.Dnn.Publish.Services;
using Engage.Dnn.Publish.Tags;
using Engage.Dnn.Publish.Util;
using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Data;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Security;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Exceptions;
public partial class DesktopModules_ImportTool_ImportTool : DotNetNuke.Entities.Modules.PortalModuleBase
{
protected void Page_Load(object sender, System.EventArgs e) {
//if (!Page.IsPostBack)
//{
// CreatePosting();
//}
}
public const string PublishCacheKeys = "PublishCacheKeys";
private static void ClearPublishCache(int portalId)
{
string cacheKey = PublishCacheKeys + portalId.ToString();
ArrayList al;
al = DataCache.GetCache(cacheKey) as ArrayList;
if (al != null)
{
foreach (string s in al)
{
DataCache.RemoveCache(s);
}
}
DataCache.RemoveCache(cacheKey);
}
private void CreatePosting()
{
try
{
string name = "Migration Test Article Name";
string htmlEncoded = "<p>Migration Test Article<p>";
string description = "Migration Test Article Description";
int pageid = 60; //current page to place article
int portalid = 0; //current portal to import into
Article art = Article.Create(0);
art.ApprovalComments = "Auto Imported by Migration Process";
art.ApprovalStatusId = ApprovalStatus.Approved.GetId();
art.ApprovalUserId = 1;
art.ArticleText = HttpUtility.HtmlDecode(htmlEncoded);
art.AuthorUserId = 1;
art.CreatedDate = DateTime.Now.ToString();
art.LastUpdated = DateTime.Now.ToString();
art.Description = description;
art.DisplayTabId = pageid;
art.ItemTypeId = ItemType.Article.GetId();
art.LanguageId = 1;
art.Name = name;
art.StartDate = DateTime.Now.ToString();
art.PortalId = portalid;
art.Save(1);
}
catch (Exception ex)
{
TextBox1.Text = ex.InnerException.Message;
}
finally
{
TextBox1.Text += Environment.NewLine;
TextBox1.Text += "Article Imported";
TextBox1.Text += Environment.NewLine;
}
}
private void CreateRelationship(int lastItemId, int lastItemVersionId)
{
int parentItemId = 11; //mapped to the correct parent, which happens to be category
int childItemId = lastItemId; //GetLastItemId();
int childItemVersionId = lastItemVersionId; //GetLastItemVersionId();
TextBox1.Text += Convert.ToString(childItemId);
TextBox1.Text += Environment.NewLine;
TextBox1.Text += Convert.ToString(childItemVersionId);
ItemRelationship ir = new ItemRelationship();
ir.RelationshipTypeId = RelationshipType.ItemToParentCategory.GetId();
ir.StartDate = DateTime.Now.ToString();
ir.EndDate = null;
ir.SortOrder = 0;
ItemRelationship.AddItemRelationship(childItemId, childItemVersionId, parentItemId, ir.RelationshipTypeId, ir.StartDate, ir.EndDate, ir.SortOrder);
TextBox1.Text += Environment.NewLine;
TextBox1.Text += "Relationship Imported";
}
private static int GetArticleDisplayTabId(string pagename) //not currently being used
{
int tabid = 60; //need to add code for this, possibly from a translation table of some sort
return tabid;
}
private static int GetLastItemVersionId()
{
int itemVersionId;
StringBuilder mySqlString = new StringBuilder();
mySqlString.Append("SELECT max(ItemVersionId) ");
mySqlString.Append("FROM {databaseOwner}{objectQualifier}Publish_ItemVersion ");
System.Data.IDataReader dr = ((IDataReader)(DotNetNuke.Data.DataProvider.Instance().ExecuteSQL(mySqlString.ToString())));
dr.Read();
itemVersionId = Convert.ToInt32(dr[0]);
return itemVersionId;
}
private static int GetLastItemId(int lastItemVersionId)
{
int itemVersionId = lastItemVersionId;//GetLastItemVersionId();
int itemId;
StringBuilder mySqlString = new StringBuilder();
mySqlString.Append("SELECT ItemId ");
mySqlString.Append("FROM {databaseOwner}{objectQualifier}Publish_ItemVersion ");
mySqlString.Append("WHERE ItemVersionId = " + Convert.ToString(itemVersionId));
System.Data.IDataReader dr1 = ((IDataReader)(DotNetNuke.Data.DataProvider.Instance().ExecuteSQL(mySqlString.ToString())));
dr1.Read();
itemId = Convert.ToInt32(dr1[0]);
return itemId;
}
public void PopulateItemVersionSettingsTable(int lastItemVersionID)
{
// Create Item Version Settings
ItemVersionSetting ivs1 = new ItemVersionSetting();
ivs1.ItemVersionId = lastItemVersionID; //Convert.ToInt16(TextBox4.Text);//Grab Greatest ItemVersionId from ItemVersion table
ivs1.ControlName = "chkUseApprovals";
ivs1.PropertyName = "Checked";
ivs1.PropertyValue = "True";
ivs1.Save();
ItemVersionSetting ivs2 = new ItemVersionSetting();
ivs2.ItemVersionId = lastItemVersionID; //Convert.ToInt16(TextBox4.Text);//Grab Greatest ItemVersionId from ItemVersion table
ivs2.ControlName = "ArticleSettings";
ivs2.PropertyName = "DisplayOnCurrentPage";
ivs2.PropertyValue = "True";
ivs2.Save();
ItemVersionSetting ivs3 = new ItemVersionSetting();
ivs3.ItemVersionId = lastItemVersionID; //Convert.ToInt16(TextBox4.Text);//Grab Greatest ItemVersionId from ItemVersion table
ivs3.ControlName = "ArticleSettings";
ivs3.PropertyName = "ForceDisplayOnPage";
ivs3.PropertyValue = "False";
ivs3.Save();
ItemVersionSetting ivs4 = new ItemVersionSetting();
ivs4.ItemVersionId = lastItemVersionID; //Convert.ToInt16(TextBox4.Text);//Grab Greatest ItemVersionId from ItemVersion table
ivs4.ControlName = "pnlPrinterFriendly";
ivs4.PropertyName = "Visible";
ivs4.PropertyValue = "True";
ivs4.Save();
ItemVersionSetting ivs5 = new ItemVersionSetting();
ivs5.ItemVersionId = lastItemVersionID; //Convert.ToInt16(TextBox4.Text);//Grab Greatest ItemVersionId from ItemVersion table
ivs5.ControlName = "ArticleSettings";
ivs5.PropertyName = "IncludeParentCategoryArticles";
ivs5.PropertyValue = "False";
ivs5.Save();
ItemVersionSetting ivs6 = new ItemVersionSetting();
ivs6.ItemVersionId = lastItemVersionID; //Convert.ToInt16(TextBox4.Text);//Grab Greatest ItemVersionId from ItemVersion table
ivs6.ControlName = "ArticleSettings";
ivs6.PropertyName = "DisplayReturnToList";
ivs6.PropertyValue = "False";
ivs6.Save();
ItemVersionSetting ivs7 = new ItemVersionSetting();
ivs7.ItemVersionId = lastItemVersionID; //Convert.ToInt16(TextBox4.Text);//Grab Greatest ItemVersionId from ItemVersion table
ivs7.ControlName = "pnlEmailAFriend";
ivs7.PropertyName = "Visible";
ivs7.PropertyValue = "True";
ivs7.Save();
TextBox1.Text += Environment.NewLine;
TextBox1.Text += "Settings Imported";
}
public void Import()
{
CreatePosting();
int itemVersionID = GetLastItemVersionId();
int itemID = GetLastItemId(GetLastItemVersionId());
CreateRelationship(itemID, itemVersionID);
PopulateItemVersionSettingsTable(itemVersionID);
ClearPublishCache(0);
}
protected void Button1_Click(object sender, EventArgs e)
{
Import();
}
}