2SXC Import Content Items - Hyperlink File Libraries

不想你离开。 提交于 2021-01-29 09:45:02

问题


I've looked through the database best as I can, but I can't find a way to import files in a Hyperlink-Library for each content item. I see that 2sxc uses DNN's Filed and Folders table, but I cant see how the 2sxc content type's field links to the folder and the files.

Basically, I have about 400+ content items to import and about 6000+ linked files that need to be imported.

I figure it probably isn't possible to import the files directly from the XML file, but is it possible to write an sql script to link the files to the content item?


回答1:


There is a way but it's a bit of a secret :)

Linking the images to the item (and the right field) happens automatically, when the items are in the folder dedicated to that field in ADAM. The schema is ca. like this [portal root]/adam/[app-name]/[entity-guid22]/[field-name]

Create one entry manually, and verify what you see. So you can basically import the data using the excel / xml import https://2sxc.org/en/Learn/Import-Export and then your biggest challenge will be to generate the guid22. This is a more compact form of the guid, which takes a long guid and re-encodes it using url-safe characters.

Theres a command in 2sxc which does this, basically ToSic.Eav.Identity.Mapper.GuidCompress(original guid)

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




回答2:


Thanks to @iJungleBoy, I have been able to create a process that can help automate the importing of file libraries into 2sxc content items.

The importing of content items follows the instructions at https://2sxc.org/en/Learn/Import-Export

Expanding upon his guidance, I created some MS SQL scripts to help do some of the heavy lifting.

Basically, we need to create the directory structure for the files in the ADAM folder and they need to be named specifically so that they are properly associated with each content item. The scripts rely upon an additional table that holds info on the files to be imported so that they can be correlated with the content items that have been previously imported into 2sxc.

Here is an SQL script that can be modified based on your needs:

-- 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

After the script is run, you will have columns named cmdMkdir and cmdMove that are the command line scripts you can run to create the folders and move the files into them as needed.

When the content items have been imported, and the scripts to create the folders and move the files have been run, you should clear the server cache in DNN and also go to the Site Assets (file manager) in DNN and refresh the ADAM folder and subfolders.

After you do that, all the files in the library for your content items should appear.



来源:https://stackoverflow.com/questions/54816165/2sxc-import-content-items-hyperlink-file-libraries

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!