using System;
using System.Collections.Generic;
using System.Text;
using IRSearch.irwebservice;
using System.Xml;
namespace SearchSample
{
class Program
{
static string _username = "sysadmin";
static string _password = "pass";
static string _wsURL = "http://docsrv/srv.asmx";
static int _foundItemCount = 0;
static void Main(string[] args)
{
srv ir = null;
string ticket = "";
try
{
writestatus("------------------------------------------------------------");
writestatus("Connecting to infoRouter Server...");
ir = new srv();
ir.Url = _wsURL;
writestatus("Connected.");
writestatus("Authenticating...");
XmlNode xResponse = ir.AuthenticateUser(_username, _password);
if (getXMLAttributeValue(xResponse, "success", "").ToUpperInvariant() != "TRUE")
{
writestatus("Authencatication failed!");
writestatus("Server Response:" + getXMLAttributeValue(xResponse, "error", ""));
return;
}
ticket = getXMLAttributeValue(xResponse, "ticket", "");
writestatus("Authenticated.");
writestatus("Searching...");
if (!prepareSearch(ir, ticket))
{
return;
}
_foundItemCount = 0;
bool lastPage = false;
while (lastPage == false)
{
nextPage(ir, ticket, out lastPage);
}
writestatus("------------------------------------------------------------");
writestatus("Found item count: " + _foundItemCount.ToString());
}
catch (Exception ex)
{
writestatus("ERROR:" + ex.Message);
writestatus("DETAILS:" + ex.ToString());
}
finally
{
ir = null;
Console.ReadKey();
}
return;
}
static bool prepareSearch(srv ir, string ticket)
{
string FOLDER = "/Corporate/Letters/";
string qry = "<SEARCH>" +
"<ITEM NAME=\"SEARCHSCOPE\" VALUE=\"ONLINE\" />" +
"<ITEM NAME=\"FOLDER\" VALUE=\"" + xmlEncode(FOLDER) + "\" />" +
"<ITEM NAME=\"SEARCHFOR\" VALUE=\"DOCUMENTSONLY\" />" +
"<ITEM NAME=\"PROPERTYSETNAME\" VALUE=\"" + xmlEncode("ADDRESS") + "\" >" +
" <ATTR NAME=\"" + xmlEncode("ADDRESSTYPE") + "\" OPERATOR=\"EQ\" VALUE=\"" + xmlEncode("HOME") + "\" />" +
" <ATTR NAME=\"" + xmlEncode("STATE") + "\" OPERATOR=\"EQ\" VALUE=\"" + xmlEncode("NY") + "\" />" +
"</ITEM>" +
"</SEARCH>";
XmlNode xResponse = ir.Search(ticket, qry, "DOCUMENTNAME", true);
if (getXMLAttributeValue(xResponse, "success", "").ToUpperInvariant() == "TRUE")
{
writestatus("Search prepared.");
return true;
}
else
{
writestatus("Search cannot be prepared.");
writestatus("Server Response:" + getXMLAttributeValue(xResponse, "error", ""));
return false;
}
}
static bool nextPage(srv ir, string ticket, out bool lastpage)
{
lastpage = true;
XmlNode xResponse = ir.GetNextSearchPage(ticket, false, false, false, false, false);
if (getXMLAttributeValue(xResponse, "success", "").ToUpperInvariant() != "TRUE")
{
writestatus("Retrive failed..");
writestatus("Server Response:" + getXMLAttributeValue(xResponse, "error", ""));
return false;
}
lastpage = (getXMLAttributeValue(xResponse, "LastPage", "").ToUpperInvariant() == "TRUE");
foreach (XmlElement xmlitem in xResponse)
{
if (xmlitem.Name.ToUpperInvariant() == "DOCUMENT")
{
_foundItemCount++;
string docpath = getXMLAttributeValue(xmlitem, "Path", "");
string docname = getXMLAttributeValue(xmlitem, "Name", "");
int docid = Convert.ToInt32(getXMLAttributeValue(xmlitem, "DocumentID", "0"));
writestatus(docid.ToString().PadRight(10) + docpath.Replace("\\","/") + "/" + docname);
}
}
xResponse = null;
return true;
}
static void writestatus(string xStatus)
{
Console.WriteLine(xStatus);
return;
}
static string xmlEncode(string xmlstr)
{
xmlstr = xmlstr.Replace("&", "&");
xmlstr = xmlstr.Replace("<", "<");
xmlstr = xmlstr.Replace(">", ">");
xmlstr = xmlstr.Replace("'", "'");
xmlstr = xmlstr.Replace("\"", """);
return xmlstr;
}
static string getXMLAttributeValue(XmlNode xNode, string AttrName, string defaultvalue)
{
if (xNode == null) { return defaultvalue; }
if (xNode.Attributes[AttrName] == null) { return defaultvalue; }
string xValue = xNode.Attributes[AttrName].Value;
if (xValue == null) { return defaultvalue; }
xValue = xValue.Trim();
if (xValue.Length == 0) { return defaultvalue; }
return xValue;
}
}
}
|