6 Replies Latest reply on Apr 17, 2015 8:05 AM by salte

    How to set configuration attribute value using PI web API




      I trying to work out how to set the value of a configuration item attribute from a C# client using the PI web API.


      I can successfully set the value for an attribute which has a PI Point data reference using this code:


      var client = new HttpClient(...) //more stuff to initalize http client
      var webId = "theActualWebId";
      HttpContent cont = new StringContent("{ \"Timestamp\":\"*\", \"Value\":1500 }", Encoding.UTF8, "application/json");
      var request = string.Format(@"streams/{0}/value", webId );
      var response = client.PostAsync(request, cont).Result;


      But when I use the same code for setting a configuration attribute value the HttpResponseMessage.IsSuccessStatusCode will be false, and the .ReasonPhrase property will be "Conflict".

      I've also tried changing the two last lines of code to:


      var request = string.Format(@"attributes/{0}/value", webId );
      var response = client.PutAsync(request, cont).Result;


      but the reponse is the same.


      The actual value of the configuration attribute is 500 with the timestamp "1970-01-01T00:00:00Z".


      Is there an obvious reason why this is failing that I'm not getting?

        • Re: How to set configuration attribute value using PI web API
          Eugene Lee



          Try removing the timestamp part of the code since it doesn't accept the timestamp parameter. I have tested without it and it works.

          • Re: How to set configuration attribute value using PI web API

            Hi Sigmund,


            If you are trying to set the value of a configuration attribute that has no data reference, use the PUT method with attributes/{WebId}/value as you have specified in your 2nd example. The POST method for streams is used for attributes with a data reference or non-configuration item attributes.


            Are you getting a HTTP response code of 409 (Conflict)? Usually, this means the request could not be completed due to a conflict with the current state of the resource. Some troubleshooting steps that you can consider:

            • Can you check the response body to see if there is any additional information?
            • Does this issue happen to multiple attributes or only a specific attribute?
            • Can you access the current value of the same attribute using https://{PI Web API server name}/piwebapi/attributes/{WebId}/value? Let's make sure the WebId is valid.
            • Have you tried using any other tools to write to the same attribute? Some example include REST clients or PowerShell.

            A sample powershell script that you can use for testing:


            $method = "PUT"
            $uri = "https://{PI Web API server name}/piwebapi/attributes/{WebId}/value"
            $contenttype = "application/json"
            $body = @"
              "Value": 25.0,
            Invoke-RestMethod -Method $method -Uri $uri -UseDefaultCredentials -Body $body -ContentType $contenttype
            • Re: How to set configuration attribute value using PI web API
              Mike Zboray

              You can inspect the response's Content as well, it should have more information on the cause of the error.


              I would look at the attribute/element in PI System Explorer. Is the element checked out to someone else? If it is, you will not be able to change the value for the attribute since changing a configuration item requires a checkout of the element.


              I tried this and received a Conflict response code in this situation.


              You can't set the value timestamp of non-data reference attributes that are typically used as configuration items. The value's timestamp is the element version's effective time.

              • Re: How to set configuration attribute value using PI web API

                Thanks for all your helpful response!. After working on a different issue for a while I've finally had a chance to have a look at the various suggestions. I got it working in the end, but I'm pretty sure the final solution is euqal (in code) to stuff that I've already tried before, so I guess there must have been som conflict related to the element/attribute. (Either that or possibly I didn't try the PUT + no timetimestamp in request body combination).


                I've marked Mikes answer as the correct, as I assume that his post was most related to the actual problem.


                Now I have a different issue (on a very similar subject), but I'll post that as a new thread...


                For anyone wondering, here is the code I used in the end:


                HttpContent requestBody = new StringContent("{ \"Value\":" + value + " }", Encoding.UTF8, "application/json");

                var request = string.Format(@"attributes/{0}/value", attributeWebId);

                var response = _piClient.PutAsync(request, requestBody).Result;