Hi,
I've seen the document which describes how you can get your users' responses to your surveys using the new export APIs. Examples are included in both Python and Java.
https://api.qualtrics.com/guides/docs/Guides/Common%20Tasks/getting-survey-responses-via-the-new-export-apis.md
Has anyone translated this to C# code for me to use in SSIS for the Getting Survey Responses Using New Export APIs?
Thanks!
Getting Survey Responses Using New Export APIs Sample in C# for SSIS
Did you get to work with SSIS to generate survey responses out of Qualtrics?
Hello,
I am also looking for the same. I want to integrate C# into an package. does anyone have an example of this?
Thanks,
Ryan
Here is c# code that works
//step 1 - Start the export [ steps are followed as per Qualtrics guidelines for V3 API]
try
{
var httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
httpWebRequest.Headers["X-API-TOKEN"] = api_Token;
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.ServicePoint.Expect100Continue = false;
System.IO.StreamWriter streamWriter = null;
System.IO.StreamReader streamReader = null;
using (streamWriter = new System.IO.StreamWriter(httpWebRequest.GetRequestStream()))
{
jsonInput = String.Format(@"{{ ""format"": ""xml"",""useLabels"":{0},""filterId"":""{1}""}}", useLabels.ToLowerInvariant(), surveyFilter); // filter is created in Qualtrics survey and used here
streamWriter.Write(jsonInput);
}
var httpResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
streamReader = new System.IO.StreamReader(httpResponse.GetResponseStream());
var responseText = streamReader.ReadToEnd();
var results = JObject.Parse(responseText); //JSON deserialize object will need an object model. So, used JObject for this result retreival
var rstCode = (string)results["meta"]["httpStatus"];
if (rstCode == "200 - OK")
{
exportID = (string)results["result"]["progressId"];
}
httpResponse.Dispose();
streamReader.Close();
streamReader.Dispose();
//step-2 Wait for the export to complete
//url = url + "/" + exportID;
httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url + "/" + exportID);
httpWebRequest.Headers["X-API-TOKEN"] = api_Token;
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "GET";
httpWebRequest.ServicePoint.Expect100Continue = false;
streamReader = null;
int counter = 0;
System.Threading.Thread.Sleep(3000); // Qualtrics requires a time delay for this next call
while (true)
{
httpResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
streamReader = new System.IO.StreamReader(httpResponse.GetResponseStream());
responseText = streamReader.ReadToEnd();
if (responseText == "" && counter < 10)
{
System.Threading.Thread.Sleep(2000);
continue;
}
results = JObject.Parse(responseText);
rstCode = (string)results["meta"]["httpStatus"];
if (rstCode == "200 - OK")
{
if ((int)results["result"]["percentComplete"] == 100)
{
fileUrl = (string)results["result"]["fileId"];
break;
}
}
else
{
counter++;
//email error
if (counter == 10)
{
throw new Exception("Url tried for 10 times.");
}
else
{
System.Threading.Thread.Sleep(2000);
}
}
}
httpResponse.Dispose();
streamReader.Close();
streamReader.Dispose();
//step-3 Download the exported file
//url = url + "/" + fileUrl;
httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url + "/" + fileUrl + "/file");
httpWebRequest.Headers["X-API-TOKEN"] = api_Token;
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "GET";
httpWebRequest.ServicePoint.Expect100Continue = false;
httpResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
// Step 4: Extract exported file
ZipArchive archive = new ZipArchive(httpResponse.GetResponseStream());
foreach (ZipArchiveEntry entry in archive.Entries)
{
Stream s = entry.Open();
var sr = new StreamReader(s);
finalResponse = sr.ReadToEnd();
}
archive.Dispose();
httpResponse.Dispose();
streamReader.Close();
streamReader.Dispose();
}
catch (Exception ex)
{
Environment.Exit(0);
}
Leave a Reply
Enter your username or e-mail address. We'll send you an e-mail with instructions to reset your password.