2 Replies Latest reply on Mar 22, 2018 12:18 PM by norman_soriano

    Adding Element with ElementTemplate

    norman_soriano

      Hi to all!

       

      I am just a newbie in PI Web API. I just want to ask how can I add new Elements with Element template? I am trying to make it with my code below but i always get

      The remote server returned an error: (409) Conflict. error.

       

      public JsonResult CreateElement()

              {

                  var url = "https://" + serverName + "/piwebapi/elements/" + GetReportsWebID() + "/elements";

                  string postData = "{ 'Name': 'Report 2','Description': 'System Insert Report','TemplateName': 'Report Template','CategoryNames': ['Report Components']}";

                  int statusCode = -1;

       

                  MakePostRequest(url, postData, out statusCode);

                  return Json(statusCode, JsonRequestBehavior.AllowGet);

              }

       

       

      internal static void MakePostRequest(string url, string postData, out int statusCode)

              {

                  ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

                  WebRequest request = WebRequest.Create(url);

       

       

                  //Kerberos Authentication Only

                  //request.UseDefaultCredentials = true;

       

       

                  //Basic and Kerberos Authentication

                  request.Credentials = new NetworkCredential(userName, password);

       

                  ((HttpWebRequest)request).UserAgent = ".NET Framework Example Client";

       

                  //The method is "GET" by default.

                  request.Method = "POST";

       

                  //If the content-type is not specified as application/json, your post request won't work.

                  request.ContentType = "application/json";

       

                  //The content length should be present on the request header.

                  byte[] byteArray = Encoding.UTF8.GetBytes(postData);

                  request.ContentLength = byteArray.Length;

                  Stream dataStream = request.GetRequestStream();

                  dataStream.Write(byteArray, 0, byteArray.Length);

                  dataStream.Close();

       

                  //Get the response to make sure the value was sent.

                  WebResponse response = request.GetResponse();

       

                  //The StatusCode property is available only if the WebRequest object is converted to HttpWebRequest

                  //The StatusCode will confirm that the operation was executed successfully.

                  statusCode = Convert.ToInt32(((System.Net.HttpWebResponse)(response)).StatusCode);

              }

       

      Hoping for your positive response.

      Thank you very Much!