cancel
Showing results for 
Search instead for 
Did you mean: 

RSA.Archer.Api DeleteContentAsync

KennethBlacow
Collaborator II

Hello all,

 

I am a newbie to C# and async tasks and the like. I can create a session and get content thanks to the RSA Charge presentation and labs. However, I am now ready to delete a ton, 4000+ records from my dev environment and I am having trouble building the right comment. I think I want to use DeleteContentAsync(), but I do not understand the parameters. I will show you my code below. Thanks for any help.

 

//This is my Program Class using hardcoded constants for the Username, Password, and Instance

static async Task MainAsync()
{
var restClient = new ArcherRestClient(BaseUrl);

var session = await restClient.LoginAsync(Username, Password, InstanceName);

var soapRequest = new AccessContent();

var content = soapRequest.ReadSyncReport(ModuleId, session.SessionToken, 1);


if(content.Count > 0)

foreach (int contentId in content)
{

Console.WriteLine(contentId); //This works to this line, I want to
//delete each record returned to
//contentId
//is this the correct Method?
var delete = await restClient.DeleteContentAsync()

}

Console.WriteLine("Logging Out Of Session");
var logout = await restClient.LogoutAsync(session.SessionToken, session);
Console.WriteLine($"Session: {logout}");

/************This is the separate class created to get Content.******************/

namespace TestingArcherAPI.ContentManager
{
public class AccessContent
{
//private static readonly searchSoapClient SoapClient = new searchSoapClient();
private static readonly edu.emory.service.archerdev.search Client = new search();

public List<int> ReadSyncReport(string ModuleId, string SessionToken, int PageNumber)
{
var soapResult = Client.SearchRecordsByReport(SessionToken, ModuleId, PageNumber);

Console.WriteLine(soapResult);

//var reportResult = SoapClient.SearchRecordsByReport(SessionToken, ModuleId, PageNumber); //SearchRecordsByReport(SessionToken, ReportId, PageNumber);
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(soapResult);
Console.WriteLine(soapResult);

List<int> ContentIds = new List<int>();

XmlNodeList nodeList = xmlDocument.GetElementsByTagName("Record");

foreach (XmlNode node in nodeList)
{
var contentId = node.Attributes["contentId"].Value;
ContentIds.Add(Convert.ToInt32(contentId));
Console.WriteLine($"Content Id is {contentId}");
}

return ContentIds;
}

}
}
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
1 ACCEPTED SOLUTION

Accepted Solutions

KennethBlacow
Collaborator II

I was overthinking the code. Here is what I used to delete records found in a report with the ID 10062. 

 

/*-------------------------------Needs using statment for ContentManager Class-------------------*/ 
private const string BaseUrl = "https://archerdev/rsaarcher/api/";
private const string Username = "apiuser";
private const string Password = "yourpassword";
private const string InstanceName = "Archer";
private const string ReportId = "10062";
static async Task MainAsync()
{
var restClient = new ArcherRestClient(BaseUrl);
var soapRequest = new AccessContent();

var session = await restClient.LoginAsync(Username, Password, InstanceName);

var content = soapRequest.ReadSyncReport(ReportId, session.SessionToken, 1);
if (content.Count > 0)
{
var i = content.Count;
foreach (int contentId in content)
{
var response = await restClient.DeleteContentAsync(contentId, session);
Console.WriteLine(i);
i = --i;
Console.WriteLine("------------------------------------------");
Console.WriteLine(i);
Console.WriteLine($"Content ID: {contentId} has been deleted");
Console.WriteLine("------------------------------------------");
}

}

//var sessionToken = session.SessionToken;
Console.WriteLine("Press Enter the Required Report ID from the list above.");

Console.WriteLine("Logging Out Of Session");
var logout = await restClient.LogoutAsync(session.SessionToken, session);
Console.WriteLine($"Session: {logout}");
Console.WriteLine();

WaitForSeconds(10);
}

/*-----------------------------Content Manager Class with Method ReadSyncReport------------------------------*/
public class ContentManager
{
//private static readonly searchSoapClient SoapClient = new searchSoapClient();
private static readonly edu.emory.service.archerdev.search Client = new search();

public List<int> ReadSyncReport(string ModuleId, string SessionToken, int PageNumber)
{
var soapResult = Client.SearchRecordsByReport(SessionToken, ModuleId, PageNumber);

var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(soapResult);

List<int> ContentIds = new List<int>();

XmlNodeList nodeList = xmlDocument.GetElementsByTagName("Record");
foreach (XmlNode node in nodeList)
{
var contentId = node.Attributes["contentId"].Value;
ContentIds.Add(Convert.ToInt32(contentId));
}

return ContentIds;
}

}
}

View solution in original post

2 REPLIES 2

KennethBlacow
Collaborator II

I was overthinking the code. Here is what I used to delete records found in a report with the ID 10062. 

 

/*-------------------------------Needs using statment for ContentManager Class-------------------*/ 
private const string BaseUrl = "https://archerdev/rsaarcher/api/";
private const string Username = "apiuser";
private const string Password = "yourpassword";
private const string InstanceName = "Archer";
private const string ReportId = "10062";
static async Task MainAsync()
{
var restClient = new ArcherRestClient(BaseUrl);
var soapRequest = new AccessContent();

var session = await restClient.LoginAsync(Username, Password, InstanceName);

var content = soapRequest.ReadSyncReport(ReportId, session.SessionToken, 1);
if (content.Count > 0)
{
var i = content.Count;
foreach (int contentId in content)
{
var response = await restClient.DeleteContentAsync(contentId, session);
Console.WriteLine(i);
i = --i;
Console.WriteLine("------------------------------------------");
Console.WriteLine(i);
Console.WriteLine($"Content ID: {contentId} has been deleted");
Console.WriteLine("------------------------------------------");
}

}

//var sessionToken = session.SessionToken;
Console.WriteLine("Press Enter the Required Report ID from the list above.");

Console.WriteLine("Logging Out Of Session");
var logout = await restClient.LogoutAsync(session.SessionToken, session);
Console.WriteLine($"Session: {logout}");
Console.WriteLine();

WaitForSeconds(10);
}

/*-----------------------------Content Manager Class with Method ReadSyncReport------------------------------*/
public class ContentManager
{
//private static readonly searchSoapClient SoapClient = new searchSoapClient();
private static readonly edu.emory.service.archerdev.search Client = new search();

public List<int> ReadSyncReport(string ModuleId, string SessionToken, int PageNumber)
{
var soapResult = Client.SearchRecordsByReport(SessionToken, ModuleId, PageNumber);

var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(soapResult);

List<int> ContentIds = new List<int>();

XmlNodeList nodeList = xmlDocument.GetElementsByTagName("Record");
foreach (XmlNode node in nodeList)
{
var contentId = node.Attributes["contentId"].Value;
ContentIds.Add(Convert.ToInt32(contentId));
}

return ContentIds;
}

}
}

BodieMinster
Archer Employee
Archer Employee

There is support in the platform API for bulk delete so that you don't have to loop through your contents. Use the URL

/platformapi/core/content/DeleteSelections. Make your call an HTTP POST but pass the X-Http-Method-Override header with a value of DELETE. Then pass your content IDs as List<int> in the request body like this: [674729, 674730]. You'll get back a response for each individual content you asked it to delete that will include validation messages for anything that failed.