VMware {code} Community
johndemic
Contributor
Contributor

Finding a Folder by Name using SOAP

Hey All,

I'm attempting to find the managed object id of a folder by its name and root folder. I suspect I have to use the FindByInventoryPath method from the WSDL, but I'm struggling getting a proper message together. I was hoping someone could validate this is the correct approach and perhaps give a pointer to an example message.

Thanks!

-jd

0 Kudos
3 Replies
stumpr
Virtuoso
Virtuoso

I would probably not use FindByInventoryPath in your case. Sounds like you want to use the folder name, such as "Discovered Virtual Machines" and locate the ManagedObjectReference. By RootFolder do you mean the parent? There's a distinct RootFolder object in the Inventory tree.

I would just navigate from the root folder using a recursive function call. I was asked to post up a perl script that does sort of the opposite (recursively walks up a tree to find a Host's datacenter object. You can modify it to work the other way: from rootFolder, recursively navigate all children checking for name == <desired folder>, and then returning that managed object. From that managed object you can get it's parent folder, or walk up until you get to the rootFolder. Save the last folder before the rootFolder and that will be what I think you want in terms of a "root folder" (the top level folder). Give me a few, I'll post up a perl example.

Reuben Stump | http://www.virtuin.com | @ReubenStump
0 Kudos
stumpr
Virtuoso
Virtuoso

Looks like work is going to keep me busy this afternoon. Look into the childEntity and parent properties. If you only care about the parent of your desired folder, you can simply do a RetrieveProperties to get the MOR with a filter for the name. Then get the parent's name property with another RetrieveProperty call. If you want the full path to your desired folder, you'll have to walk down the inventory tree and save the relevant path parts, or you could probably do a RetrieveProperties call for all folders in the inventory, and then build a tree in your code logic.

I'll try to be a bit more helpful later, or perhaps someone else in the forums can hop in.

Reuben Stump | http://www.virtuin.com | @ReubenStump
0 Kudos
johndemic
Contributor
Contributor

Thanks for the quick response Stumpr,

I want to avoid doing recursive SOAP calls if possible. I found the following workaround, which is a bit of a hack, but works. The "find folder" functionality I'm implementing needs to ultimately result in a valid folder reference (ie, if the folder isn't found then I need to create it.) As such, I found I can invoke the CreateFolder operation and use the same XPath expression to infer the folder's name regardless if the operation succeeded (a new folder was created) or failed (the folder already exists.) The XPath expression is "//*[@type="Folder"]/text()" in case someone else is interested. This works on the assumption that the SOAP response or SOAP fault will contain only one element with a type attribute set to "Folder", which seems to be the case.

Cheers,

-jd

0 Kudos