February 19, 2008

How to create folders programatically in Business Objects XI repository

This is an example of how to create folders in the Business Objects XI InfoStore using VB6 and the COM API. This code creates a folder called "MyNewFolder" beneath "TestFolder", which is created at the folder root.



Dim sessionMgr, entSession, infoStore, pluginMgr, folderPlugin

'create a session and connect to the InfoStore
Set sessionMgr = CreateObject("CrystalEnterprise.SessionMgr")
Set entSession = sessionMgr.Logon("Administrator", "myPassword", "myBoServer", "secEnterprise")
Set infoStore = entSession.Service("", "InfoStore")
Set pluginMgr = infoStore.PluginManager
Set folderPlugin = pluginMgr.PluginInfo("Folder")

Dim infoObjectCollection, newFolder

Set infoObjectCollection = infoStore.NewInfoObjectCollection()
Set newFolder = infoObjectCollection.Add(folderPlugin)

newFolder.Title = "MyNewFolder"
newFolder.Description = "This is the folder we're creating in the example"

Dim infoObjectCollectionAux 'used to help determine the parent folder id
Dim queryStr As String
Dim queryStr1 As String
Dim queryStr2 As String

'build a query to get the parent folder id
'you can test this using the Query Builder in the Admin Launchpad
queryStr1 = "SELECT * FROM CI_INFOOBJECTS WHERE SI_NAME='"
queryStr2 = "' AND SI_KIND='Folder' AND SI_PARENTID=0" 'the root folder has id=0
queryStr = queryStr1 & "TestFolder" & queryStr2

'execute the query and get the parent folder id
'if "TestFolder" doesn't exist in the root, the infoObjectCollectionAux will be empty
Set infoObjectCollectionAux = infoStore.Query(queryStr)
Dim parentFolderId As Long
'only get the first item, for example simplicity sake
parentFolderId = infoObjectCollectionAux.Item(1).Id

'set the new folder's parent id and commit the changes
newFolder.ParentID = parentFolderId
infoStore.Commit (infoObjectCollection)

No comments: