5

I've the following XML in my Alchemy GUI extension:

<client>
  <Allowed_Publishing_Groups>
    <Group>Publish Ability</Group>
  </Allowed_Publishing_Groups>
  <Allowed_UnPublishing_Groups>
    <Group>Unpublish Ability</Group>
  </Allowed_UnPublishing_Groups>
  <Ignore_Rules_For_Administrators c:type="bool">false</Ignore_Rules_For_Administrators>
  <Ignore_Warnings c:type="bool">false</Ignore_Warnings>      
</client>

I'm wondering what's the best way to read the embedded <group> nodes, from within the Javascript as the documentation currently shows only the .net method?

1 Answer 1

3

You'll have to add a little more data to your elements so that the default deserialization of the client settings knows what to do with them. Assuming your <Allowed_Publishing_Groups> and <Allowed_UnPublishing_Groups> are multi valued string enumerables, as of 0.7 they'll need to look something like:

<Allowed_Publishing_Groups array="true" c:multi="true" c:element="Group">
  <Group>Publish Ability</Group>
</Allowed_Publishing_Groups>
<Allowed_UnPublishing_Groups array="true" c:multi="true" c:element="Group">
  <Group>Unpublish Ability</Group>
</Allowed_UnPublishing_Groups>

There's a slight detachment from the UI form and the serialization process, c:multi="true" tells the UI to make it a multi valued form element (with c:element determining what the name of the child elements should be), and array="true" tells the serializer that this should be a multi-valued array. From here, you should see the following in your JavaScript settings object.

for (var i = 0; i < settings.Allowed_Publishing_Groups.length; i++) {
    var group = settings.Allowed_Publishing_Groups[i];
}

Note that the current 0.7 default serializer turns the client settings into an ExpandoObject (which means all values will be treated as strings) which gets returned as a JSON object (and property names will remain as is to their xml element counter parts).

A better approach would be to use a strongly typed client object, which better controls the property types returned in the JSON object. The strongly typed client settings is described http://a4t-api.alchemywebstore.com/html/a155bb48-95ab-49c9-9b61-ebcc119dc2dd.htm#StronglyTypedClientSettings. In this case you wouldn't need the array="true" attribute.

public class MyClientSettings
{
    [XmlArrayItem("Group")]
    public List<string> Allowed_Publishing_Groups { get; set; }
    [XmlArrayItem("Group")]
    public List<string> Allowed_UnPublishing_Groups { get; set; }
    public bool Ignore_Rules_For_Administrators { get; set; }
    public bool Ignore_Warnings { get; set; }
}

And to ensure that the JavaScript getSettings() call will use this class to deserialize to, you'll have to update your class that extends the AlchemyPluginBase class.

public override void Configure(IPluginServiceLocator services)
{
    services.SettingsDeserialization.ClientSettingsType = typeof(MyClientSettings);
}

Note that this serialization just uses the standard .NET Xml Serialization, so for example if you did not like the underscores in your C# class' property names, you could use XmlElementAttribute with the ElementName property. You should also observe that with the strongly typed client settings, the return object will have the property names as camelcased.

Hope that helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.