I managed to get all Port Groups in a Sandard switch but the list includes the Distributed Port Groups from the Distributed vSwitch that it is included inside the Standard switch structure. I don't want this in my case.
I am using VMware vSphere SDK (5.5) and I tried this one:
public List<Network> GetPortGroups(VimClient vimClient, Datacenter selectedDC = null, string pgName = null)
{
List<Network> lstPortGroups = new List<Network>();
NameValueCollection pgFilter = new NameValueCollection();
ManagedObjectReference DcMoRef = new ManagedObjectReference();
if (pgName != null)
{
pgFilter.Add("name", pgName);
}
else
{
pgFilter = null;
}
if (selectedDC != null)
{
DcMoRef = selectedDC.MoRef;
}
else
{
DcMoRef = null;
}
List<EntityViewBase> appPortGroups = vimClient.FindEntityViews(typeof(Network), DcMoRef, pgFilter, null);
if (appPortGroups != null)
{
foreach (EntityViewBase appPortGroup in appPortGroups)
{
Network thisPortGroup = (Network)appPortGroup;
lstPortGroups.Add(thisPortGroup);
}
return lstPortGroups;
}
else
{
return null;
}
}