5 Replies Latest reply on Oct 29, 2015 9:50 AM by alexe RSS
    alexe Apprentice

    Set Metadata via API

    Is there a way to set an entity's metadata via the API, or some service I'm overlooking?

     

    I'm asking specifically so I can set the units on a property from a separate service. I can access a properties units via GetInstanceMetadataAsJSON, but I can't find any way to set this data.

      • Re: Set Metadata via API
        jasong Creator

        Well, yes. it's doable but I wouldn't recommend it. But Composer is doing this. You can figure out how to do this by watching your network traffic when hitting the SAVE button on a Thing/ThingTemplate/ThingShape...whatever.

        So you could use PutJSON to localhost/Thingworx/ENTITYTYPE/ENTITYNAME....

         

         

        A better option and the one I would take would be to RemovePropertyDefinition and then AddPropertyDefinition. I think, due to the way that ThWx holds data in memory, that any values on live things will be there as long as you do it quickly. If you take 1 day or a server reboot between the Remove and the Add, then the data will be lost. This appears to be true in my past trials, but I don't know if it will be true forever. Also. it appears that AddPropewrtyDefinition does not take a Units parameter. So that's a dead end. Bummer.

         

        It would be nice if ThWx could provide us a service EdiPropertyDefinition() but so far we don't have it.

        • Re: Set Metadata via API
          ckulak Apprentice

          I'm not sure what you mean by API, but from Java you can do it like this:

           

          Thing thing = ...;

          thing.getInstancePropertyDefinition("myPropertyName").getAspects().setStringAspect("units", "kg");

          ThingManager.getInstance().updateEntity(thing, "Changed units to kg", true);

           

          Or you need to do it from JavaScript?

          • Re: Set Metadata via API
            alexe Apprentice

            Thanks guys.

             

            The PutJSON works, its a little quirky, but for anyone else trying this out the easiest method is:

             

            GetJSON //call this first to get the JSON  in correct format, I tried some others (ReadEntityAsJSON) that gave me trouble

            Edit your JSON values

            Update 'lastModifiedDate' (usually resides in your JSON at yourObject.lastModifiedDate)

            PutJSON

             

            Just as a reference, the Delete and quick Re-AddPropertyDefinition does appear to preserve live thing values of that property. I'm doing that elsewhere, and if AddPropertyDefinition had a units parameter I would use it there. Certainly easier and less dangerous than the JSON manipulation. I put in a request to have units added to that service though.

             

            And thanks for the Java solution as well. I was looking to do it in javascript or some REST call, but the Java might be helpful in the future.

              • Re: Set Metadata via API
                jasong Creator

                Alex, since this is doable in Java we can add that service ourselves but only to templates that we control. Or as a tool on a Resource. So if they don't do it we can, but, lets at least let them try first.

              • Re: Set Metadata via API
                alexe Apprentice

                As a further update here for anyone trying this, especially when trying to set units or other property metadata, the URL I'm setting in the PutJSON is formatted as such:

                 

                localhost/Thingworx/ThingShapes/'+Shape+'?reason=properties%20%3A%20'+PropertyName+'%20%3A%20Updated%0A'

                 

                However, as expected, if the entity is open for edit in composer, the units will not be set. Annoyingly, the call will return a "200 ok" http response no matter what the actual outcome was. To force the update of the aspects append '&forceUpdate=1' to the end of the URL.


                Of course there are reasons you may not want to force update, but in my situation it seems fine for now. Just thought I'd share.