SilverlightIntrawebPart1


We start off by creating a new Intraweb application. Drop a TSQLConnection component on the ServerController and add the following code.

function TIWServerController.GetCustomerData(ASearch: string): string;
var
  query: TSQLQuery;
  data: TStringList;
  param: TParam;
begin
  query := TSQLQuery.Create(nil);
  try
    query.SQLConnection := SQLConnection1;
    query.SQL.Add('select FirstName,LastName,EmailAddress from Person.Contact where FirstName like :FirstName');
    query.Params.Clear;
    param := TParam.Create(query.Params, ptInput);
    try
      query.Params[0].Name := 'FirstName';
      query.Params[0].Value := '%' + ASearch + '%';
      query.Open;
      data := TStringList.Create;
      try
        data.Add('<?xml version="1.0" encoding="utf-8" ?>');
        data.Add('<CustomerList>');
        while not query.Eof do begin
          data.Add('<Customer>');
          data.Add('<FirstName>' + query.FieldByName('FirstName').AsString + '</FirstName>');
          data.Add('<LastName>' + query.FieldByName('LastName').AsString + '</LastName>');
          data.Add('<EmailAddress>' + query.FieldByName('EmailAddress').AsString + '</EmailAddress>');
          data.Add('</Customer>');
          query.Next;
        end;
        data.Add('</CustomerList>');
        Result := data.Text;
        query.Close;
      finally
        FreeAndNil(data);
      end;
    finally
      FreeAndNil(param);
    end;
  finally
    FreeAndNil(query);
  end;
end;

To add REST capabilities to our server, all we need to do is intercept the requests and process them as needed. In our case, we only have one function (GetCustomerData), so I've simplified the verification logic in the OnBeforeDispatch

procedure TIWServerController.IWServerControllerBaseBeforeDispatch(
  Sender: TObject; Request: TWebRequest; Response: TWebResponse;
  var Handled: Boolean);
begin
  if AnsiPos('/SERVICES/CUSTOMERDATA', UpperCase(Request.PathInfo)) > 0 then begin
    Response.Content := GetCustomerData(Request.QueryFields.Values['search']);
    Response.ContentType := 'text/xml';
    Response.SendResponse;
    Handled := True;
  end;
end;


In the next section we'll see how to display the data using Silverlight