using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
//Add references to the .NET My Services specific namespaces
using Microsoft.Hs.ServiceLocator; //referenced from the ServiceLocator project
using Microsoft.Hs; //referenced from the HsSoapExtension project
using AddressBook.glengatest; //namespace generated by the Add Web Reference tool
namespace AddressBook
{
///
/// This is the class for the default AddressBook webpage
///
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox serviceUrl;
protected System.Web.UI.WebControls.TextBox userName;
protected System.Web.UI.WebControls.Label msgStatus;
protected System.Web.UI.WebControls.Table contactTable;
protected System.Web.UI.WebControls.Label lblStatusMessage;
protected System.Web.UI.WebControls.Label lblUrl;
protected System.Web.UI.WebControls.Label lblUserName;
protected System.Web.UI.WebControls.Button getService;
private myContacts GetService()
{
//This method creates an instance of the ServiceLocator object with a
//reference to the service URL from the Web form
//in this example, SOAP requests are logged to c:\temp\hs_soaplog.txt
ServiceLocator srvLoc = new ServiceLocator(serviceUrl.Text,"c:\\temp\\hs_soaplog.txt",true);
//try accessing the myContacts service with the supplied username
//(future version of .NET My Services will require the passing of credential as well)
try
{
myContacts myC = (myContacts)srvLoc.GetService(typeof(myContacts), userName.Text);
return myC;
}
catch (Exception ex)
{
StatusMessage("Unable to connect to the requested service." +
" Please verify the service URL and username.");
//StatusMessage(ex.ToString())
}
return null;
}
private void QueryContacts()
{
//This method creates the query request and extracts data from the response object
//get the service connection
myContacts myC = GetService();
//if nothing is returned then quit
if(myC==null) return;
//build the XPath query request
//create the request object
queryRequestType qRequest = new queryRequestType();
//instatiate new xpQuery
xpQueryType myQuery = new xpQueryType();
//this XPath statement returns all m:contacts elements
myQuery.select = "/m:myContacts/m:contacts";
//Add myQuery to a new query array
qRequest.xpQuery = new xpQueryType[]{myQuery};
//execute the query and place response in a new response object
queryResponseType qResponse = myC.query(qRequest);
if(qResponse==null) //checks if response object was returned
{
StatusMessage(" A query response was not received from the service ");
}
else if(qResponse.xpQueryResponse[0].Items==null) // checks if any contacts exist
{
StatusMessage(" There are no contacts in your .NET Contacts service ");
}
else //load contact items into an HTML table on the page
{
contactTable.Visible=true;
//extracts response data from the items collection of the response object
//do once for each contact returned
foreach(Object obj in qResponse.xpQueryResponse[0].Items)
{
myNameType name = (myNameType) obj;
addressType address = (addressType) obj;
telephoneNumberType phone = (telephoneNumberType) obj;
TableRow row= new TableRow();
TableCell cell= new TableCell();
//load name columns
cell.Text = name.surname.ToString();
row.Cells.Add(cell);
cell.Text = name.givenName.ToString();
row.Cells.Add(cell);
//load address column data
cell.Text = address.street.ToString();
row.Cells.Add(cell);
cell.Text = address.primaryCity.ToString();
row.Cells.Add(cell);
cell.Text = address.subdivision.ToString();
row.Cells.Add(cell);
cell.Text = address.postalCode.ToString();
row.Cells.Add(cell);
//load address column data
cell.Text = address.street.ToString();
row.Cells.Add(cell);
contactTable.Rows.Add(row);
}
}
}
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.getService.Click += new System.EventHandler(this.getService_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void getService_Click(object sender, System.EventArgs e)
{
msgStatus.Text ="";
StatusMessage("Loading contacts from .NET My Services... ");
QueryContacts();
}
private void StatusMessage(string message)
{
//used to display relevant messages in the statusMessage text control
msgStatus.Text = message;
}
}
}