Portal Engine Questions on portal engine and web parts.
Version 5.x > Portal Engine > Adding Grouped Attachments View modes: 
User avatar
Member
Member
Jason - 5/9/2011 9:03:07 AM
   
Adding Grouped Attachments
Hi all,
I am trying to add gruped attachments to my custom documnets using the API.

I am using workflow and save files to the disk

The documents I am working on require around 10-15 images per document to form an image gallery. It will take useres to long to add each of the images to the documents as there are around 200+ documents.

So I have built an ASPX page that allows a user to select a node ( I have bound my cms tree to a telerik treeview control) and then the user inputs the path to the files that I need to be added as attachments and then presses the button.

My code loops through all jpegs in the folder and then tries to add them as grouped attachments to the document which they have selected.

I have tried both examples in the api but it does not work, I get no errors but i get no attachments added to my documents.

I have added my code here for you guys to check.


Imports CMS.SiteProvider
Imports CMS.TreeEngine
Imports CMS.CMSHelper
Imports CMS.FileManager
Imports System.Data
Imports System.IO
Imports System.Collections
Imports CMS.WorkflowEngine

Partial Class DocAttach
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Not Page.IsPostBack Then
Dim ds As DataSet = Nothing
' create a TreeProvider instance
Dim ui As UserInfo = UserInfoProvider.GetUserInfo("administrator")
Dim tree As New CMS.TreeEngine.TreeProvider(ui)
' get dataset of tree nodes specified by alias path and class names (separated by semicolon),
' the second parameter says whether to return default culture documents if the required
' document language version is not available
ds = tree.SelectNodes("mosaicholidays", "/Destinations/%", "en-GB", True)
'ds = tree.SelectNodes("CorporateSite", "/Products/%", "en-us", [True], "cms.menuitem;cms.products")
'
Dim row As DataRow
For Each row In ds.Tables(0).Rows
If row.Item("ClassName") = "Mosaicholidays.Country" Then
row.Item("NodeParentID") = DBNull.Value
End If
Next
Me.RadTreeview.DataSource = ds
Me.RadTreeview.DataBind()

'With Me.GridView1
' .DataSource = ds
' .DataBind()
'End With
End If

End Sub

Private Sub ImportCMSAttachmentWorkFlow()
Dim sNodeAliasPath As String = Me.RadTreeview.SelectedNode.Value.Trim().ToString() 'Me.Label1.Text.Trim()
Dim ui As UserInfo = UserInfoProvider.GetUserInfo("administrator")
Dim tree As New TreeProvider(ui)
' Get the document (current culture)
Dim node As CMS.TreeEngine.TreeNode = tree.SelectSingleNode(CMSContext.CurrentSiteName, sNodeAliasPath, CMSContext.CurrentUser.PreferredCultureCode, False, Nothing, False)
If node IsNot Nothing Then
' Get the latest version of the document (always use DocumentHelper when using workflow)
node = DocumentHelper.GetDocument(node, tree)
'Check out the document
Dim vm As New VersionManager(tree)
vm.CheckOut(node)
'' Update the document fields the regular way
'node.SetValue("ArticleName", "Edited testing workflow")
'node.SetValue("ArticleTeaserText", "Edited testing article for workflow")
'node.SetValue("ArticleText", "Edited testing article for workflow text")
' Update the attachment if present

' Add the attachment
DocumentHelper.AddAttachment(node, "HotelGalleryAttachments", "D:\WebSites\MosaicUpgrade\FlashGallery\236\BaronPalms_Beach.jpg", tree)
' Update the document
DocumentHelper.UpdateDocument(node, tree)
' Check in the document
vm.CheckIn(node, Nothing, Nothing)
'
Dim wm As New WorkflowManager(tree)
Dim currentStep As WorkflowStepInfo = wm.GetStepInfo(node)
While (currentStep IsNot Nothing) AndAlso (currentStep.StepName.ToLower() <> "published")
currentStep = wm.MoveToNextStep(node, "")
End While
End If
End Sub


Private Sub ImportCMSAttachments()
Dim sFolderPath As String = "D:\WebSites\MosaicUpgrade\FlashGallery\" & Me.txtFolderPath.Text.Trim() & "\"
Dim ui As UserInfo = UserInfoProvider.GetUserInfo("administrator")
Dim sNodeAliasPath As String = Me.RadTreeview.SelectedNode.Value.Trim().ToString() 'Me.Label1.Text.Trim()
Dim tree As New TreeProvider(ui)
' First, get the document
Dim node As CMS.TreeEngine.TreeNode = tree.SelectSingleNode(CMSContext.CurrentSite.SiteName, sNodeAliasPath, "en-GB")
If node IsNot Nothing Then
' Create the attachment manager
Dim am As New AttachmentManager(tree.Connection)

'File Loop
Dim pictures As New SortedList
Dim filename As String = ""
Dim i As Integer
Dim files As FileInfo() = New DirectoryInfo(sFolderPath).GetFiles()
'Array.Sort(Of FileInfo)(files, AddressOf FileInfoComparison)
For i = 0 To files.Length - 1
filename = files(i).Name.ToLower()
If filename.EndsWith(".jpg") Then
'Attachment Code
' Create the attachment
Dim attachmentGuid As Guid = Guid.NewGuid()
Dim ai As New AttachmentInfo(sFolderPath & filename, node.DocumentID, attachmentGuid, tree.Connection)
am.SetAttachmentInfo(ai)
' Set the attachment reference
node.SetValue("HotelGalleryAttachments", attachmentGuid)
node.Update()
'
End If
Next
'End File Loop
End If
End Sub

Protected Sub RadTreeview_NodeClick(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadTreeNodeEventArgs) Handles RadTreeview.NodeClick
If Not e.Node.Level = 2 Then
Me.Label1.Text = "Warning You can not Import at this level"
Me.btnImport.Enabled = False
Else
Me.Label1.Text = "importing Files to Alias Path: " & e.Node.Value
Me.btnImport.Enabled = True
End If
End Sub

Protected Sub btnImport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnImport.Click
ImportCMSAttachmentWorkFlow()
End Sub
End Class




Many thanks for help on this.

Jason

User avatar
Kentico Consulting
Kentico Consulting
kentico_mirekr - 5/10/2011 11:22:49 AM
   
RE:Adding Grouped Attachments
Hi Jason,

If you would like to save group attachments for your document you need to specify AttachmentGroupGUID value of your AttachmentInfo object which you are missing.

Please find example code below on how to get AttachmentGroupGUID of your document type field (example is made for cms.news document type and field with name NewsAttachments):

using CMS.SettingsProvider;
using CMS.FormEngine;

DataClassInfo dci = DataClassInfoProvider.GetDataClass("cms.news");

if (dci != null)
{
FormInfo fi = new FormInfo(dci.ClassFormDefinition);
FormFieldInfo ffi = fi.GetFormField("NewsAttachments");
Guid AttachmentGroupGUID = ffi.Guid;
}


Best regards,
Miro Remias