Skip to main content

I am trying to figureout have to convert this C# cost into powershell to create Envirmnent Library Fields

http://moss-net4u.blogspot.com/2012/02/creating-k2-environment-fields-with.html

 

In what DLLs are these methods in?

Create a new EnvironmentSettingsManager Instance

Create a new EnvironmentTemplateCollection Instance

Create a new EnvironmentInstanceCollection Instance: 

Create new EnvironmentField instance by calling CreateFieldInstance()

 

The trick is to get a new instance of an Environment Field since is doesn’t have a new() constructor, you have to get a new instance by using the CreateFieldInstance() method of the EnvironmentFieldType class.

Steps:

  1. Create a new EnvironmentSettingsManager Instance and Connect to the K2 Server using the ConnectToServer() method of the EnvironmentSettingsManager instance.
  2. Create a new EnvironmentTemplateCollection Instance and a new EnvironmentInstanceCollection Instance.
  3. Select the Environment Template to use (e.g. Default Template) and select the Environment to use (e.g. Development)
  4. Get a handle on the EnvironmentFieldType you want to use by calling the EnvironmentSettingsManager.EnvironmentFieldTypes.GetItemByFriendlyName(“Miscellaneous Field”)
  5. Get a new EnvironmentField instance by calling CreateFieldInstance() method of the EnvironmentFieldType instance.
  6. Set the properties of the EnvironmentField instance
  7. Add the Field to the EnvironmentFieldCollection of the Environment Instance with the Add(newField) method of the EnvironmentFieldCollection
  8. Call the SaveUpdate() method of the EnvironmentField

Code:

EnvironmentInstance ei = new EnvironmentInstance();ei = eic.GetItemByName("Development");esm.ChangeEnvironment(ei.EnvironmentId);EnvironmentFieldType eft = esm.EnvironmentFieldTypes.GetItemByFriendlyName("Miscellaneous Field");EnvironmentField ef = (EnvironmentField)eft.CreateFieldInstance();ef.FieldName = "DeonTestField";ef.FieldDescription = "Deon Test Field";ef.DisplayName = "DeonTestField";ef.Value = "Some Value in Here";efc = new EnvironmentFieldCollection(ei);efc = ei.EnvironmentFields;efc.Add(ef);ef.SaveUpdate();

 

 

 

Hello,


 


You would need to reference SourceCode.EnvironmentSettings.Client which is located in bin.


 


Regards,


Matt


Reply