Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Unable to use Export Data; Invalid URI

Vote:
 

I am working on my second site using EPiServer, and I have come across a problem both times when I try to export content items. I can export content types just fine, but without the items I have to rebuild the site at the new location from scratch. When I try to use the Export Data tool in the Admin tab of the CMS, it errors out with this(on the second site):

The following warnings have been generated:[Exporting content 91] Failed to export property MainBody, exception: Invalid URI: The Uri string is too long.

I have attempted removing the page, but then the error just happens, referencing another content item instead. The website in question has 15 pages, despite having such a high number for the content item (due to deleting and creating new pages).

Has anyone experienced a similar problem? It is okay to have to rebuild the sites right now, but my next site is going to be many times larger than these last two have been, and I'd hate to have to build this site multiple times if I can help it. Any new ideas would be great, as I have exhausted my own.

#113623
Nov 25, 2014 20:44
Vote:
 

We have stopped trying to use the built-in exporter due to these kinds of errors, and have instead begun to export the database through sql server. That being said, I'm sure some other people might have this problem, and it would probably be good if there was a solution. Is this a bug with EPiServer 7.5? Is there a compatibility issue with Visual Studio? Is it something else entirely?

#114900
Dec 30, 2014 22:14
Vote:
 

Hi!

A bit late to the party, but I just had similar problem and I managed to find the reason & fix it. I will post my findings here.

In my case the issue was inline images that where embedded directly in the content. Content ID in the error message was not related to the page with embedded images. Please check if there are no pages that contain such embedded images and replace them with proper uploaded & linked images.

I wrote a blog post on this issue http://geta.no/blogg/when-data-export-fails

#132653
Edited, Aug 14, 2015 18:30
Vote:
 

You can write a custom schedule job to convert the inline images to image files (and updating the url in XHTML string fields) before exporting the data. I ran into the same problem with an EPiServer CMS 6 R2 site, and the schedule plugin look like the code below. This is for the CMS 6 R2 site which still use old VPP file system but I think you can get the general idea, and update the code to work with later version of EPiServer CMS.

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Hosting;
using EPiServer;
using EPiServer.Core;
using EPiServer.Core.Html.StringParsing;
using EPiServer.DataAccess;
using EPiServer.Security;
using EPiServer.SpecializedProperties;
using EPiServer.Web;
using EPiServer.Web.Hosting;

namespace Core.Web.Templates.Plugins.Admin
{
    [EPiServer.PlugIn.ScheduledPlugIn(
        DisplayName = "Convert inline images to VPP files",
        Description = "A scheduled job to convert inline images to VPP files",
        SortIndex = 1)]
    public class ConvertInlineImageToVpp
    {
        private static readonly DataFactory _dataFactory = DataFactory.Instance;

        private const string InlineImageRegex =
            "src[ ]*=[ ]*[\",'](?<src>data:image\\/(?<imageType>[a-z]*);(?<encode>[a-zA-Z0-9]*),(?<data>[^\"]*))[\",']";

        public static string Execute()
        {
            var message = new StringBuilder("Migrated contents: ");

            // Get all page ids which contain inline images in XHTML strings
            var pagesToMigrate = GetContentToMigrate();
            foreach (var pageId in pagesToMigrate)
            {
                MigrateContent(pageId);
                message.Append($"{pageId} ");
            }

            return message.ToString();
        }

        private static void MigrateContent(int contentId)
        {
            var page = _dataFactory.GetPage(new PageReference(contentId)).CreateWritableClone();
            if (page != null)
            {
                var updated = false;

                // Loop through all XHTML property
                foreach (var property in page.Property)
                {
                    if (property.Type == PropertyDataType.LongString)
                    {
                        if (property is PropertyXhtmlString xhtmlValue)
                        {
                            if (!xhtmlValue.IsNull)
                            {
                                var fragments = xhtmlValue.Fragments.ToArray();
                                for (int i = 0; i < fragments.Length; i++)
                                {
                                    var bodyTextFragment = fragments[i];
                                    if (bodyTextFragment.GetType() == typeof(StaticFragment))
                                    {
                                        var htmlFragment = bodyTextFragment as StaticFragment;

                                        var plainText = htmlFragment.InternalFormat;
                                        var regex = new Regex(InlineImageRegex);
                                        var results = regex.Matches(plainText);

                                        // Find all matches in the string
                                        if (results.Count > 0)
                                        {
                                            foreach (Match match in results)
                                            {
                                                // Get the capturing groups value
                                                var imageType = match.Groups["imageType"].Value;
                                                var data = match.Groups["data"].Value;
                                                var alt = match.Groups["alt"]?.Value;
                                                var src = match.Groups["src"].Value;

                                                // Perform conversion from base 64 string to binary data
                                                var binaryData = Convert.FromBase64String(data);

                                                // Create a VPP file for this page
                                                var file = CreateUnifiedFile(page, imageType, $"inline_image_{i}",
                                                    binaryData);

                                                // Replace the fragment with new text
                                                var url = file.VirtualPath;
                                                plainText = plainText.Replace(src, url);
                                            }

                                            var originalIndex = xhtmlValue.Fragments.IndexOf(bodyTextFragment);
                                            xhtmlValue.Fragments.Remove(bodyTextFragment);
                                            xhtmlValue.Fragments.Insert(originalIndex, new StaticFragment(plainText));

                                            updated = true;

                                            property.IsModified = true;
                                            page.IsModified = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                // Save the updated page
                if(updated)
                    _dataFactory.Save(page, SaveAction.Publish, AccessLevel.NoAccess);
            }

        }

        private static UnifiedFile CreateUnifiedFile(PageData page, string extension, string fileName, byte[] data)
        {
            // Create page folder if it does not exist
            int folderId = (int)page.Property["PageFolderID"].Value;
            string pageDirectoryRootVirtualPath = VirtualPathHandler.PageDirectoryRootVirtualPath;
            IPageDirectory pageDirectory = HostingEnvironment.VirtualPathProvider.GetDirectory(pageDirectoryRootVirtualPath) as IPageDirectory;
            string virtualPathFromFolderId = VirtualPathUtilityEx.Combine(pageDirectoryRootVirtualPath, VirtualPathUtility.AppendTrailingSlash(folderId.ToString()));

            UnifiedDirectory directory = HostingEnvironment.VirtualPathProvider.GetDirectory(virtualPathFromFolderId) as UnifiedDirectory;
            if (directory == null)
            {
                directory = pageDirectory.CreateSubdirectory(folderId.ToString(), page);
            }

            // Create the handle for the file
            var uploadPath = VirtualPathUtilityEx.Combine(directory.VirtualPath, $"{fileName}.{extension}");

            UnifiedFile file = HostingEnvironment.VirtualPathProvider.GetFile(uploadPath) as UnifiedFile;
            if (file == null)
            {
                file = directory.CreateFile(VirtualPathUtility.GetFileName(uploadPath));
            }

            // Write data to the file
            using (var stream = file.Open(FileMode.OpenOrCreate, FileAccess.Write))
            {
                var writer = new BinaryWriter(stream);
                writer.Write(data);
                writer.Flush();
            }
            
            return file;
        }

        private static IEnumerable<int> GetContentToMigrate()
        {
            var returnItems = new List<int>();
            const string query = "SELECT [fkPageID] FROM [dbo].[tblProperty] WHERE LongString LIKE \'%data:image/%\' ORDER BY [fkPageId]";

            using (var cn = new SqlConnection(ConfigurationManager.ConnectionStrings["EPiServerDB"].ConnectionString))
            {
                cn.Open();

                var cmd = new SqlCommand(query, cn);
                var reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    returnItems.Add((int)reader[0]);
                }

                reader.Close();
                cn.Close();
            }

            return returnItems;
        }
    }
}
#198018
Oct 19, 2018 4:12
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.