Listing 1: Code That Uses the SharePoint Server Client Object Model to Return BCS Data
private SPListItemCollection results;protected void Page_Load(object sender, EventArgs e)
{
SPQuery query = new SPQuery();
query.Query = "<OrderBy>" +
"<FieldRef Name='City' Ascending='True' />" +
"</OrderBy>";
results = SPContext.Current.Web.Lists\["Customers"].GetItems(query);
}
public override void RenderControl(HtmlTextWriter writer)
{
foreach (SPListItem spListItem in results)
{
writer.Write(spListItem\[0] + "<BR><BR>");
}
base.RenderControl(writer);
}
Listing 2: Code That Configures the BCS Model to use the Secure Store Service
<Property Type="System.String">WindowsCredentials</Property><Property Type="System.String">SqlServer</Property>
<Property Type="System.String">DEMO2010A</Property>
<Property Type="System.String">AdventureWorks</Property>
<Property Type="System.String">SSPI</Property>
<Property Type="System.String">True</Property>
<Property Type="System.String">CustApp</Property>
<Property
Type="System.String">Microsoft.Office.SecureStoreService.Server.SecureStoreProvider,
Microsoft.Office.SecureStoreService, Version=14.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c</Property>
Listing 3: Code That Uses the SharePoint ECMA Client Object Model to Return BCS Data
var customersListName = "Customers";var customersList = "Customers";
var output = "";
var customerListItem;
function GetCustomers() {
$("#MainContainer").empty();
this.ClientContext = SP.ClientContext.get_current();
var CamlQuery = new SP.CamlQuery();
CamlQuery.set_viewXml = "<View>" +
"<Query><OrderBy>" +
"<FieldRef Name='City' Ascending='True' />" +
"</OrderBy></Query>"
"</View>";
this.web = this.ClientContext.get_web();
this.site = this.ClientContext.get_site();
this.ClientContext.load(this.web);
this.ClientContext.load(this.site);
this.customersList = this.web.get_lists().getByTitle(customersListName);
this.customersListItems = this.customersList.getItems(CamlQuery);
this.ClientContext.load(this.customersListItems, "Include(Identifier1, " +
"FirstName, LastName, Address, City, State, PostalCode)");
this.ClientContext.executeQueryAsync(
Function.createDelegate(this, this.OnCustomersQuerySuccess),
Function.createDelegate(this, this.OnCustomersQueryFailed));
}
Listing 4: Code That Formats the Items Returned from an External List in a Visual Web Part
function OnCustomersQuerySuccess(sender, args) { if (this.customersListItems.get_count() > 0) {
var customersEnumerator = this.customersListItems.getEnumerator();
output = "";
while (customersEnumerator.moveNext()) {
customerListItem = customersEnumerator.get_current();
output += "<div class=\"customer\">";
output += "<div class=\"customerID\">Customer " +
customerListItem.get_item('Identifier1') +
"</div>";
output += "<div class=\"customerName\">" +
customerListItem.get_item('FirstName') + " " +
customerListItem.get_item('LastName') +
"</div>";
output += "<div class=\"customerAddress\">" +
customerListItem.get_item('Address') +
"</div>";
output += "<div class=\"customerAddress\">" +
customerListItem.get_item('City') + ", " +
customerListItem.get_item('State') + ", " +
customerListItem.get_item('PostalCode') +
"</div>";
output += "</div>";
}
$('.MainContainer').append(output);
}
else {
$('.MainContainer').append(output);
output += "<div class=\"result\">No customers found.</div>";
}
}
Listing 5: Code That Uses the SharePoint Silverlight Client Object Model to Return BCS Data
private void loadCustomers(){
List customersList = Common.ClientContext.Web.Lists.GetByTitle("Customers");
CamlQuery query = new Microsoft.SharePoint.Client.CamlQuery();
query.ViewXml = @"<View>" +
@"<Query><OrderBy>" +
@"<FieldRef Name='City' Ascending='True' />" +
@"</OrderBy></Query>" +
@"<ViewFields>" +
@"<FieldRef Name='Identifier1' />" +
@"<FieldRef Name='FirstName' />" +
@"<FieldRef Name='LastName' />" +
@"<FieldRef Name='Address' />" +
@"<FieldRef Name='City' />" +
@"<FieldRef Name='State' />" +
@"<FieldRef Name='PostalCode' />" +
@"<FieldRef Name='Country' />" +
@"<FieldRef Name='EmailAddress' />" +
@"<FieldRef Name='Phone' />" +
@"<FieldRef Name='ShippingCompany' />" +
@"<FieldRef Name='ShippingAccountNumber' />" +
@"</ViewFields>" +
@"</View>";
Common.Customers = customersList.GetItems(query);
Common.ClientContext.Load(Common.Customers);
Common.ClientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed);
statusTextBlock.Text += "Loading Customers . . .\n";
}
Listing 6: Code That Invokes a Delegate Method on the UI Thread to Process the Query Results
private void onQuerySucceeded(object sender, ClientRequestSucceededEventArgs args)
{
UpdateUIMethod updateUI = DisplayCustomerInfo;
this.Dispatcher.BeginInvoke(updateUI);
}
Listing 7: Code That Formats the Items Returned from an External List in a Silverlight Application
private void DisplayCustomerInfo()
{
customersListBox.ItemsSource = null;
// Create a generic list of type Customer to hold the customers.
List<Customer> customersList = new List<Customer>();
// Populate the generic list of type Customer with customers.
foreach (ListItem item in Common.Customers)
{
Customer customer = new Customer();
customer.ID = item\["Identifier1"].ToString();
customer.FirstName = item\["FirstName"].ToString();
customer.LastName = item\["LastName"].ToString();
customer.Address = item\["Address"].ToString();
customer.City = item\["City"].ToString();
customer.State = item\["State"].ToString();
customer.PostalCode = item\["PostalCode"].ToString();
customer.Country = item\["Country"].ToString();
customer.EmailAddress = item\["EmailAddress"].ToString();
customer.Phone = item\["Phone"].ToString();
customer.ShippingCompany = item\["ShippingCompany"].ToString();
customer.ShippingAccountNumber = item\["ShippingAccountNumber"].ToString();
customersList.Add(customer);
}
// Bind the list of customers to the list box.
customersListBox.ItemsSource = customersList;
statusTextBlock.Text += "Customers Loaded\n";
}
Listing 8: Code That Uses the BCS Object Model to Return BCS Data
public DataTable GetCustomers(){
// Get the Customer entity from the metadata catalog.
IEntity entity = catalog.GetEntity(BdcEntityNameSpace, "Entity1");
// Get the filters defined on the default Finder method for the entity.
IFilterCollection filters = entity.GetDefaultFinderFilters();
// Return the filtered list of items from the external data source.
IEntityInstanceEnumerator enumerator = entity.FindFiltered(filters, lobSystemInstance);
// Convert the filtered list of items to a DataTable and return it.
return entity.Catalog.Helper.CreateDataTable(enumerator);
}
Listing 9: Code That Executes the SpecificFinder Method with the BCS Object Model and Formats the Results
private string GetCustomer(string customerID){
Identity identity = new Identity(customerID);
IEntity entity = catalog.GetEntity(BdcEntityNameSpace, "Entity1");
IEntityInstance instance = entity.FindSpecific(identity, lobSystemInstance);
string output = "<div class=\"customer\">";
output += "<div class=\"customerID\">Customer " +
instance\["Identifier1"].ToString() +
"</div>";
output += "<div class=\"customerName\">" +
instance\["FirstName"].ToString() + " " +
instance\["LastName"].ToString() +
"</div>";
output += "<div class=\"customerAddress\">" +
instance\["Address"].ToString() +
"</div>";
output += "<div class=\"customerAddress\">" +
instance\["City"].ToString() + ", " +
instance\["State"].ToString() + ", " +
instance\["PostalCode"].ToString() +
"</div>";
output += "</div>";
return output;
}