2SXC 导入内容项 - 超链接文件库

2SXC Import Content Items - Hyperlink File Libraries

我已经尽我所能地查看了数据库,但我找不到一种方法来为每个内容项在 Hyperlink-Library 中导入文件。我看到 2sxc 使用 DNN 的文件和文件夹 table,但我看不到 2sxc 内容类型的字段 links 如何指向文件夹和文件。

基本上,我有大约 400 多个内容项要导入,还有大约 6000 个 linked 文件需要导入。

我认为可能无法直接从 XML 文件中导入文件,但是是否可以编写一个 sql 脚本来 link 文件到内容项?

有办法,但有点神秘:)

当项目位于 ADAM 中专用于该字段的文件夹中时,将图像链接到项目(和右侧字段)会自动发生。模式是 ca。像这样 [portal root]/adam/[app-name]/[entity-guid22]/[field-name]

手动创建一个条目,并验证您看到的内容。所以您基本上可以使用 excel / xml import https://2sxc.org/en/Learn/Import-Export 导入数据,然后您最大的挑战将是生成 guid22。这是一种更紧凑的 guid 形式,它采用长 guid 并使用 url 安全字符对其重新编码。

2sxc 中有一个命令基本上可以执行此操作 ToSic.Eav.Identity.Mapper.GuidCompress(original guid)

另见 https://github.com/2sic/eav-server/blob/05d79bcb80c109d1ceb8422875a6ff7faa34ff4f/ToSic.Eav.Core/Identity/Mapper.cs

感谢@iJungleBoy,我已经能够创建一个流程来帮助自动将文件库导入到 2sxc 内容项中。

内容项的导入遵循 https://2sxc.org/en/Learn/Import-Export

中的说明

在他的指导下,我创建了一些 MS SQL 脚本来帮助完成一些繁重的工作。

基本上,我们需要为 ADAM 文件夹中的文件创建目录结构,并且需要专门为它们命名,以便它们与每个内容项正确关联。这些脚本依赖于一个额外的 table 来保存有关要导入的文件的信息,以便它们可以与之前导入到 2sxc 中的内容项相关联。

这是一个SQL脚本,可以根据您的需要进行修改:

-- Create function to return content items guid converted to base64
CREATE FUNCTION dbo.import2sxc_BinaryToBase64
(
    @bin VARBINARY(MAX)
)
RETURNS VARCHAR(MAX)
AS
BEGIN
    DECLARE @Base64 VARCHAR(MAX)
    SET @Base64 = CAST(N'' AS XML).value('xs:base64Binary(xs:hexBinary(sql:variable("@bin")))', 'VARCHAR(MAX)')
    RETURN @Base64
END
GO

-- Create function to compress guid for 2sxc
CREATE FUNCTION dbo.import2sxc_GuidCompress
(
    @guidStr VARCHAR(36)
)
RETURNS VARCHAR(22)
AS
BEGIN
    declare @guid uniqueidentifier = @guidStr
    RETURN Substring(Replace(Replace(dbo.import2sxc_BinaryToBase64(@guid), '+', '-'),'/', '_'), 1, 22)
END
GO

-- Define the app name
DECLARE @appName nvarchar(255) = 'MyAppName'

-- Define the name of the content type
DECLARE @contentType nvarchar(150) = 'MyContentType'

-- Set the path for the adam files for the app
DECLARE @adamPath nvarchar(max) = 'c:\path\to\Portals\x\adam'

-- For importing images, get the name of the field that holds the id of the record from the original system
DECLARE @idFieldname nvarchar(50) = 'OriginalId'



-- Get the attribute set id for the content item
DECLARE @attributeSetId int
SELECT @attributeSetId = AttributeSetID FROM dbo.ToSIC_EAV_AttributeSets WHERE Name = @contentType


-- Get the attribute id 
DECLARE @attributeId int
SELECT @attributeId = a.AttributeID
FROM dbo.ToSIC_EAV_Attributes a
INNER JOIN dbo.ToSIC_EAV_AttributesInSets ais on a.AttributeID = ais.AttributeID
WHERE ais.AttributeSetID = @attributeSetId AND StaticName = @idFieldname


-- Get all the content items, along with the compressed guid for the folder name, and generate the commands to create the direcctories
SELECT v.Value as SourceId, EntityGUID, dbo.import2sxc_GuidCompress(EntityGUID) as FolderName, 'mkdir "' + @adamPath + '\' + @appName + '\' +  dbo.import2sxc_GuidCompress(EntityGUID) + '\Photos"' as cmdMkdir
FROM ToSIC_EAV_Entities e
INNER JOIN ToSIC_EAV_Values v ON e.EntityID = v.EntityID AND v.AttributeID = @attributeId
WHERE AttributeSetID = @attributeSetId


-- Create command to move files into the new folders
SELECT 'copy "' + f.Filename + '" "' + @adamPath + '\' + @appName + '\' +  dbo.import2sxc_GuidCompress(EntityGUID) + '\Photos"' as cmdMove
FROM ToSIC_EAV_Entities e
INNER JOIN ToSIC_EAV_Values v ON e.EntityID = v.EntityID AND v.AttributeID = @attributeId
INNER JOIN import2sxc_Files f on v.Value = f.OriginalId
WHERE AttributeSetID = @attributeSetId


DROP FUNCTION dbo.import2sxc_BinaryToBase64

DROP FUNCTION dbo.import2sxc_GuidCompress

在脚本 运行 之后,您将拥有名为 cmdMkdir 和 cmdMove 的列,它们是您可以 运行 创建文件夹并根据需要将文件移动到其中的命令行脚本。

当内容项已导入,创建文件夹和移动文件的脚本已 运行,您应该清除 DNN 中的服务器缓存并转到站点资产(文件管理器) 在 DNN 中并刷新 ADAM 文件夹和子文件夹。

执行此操作后,内容项的库中的所有文件都应该出现。