Error with Custom Import Module

Topics: General, Troubleshooting, Writing modules
Aug 14, 2013 at 6:06 PM
Edited Aug 14, 2013 at 6:09 PM
Hi All!
I'm trying to buld Custom import module for my old Articles
Import Method:
public ActionResult Import()
        {
            var document = XDocument.Load("C:/TEMP/export.xml");
            var articles = from ed in document.Element("Orchard").Element("Data").Elements("Article")
                           select ed;

            var images = _mediaLibrary.GetMediaContentItems(@"Publications\Articles\MainImage", 0, 0, "Image", "");


            foreach (var element in articles) {
                var article = new ImportPart();
                article.Title = element.Element("TitlePart").Attribute("Title").Value;
                article.Body = element.Element("BodyPart").Attribute("Text").Value;
                article.CreatedUtc = element.Element("CommonPart").Attribute("CreatedUtc").Value;
                article.ModifiedUtc = element.Element("CommonPart").Attribute("ModifiedUtc").Value;
                article.PublishedUtc = element.Element("CommonPart").Attribute("PublishedUtc").Value;
                //article.MetaKeywords = element.Element("TextField.Keywords").Attribute("Text").Value;
                var xElement = element.Element("TextField.Keywords").Attribute("Text");
                article.MetaKeywords = xElement != null ? xElement.Value : null;
                xElement = element.Element("TextField.Title").Attribute("Text");
                article.MetaTitle = xElement != null ? xElement.Value : article.Title;
                article.Offtop = "";
                article.Permalink = element.Element("AutoroutePart").Attribute("Alias").Value;
                article.PreviewText = element.Element("TextField.PreviewText").Attribute("Text").Value;
                //article.SourceText = element.Element("LinkField.sourceLink").Attribute("Text").Value;
                xElement = element.Element("LinkField.sourceLink").Attribute("Text");
                article.SourceText = xElement != null ? xElement.Value : null;
                //article.SourceUrl = element.Element("LinkField.sourceLink").Attribute("Url").Value;
                xElement = element.Element("LinkField.sourceLink").Attribute("Url");
                article.SourceUrl = xElement != null ? xElement.Value : null;
                article.SubTitle = element.Element("TextField.SubTitle").Attribute("Text").Value;


                var rubric = element.Element("TaxonomyField.PublicationRubric").Attribute("Terms").Value;
                article.Rubric = rubric.Split(new string[] { "/alias=tags", "/alias=", "\\", "/", ",", "\"" }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();

                var sections = element.Element("TaxonomyField.PublicationSection").Attribute("Terms").Value;
                article.Sections = sections.Split(new string[] { "/alias=tags", "/alias=", "\\", "/", ",", "\"" }, StringSplitOptions.RemoveEmptyEntries);

                var tags = element.Element("TaxonomyField.Tags").Attribute("Terms").Value;
                article.Tags = tags.Split(new string[] { "/alias=", ",", "\"" }, StringSplitOptions.RemoveEmptyEntries).ToList();
                var tagList = new List<string>();
                foreach (var s in article.Tags) {
                    //var d = s.Remove(4, 2);
                    var d = s.Replace("\\/", "/");
                    tagList.Add(d);
                }

                var img = element.Element("MediaPickerField.PublicationImage").Attribute("Url").Value;
                var imgName = img.Split(new string[] { "~/Media/Default/images/" }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
                var image = images.FirstOrDefault(x => x.FileName == imgName);

                //var tagsList = new List<TermPart>();

                var sectionsList = article.Sections.Select(item => _taxonomyService.GetTermByPath(item)).ToList();
                var tagsList = tagList.Select(item => _taxonomyService.GetTermByPath(item)).ToList();
                var rubricList = _taxonomyService.GetTermByPath(article.Rubric);

                ContentItem contentItem = Services.ContentManager.New("Article");
                Services.ContentManager.Create(contentItem);

                contentItem.As<ArticlePart>().Title = article.Title;

                contentItem.As<ArticlePart>().Body = article.Body;
                contentItem.As<ArticlePart>().PreviewText = article.PreviewText;
                contentItem.As<ArticlePart>().SubTitle = article.SubTitle;

                contentItem.As<SourcePart>().SourceTitle = article.SourceText;
                contentItem.As<SourcePart>().SourceUrl = article.SourceUrl;

                contentItem.As<SeoPart>().MetaTitle = article.MetaTitle;
                contentItem.As<SeoPart>().MetaKeywords = article.MetaKeywords;

                contentItem.As<AutoroutePart>().DisplayAlias = article.Permalink;

                contentItem.As<CommonPart>().CreatedUtc = DateTime.Parse(article.CreatedUtc);
                contentItem.As<CommonPart>().ModifiedUtc = DateTime.Parse(article.ModifiedUtc);
                contentItem.As<CommonPart>().PublishedUtc = DateTime.Parse(article.PublishedUtc);

                var field = ((dynamic)contentItem).Article.MainImage;
                (field as MediaLibraryPickerField).Ids = new int[] {image.Id};

                





                _contentManager.Create(contentItem);
                Services.ContentManager.Publish(contentItem);

                _taxonomyService.UpdateTerms(contentItem, new[] { rubricList }, "Rubric");
                _taxonomyService.UpdateTerms(contentItem, sectionsList, "Section");
                _taxonomyService.UpdateTerms(contentItem, tagsList, "Tags");

                var rrrr = 33;
            }
When i try to use this method i get error: (Its first of many errors that i get with [Timbioz.Publications.Article.Models.ArticlePartRecord#280][SQL: UPDATE Timbioz_Publications_Article_ArticlePartRecord SET Title = ?, SubTitle = ?, PreviewText = ?, Body = ?, MiniTitle1 = ?, MiniBody1 = ?, MiniTitle2 = ?, MiniBody2 = ?, MiniTitle3 = ?, MiniBody3 = ?, Offtop = ? WHERE Id = ?] ---> System.Data.SqlServerCe.SqlCeException: The data was truncated while converting from one data type to another. [ Name of function(if known) = ])
2013-08-14 19:59:01,638 [272] NHibernate.Event.Default.AbstractFlushingEventListener - Could not synchronize database state with session
NHibernate.Exceptions.GenericADOException: could not update: [Timbioz.Publications.Article.Models.ArticlePartRecord#280][SQL: UPDATE Timbioz_Publications_Article_ArticlePartRecord SET Title = ?, SubTitle = ?, PreviewText = ?, Body = ?, MiniTitle1 = ?, MiniBody1 = ?, MiniTitle2 = ?, MiniBody2 = ?, MiniTitle3 = ?, MiniBody3 = ?, Offtop = ? WHERE Id = ?] ---> System.Data.SqlServerCe.SqlCeException: The data was truncated while converting from one data type to another. [ Name of function(if known) =  ]
   at System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 hr)
   at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommandText(IntPtr& pCursor, Boolean& isBaseTableCursor)
   at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options)
   at System.Data.SqlServerCe.SqlCeCommand.ExecuteNonQuery()
   at NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery(IDbCommand cmd)
   at NHibernate.AdoNet.NonBatchingBatcher.AddToBatch(IExpectation expectation)
   at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)
   --- End of inner exception stack trace ---
   at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)
   at NHibernate.Persister.Entity.AbstractEntityPersister.UpdateOrInsert(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)
   at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Int32[] dirtyFields, Boolean hasDirtyCollection, Object[] oldFields, Object oldVersion, Object obj, Object rowId, ISessionImplementor session)
   at NHibernate.Action.EntityUpdateAction.Execute()
   at NHibernate.Engine.ActionQueue.Execute(IExecutable executable)
   at NHibernate.Engine.ActionQueue.ExecuteActions(IList list)
   at NHibernate.Engine.ActionQueue.ExecuteActions()
   at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session)
When i commented lines with As<ArticlePart> all is OK
contentItem.As<ArticlePart>().Title = article.Title;
contentItem.As<ArticlePart>().Body = article.Body;
contentItem.As<ArticlePart>().PreviewText = article.PreviewText;
contentItem.As<ArticlePart>().SubTitle = article.SubTitle;
Where is my mistake?
Aug 14, 2013 at 8:11 PM
Problem solved:

Helped AlterColumn in Migrations.cs and increase lenght of DB column....