7827041

C#.NET solution for using the samanage API

Discussion created by 7827041 on Oct 22, 2015

My company had a situation where we needed to load several hundred computers to samanage that are not currently being used. The information is stored in Excel files and SQL server tables, and the data in those locations needed to be massaged before they could be loaded (you will get 422 errors if, for example, you set the location in your xml for a location that does not exist in your samanage solution.

I am a .NET programmer, so .NET is my preferred solution. However, I could not find any .NET examples in the samanage community, so I created the solutions, and the sample code is below.

You will need to set references to the following objects:

using System.Net.Http;

using System.Net.Http.Headers;

 

First, I wanted to get all hardware we already have so I can run comparisons.

string baseurl = "https://yourcompany.samanage.com/hardwares.xml";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(baseurl);
request.Method = "GET";
request.Accept = "application/vnd.samanage.v1.1+xml";
request.Credentials = new NetworkCredential("yourname@yourcompany.com", "yourpassword");

var response = request.GetResponse();

I'm just going to write the results to a text box for this example

string text;

 

                using (var sr = new StreamReader(response.GetResponseStream()))

 

 

 

                {

 

                       text = sr.ReadToEnd();

 

 

                       textBox1.Text = text.Replace("\n", System.Environment.NewLine);

 

}

 

 

 

Now I am going to post a computer, using XML:

string xmlString  = "<hardware><name>pcnumber1</name><bio><manufacturer>Dell</manufacturer><ssn>123456</model></bio><category><name>Workstation</name></category><site><name>New York, NY</name></site><status><name>Broken</name></status></hardware>"

string baseurl = "https://yourcompany.samanage.com/hardwares.xml";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(baseurl);
request.ContentType = "application/xml";
request.Method = "POST";
request.Accept = "application/vnd.samanage.v1.1+xml";
request.Credentials = new NetworkCredential("yourname@yourcompany.com", "yourpassword");
               

Convert the xml string to a byteArray


Stream postDataStream = new System.IO.MemoryStream();
byte[] xmlBytes = System.Text.Encoding.UTF8.GetBytes(xmlString);
postDataStream.Write(xmlBytes, 0, xmlBytes.Length);

request.ContentLength = postDataStream.Length;
Stream reqStream = request.GetRequestStream();

postDataStream.Position = 0;

byte[] buffer = new byte[1024];
int bytesRead = 0;

while ((bytesRead = postDataStream.Read(buffer, 0, buffer.Length)) != 0)
{
      reqStream.Write(buffer, 0, bytesRead);
}

 

The xml has been posted, now get the response and write it to the textbox

var response = request.GetResponse();
string text;

using (var sr = new StreamReader(response.GetResponseStream()))
{
    text = sr.ReadToEnd();
    textBox1.Text = text.Replace("\n", System.Environment.NewLine);
}

Outcomes