Video Screencast Help
Symantec to Separate Into Two Focused, Industry-Leading Technology Companies. Learn more.

problem with asdk createFolder function

Created: 17 Apr 2012 • Updated: 07 May 2012 | 3 comments
This issue has been solved. See solution.

Hi,

I am new to asdk and creating a script in vbs to create a SWD task.

I want to create a folder for collections and for that i am using item management's

                      createFolder(folderName, parentFolderGuid) function.

but it is giving me error that parentFolderGuid doesnt exist.

I have looked into SMP and checked the guids, if i use GetItemByGuid() function i can extract all valid information from the guid. I have also tried creating dummy folders and giving those guids, or guids of windows folders, but non is working for me.

Any suggestion?

regards

fmadni

Comments 3 CommentsJump to latest comment

AngelD's picture

Hi fmadni,

 

Here are some code to play with

 

Const FOLDER_NAME			= "My New Folder"
Const FOLDER_GUID			= "321ab45c-d634-4a92-b2e7-8ab42322c3e4"
Const PARENT_FOLDER_NAME	= "My Parent Folder"
Const PARENT_FOLDER_GUID	= "123ab45c-d634-4a92-b2e7-8ab42322c3e4"

Dim ASDK_ITEM_MANAGEMENT, oParentFolder, oFolder
Set ASDK_ITEM_MANAGEMENT = CreateObject("Altiris.ASDK.NS.ItemManagement")
ASDK_ITEM_MANAGEMENT.CreateLocalInstance()


'// try to get parent folder by Guid
If (NOT GetFolderByGuid(PARENT_FOLDER_GUID, oParentFolder)) Then
	WScript.Echo "GetFolderByGuid failed (FolderGuid: " & PARENT_FOLDER_GUID & ")"
	WScript.Quit
End If
WScript.Echo "ParentFolder (Name: " & oParentFolder.Name & ", Guid: " & oParentFolder.Guid & ", ParentFolderName: " & oParentFolder.ParentFolderName & ", ParentFolderGuid: " & oParentFolder.ParentFolderGuid & ")"

'// try to get parent folder by Name
If (NOT GetFolder(PARENT_FOLDER_NAME, oParentFolder)) Then
	WScript.Echo "GetFolder failed (Name: " & PARENT_FOLDER_NAME & ")"
	WScript.Quit
End If
WScript.Echo "ParentFolder (Name: " & oParentFolder.Name & ", Guid: " & oParentFolder.Guid & ", ParentFolderName: " & oParentFolder.ParentFolderName & ", ParentFolderGuid: " & oParentFolder.ParentFolderGuid & ")"


'// PARENT_FOLDER_GUID could be changed to oParentFolder.Guid
If (NOT CreateFolder(FOLDER_NAME, PARENT_FOLDER_GUID, oFolder)) Then
	WScript.Echo "CreateFolder failed (Name: " & FOLDER_NAME & ", ParentFolderGuid: " & PARENT_FOLDER_GUID & ")"
	WScript.Quit
End If

WScript.Echo "Folder (Name: " & oFolder.Name & ", Guid: " & oFolder.Guid & ", ParentFolderName: " & oFolder.ParentFolderName & ", ParentFolderGuid: " & oFolder.ParentFolderGuid & ")"



Function CreateFolder(ByVal sFolderName, ByVal sParentFolderGuid, ByRef oFolder)
	On Error Resume Next
	CreateFolder = false
	
	If (GetFolderByParentFolder(sFolderName, sParentFolderGuid, oFolder)) Then
		'// Folder already exists, returning existing folder instead
		CreateFolder = true
		Exit Function
	End If
	
	Err.Clear
	Set oFolder = ASDK_ITEM_MANAGEMENT.CreateFolder(sFolderName, "{" & sParentFolderGuid & "}")
	If ((Err = 0) AND (NOT IsNothing(oFolder))) Then
		CreateFolder = true
		Exit Function
	End If
End Function

Function GetFolderByGuid(ByVal sFolderGuid, ByRef oFolder)
	GetFolderByGuid = NS_GetItemByGuid(sFolderGuid, oFolder)
End Function

Function GetFolderByParentFolder(ByVal sFolderName, ByVal sFolderGuid, ByRef oFolder)
	Dim oItems, oItem
	GetFolderByParentFolder = false
	
	oItems = ASDK_ITEM_MANAGEMENT.GetItemsByName(sFolderName)
	For Each oItem in oItems
		' if there is more than one item with this name then you need to pick which one you want
		Select Case oItem.TypeName
			Case "PresentationFolder"
				If (LCase(sFolderGuid) = LCase(oItem.ParentFolderGuid)) Then
					Set oFolder = oItem
					
					GetFolderByParentFolder = true
					Exit For
				End IF
		End Select
	Next
End Function

Function GetFolder(ByVal sFolderName, ByRef oFolder)
	Dim oItems, oItem
	GetFolder = false
	
	oItems = ASDK_ITEM_MANAGEMENT.GetItemsByName(sFolderName)
	For Each oItem in oItems
		' if there is more than one item with this name then you need to pick which one you want
		Select Case oItem.TypeName
			Case "PresentationFolder"
				GetFolder = true
				Set oFolder = oItem
		End Select
	Next
End Function

Function NS_GetItemByGuid(ByVal sItemGuid, ByRef oItem)
	On Error Resume Next
	NS_GetItemByGuid = false
	
	Err.Clear
	Set oItem = ASDK_ITEM_MANAGEMENT.GetItemByGuid("{" & sItemGuid & "}")
	If ((Err = 0) AND (NOT IsNothing(oItem))) Then NS_GetItemByGuid = true
End Function

Function IsNothing(ByRef oObject)
	IsNothing = false
	
	If TypeName(oObject) = "Nothing" Then IsNothing = true
End Function

SOLUTION
umibau's picture

thanks for the reply, i will look into code over weekend and then come back to post

umibau's picture

thanks the code really helped me understand the concept and get the code working.

great help.