Update a property value from an Umbraco Workspace Context that doesn't currently have a value

I have a custom workspace and I am trying to update the value of some of the properties (only in the client, hence not the data via the API’s )

Currently I am able to consume the context of UMB_DOCUMENT_WORKSPACE_CONTEXT and then update the value of any of the properties that currently have a value, but I cannot for the life of me work out how to update the values of any property that does not have a value, as it seems that we are only able to update the items that have some data already.

this.consumeContext(UMB_DOCUMENT_WORKSPACE_CONTEXT, (document) => {
   this.#documentWorkspaceContext = document;
   this.observe(this.#documentWorkspaceContext.data, (data) => {
   if (data) {
   console.log('Document data:', data);
   this.#currentDocumentData = data;
   this.updatePropertyValue('heading', 'Just a little demo of text being updated');
   }
});
updatePropertyValue(propertyAlias: string, newValue: any) {
   if (!this.#documentWorkspaceContext) return false;
   
   if (this.#currentDocumentData?.values.find(p => p.alias === propertyAlias)) {
      if (this.#documentWorkspaceContext.setPropertyValue) {
         this.#documentWorkspaceContext.setPropertyValue(propertyAlias, newValue);
         return true;
      }
   }
   else {
      console.log('Property not found: ' + propertyAlias);
      return false;
   }
}

If I just try and update one of the properties that does not currently have a value using setPropertyValue it just fails.

Does anyone have any idea if this is possible, if not, I think it should be :slight_smile:

This is a long stretch (and I’m guessing at this point), but when you have a custom property editor, you need to dispatch a change event. Maybe you need to do the same here?

this.dispatchEvent(new UmbPropertyValueChangeEvent());
1 Like

Hi @LuukPeters thank you for your reply, it wasn’t actually what I needed.

I had a message from @Nielslyngsoe, which helped me solve this one :slight_smile:

For those interested, using the UMB_DOCUMENT_WORKSPACE_CONTEXT only gives you access to properties that already have a value, you cannot add other properties using this context.

However using the UmbContentWorkspaceContext the same method setPropertyValue exists and DOES allow you to add a value to all of the properties, this method optionally has a variantId if you are using this on a multi-lingual site, it seems on a site that is not configured to be multi-lingual, it is not needed.

E.g.

this.#contentWorkspaceContext?.setPropertyValue(alias, value);