Connecting to a WCF Service Using Business Connectivity Services in Office 2010

Summary: Learn how to create a Microsoft Business Connectivity Services (BCS) external content type for Microsoft Office 2010 that is based on a Windows Communication Foundation (WCF) service. (35 printed pages)

Applies to: Excel 2010 | Office 2007 | Office 2010 | Open XML | PowerPoint 2010 | SharePoint Designer 2010 | SharePoint Server 2010 | VBA | Word 2010

In this article
Overview of Creating and Using an External Content Type Based on a WCF Service
Using Visual Studio 2010 to Create a Service Application
Creating the Simulated Back-End Data Store
Adding a Reference to Microsoft.BusinessData to the Service Project
Adding Code to the WCF Service
Hosting the Service with IIS
Configuring the Service to Publish Its Metadata
Using SharePoint Designer to Create an External Content Type from the Service
Using the External Content Type in a Word Document Created from a SharePoint Document Library
Conclusion
Additional Resources

Published:  August 2010

Provided by:  Joel Krist, iSoftStone

Contents

  • Overview of Creating and Using an External Content Type Based on a WCF Service

  • Using Visual Studio 2010 to Create a Service Application

  • Creating the Simulated Back-End Data Store

  • Adding a Reference to Microsoft.BusinessData to the Service Project

  • Adding Code to the WCF Service

  • Hosting the Service with IIS

  • Configuring the Service to Publish Its Metadata

  • Using SharePoint Designer to Create an External Content Type from the Service

  • Using the External Content Type in a Word Document Created from a SharePoint Document Library

  • Conclusion

  • Additional Resources

Click to get code   Download the sample code: Connecting To WCF Service Using BCS

Click to view video     Watch the video

Overview of Creating and Using an External Content Type Based on a WCF Service

Microsoft Business Connectivity Services (BCS) in SharePoint 2010, provided with Microsoft Office 2010 and Microsoft SharePoint 2010, builds on the functionality that is provided by the Business Data Catalog included with Microsoft SharePoint Server to introduce new features such as external system write-back support and rich-client integration. Microsoft SharePoint Designer 2010 provides the External Content Type Designer that you can use to create Microsoft Business Connectivity Services (BCS) external content types based on Windows Communication Foundation (WCF) services. This walkthrough shows how to use SharePoint Designer to create a Business Connectivity Services external content type that connects to a WCF service. The service is created with Visual Studio 2010 and uses XLINQ to provide read/write access to data that is stored in a simple XML data file. The external content type is shown being used from external data content controls in a Microsoft Word document. The Word document is created from a SharePoint document library that exposes external data via an external data column.

This walkthrough provides steps and sample code that show how to use Visual Studio 2010 to create a Windows Communication Foundation (WCF) service, and then use SharePoint Designer 2010 to create a Business Connectivity Services external content type based on that service. The WCF service can perform read/write operations on an XML data file and implements methods that can be mapped to the Create, Read Item, Read List, Update, and Delete BCS stereotypes.

After creating the external content type, this walkthrough describes how to surface external data in a Word document by creating a SharePoint document library that uses an external data column that is based on the external content type. A Word document is created from the document library and external data content controls are used to display external data. For more information about other ways that external data can be displayed on the server and client, see Where Can You Show External Data?

Using Visual Studio 2010 to Create a Service Application

The following procedure describes how to use Microsoft Visual Studio to create a WCF service application.

To create a WCF service application by using Visual Studio 2010

  1. Start Microsoft Visual Studio 2010 as an administrator.

  2. On the Visual StudioFile menu, click New, and then click Project.

  3. In the New Project dialog box, select the Visual C# Web template type in the Installed Templates pane.

  4. Select the WCF Service Application template in the templates list.

  5. Name the project and solution CustomerService, and then select .NET Framework 3.5 as the target framework version. This is shown in Figure 1.

    Figure 1. Creating the solution

    Creating the solution

  6. Click OK. Visual Studio creates the solution and opens the Service1.svc.cs service code-behind file in the Code Editor.

  7. Use the Rename command on the Visual StudioRefactor menu to change the name of the Service1 class, as follows:

    1. In the Service1.svc.cs file, right-click the Service1 class name, click Refactor, and then click Rename, as shown in Figure 2.

      Figure 2. Selecting the Rename menu for Service1 class

      Selecting the Rename menu

    2. In the Rename dialog box, change the name of the Service1 class to WCFCustomer, clear the Preview reference changes check box, and then click OK. This is shown in Figure 3.

      Figure 3. Renaming the Service1 class

      Renaming the Service1 class

  8. Repeat the previous steps to change the name of the IService1 interface, as follows:

    1. In the Service1.svc.cs file, right-click the IService1 interface name on the WCFCustomer class definition, click Refactor, and then click Rename. This is shown in Figure 4.

      Figure 4. Selecting the Rename menu for IService1 interface

      Selecting the Rename menu

    2. In the Rename dialog box, change the name of the IService1 interface to IWCFCustomer, and then click OK. This is shown in Figure 5.

      Figure 5. Renaming the IService1 interface

      Renaming the IService1 interface

  9. In the Visual StudioSolution Explorer, rename the Service1.svc node to WCFCustomer.svc. Rename the IService1.cs file to IWCFCustomer.cs.

    Figure 6. Renaming Service1.svc and IService1.cs

    Renaming Service1.svc and IService1.cs

Creating the Simulated Back-End Data Store

In this walkthrough, you create an external content type based on a customer entity named WCFCustomer. The WCFCustomer entity is very simple and has three fields: ID, first name, and last name. To reduce complexity, you will use a simple XML data file to simulate a back-end data store for customer data. The following steps describe how to create the customer data file.

To create the simulated back-end data store

  1. Start your favorite XML editor and create a new file.

  2. Copy the following XML and paste it into the new file.

    <?xml version="1.0" encoding="utf-8"?>
    <Customers NextCustomerId="3">
      <Customer ID="0">
        <CustomerFName>Customer</CustomerFName>
        <CustomerLName>Zero</CustomerLName>
      </Customer>
      <Customer ID="1">
        <CustomerFName>Customer</CustomerFName>
        <CustomerLName>One</CustomerLName>
      </Customer>
      <Customer ID="2">
        <CustomerFName>Customer</CustomerFName>
        <CustomerLName>Two</CustomerLName>
      </Customer>
    </Customers>
    
  3. Save the new file named CustomerData.xml to the App_Data folder that is located under the CustomerService project folder.

    Figure 7. Creating the CustomerData file

    Creating the CustomerData file

Adding a Reference to Microsoft.BusinessData to the Service Project

The sample code provided with this walkthrough uses exceptions that are defined in the Microsoft.BusinessData.Runtime namespace provided by the Microsoft.BusinessData.dll assembly. Use the following steps to add a reference to Microsoft.BusinessData.

To add a reference to Microsoft.BusinessData

  1. In the Visual StudioSolution Explorer, select the CustomerService project.

  2. In Visual Studio, click Project, and then click Add Reference.

  3. In the Add Reference dialog box, click the Browse tab, and then navigate to the folder in the following path: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI.

  4. Scroll down until the Microsoft.BusinessData.dll assembly is visible, select the assembly, and then click OK. This is shown in Figure 8.

    Figure 8. Adding a reference to Microsoft.BusinessData

    Adding a reference to Microsoft.BusinessData

Adding Code to the WCF Service

The IWCFCustomer interface defines the service and data contracts supported by the service. The WCFCustomer class provides the implementation of the IWCFCustomer interface. The following steps describe how to implement the Finder, Specific Finder, Creator, Updater, and Deleter stereotypes that are required so that the service can be used to create an external content type.

To add code to the WCF service

  1. In the Visual StudioSolution Explorer, double-click the IWCFCustomer.cs file to open it in the Code Editor.

  2. Replace the contents of the IWCFCustomer.cs file with the following code.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using System.Text;
    
    namespace CustomerService
    {
      [ServiceContract]
      public interface IWCFCustomer
      {
        [OperationContract]
        IEnumerable<WCFCustomerType> ReadList();
    
        [OperationContract]
        WCFCustomerType ReadItem(int customerID);
    
        [OperationContract]
        WCFCustomerType Create(WCFCustomerType newCustomer);
    
        [OperationContract]
        void Update(WCFCustomerType customer);
    
        [OperationContract]
        void Delete(int customerID);
      }
    
      [DataContract]
      public class WCFCustomerType
      {
        Int32 customerID;
        string firstName;
        string lastName;
    
        [DataMember]
        public Int32 CustomerID
        {
            get { return customerID; }
            set { customerID = value; }
        }
    
        [DataMember]
        public string FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }
    
        [DataMember]
        public string LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }
      }
    }
    
  3. Save the changes to the IWCFCustomer.cs file.

  4. Select the WCFCustomer.svc.cs file in the Code Editor.

  5. Replace the contents of the WCFCustomer.cs file with the following code.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using System.Text;
    
    using System.Xml.Linq;
    using Microsoft.BusinessData.Runtime;
    
    namespace CustomerService
    {
      [ServiceBehavior(Namespace = "urn:CustomerService:WCFCustomer")] 
      public class WCFCustomer : IWCFCustomer
      {
        // Replace the <Path_To_Service_Project_Folder> placeholder
        // below with the actual path to the service project folder.
        private const string dataFilePath =
          @"<Path_To_Service_Project_Folder>\App_Data\Customers.xml";
    
        public IEnumerable<WCFCustomerType> ReadList()
        {
          try
          {
            var customers =
              XElement.Load(dataFilePath).Elements("Customer");
    
            var customersList =
              from cust in customers
              orderby (string)cust.Attribute("ID")
              select new WCFCustomerType
              {
                CustomerID = (Int32)cust.Attribute("ID"),
                FirstName = (string)cust.Element("CustomerFName"),
                LastName = (string)cust.Element("CustomerLName")
              };
    
            return customersList;
          }
          catch (Exception generalException)
          {
            throw new RuntimeException(
              "There was a problem reading customer data.",
              generalException);
          }
        }
    
        public WCFCustomerType ReadItem(int customerID)
        {
          try
          {
            var customers =
              XElement.Load(dataFilePath).Elements("Customer");
    
            var customerList =
              from cust in customers
              where (Int32)cust.Attribute("ID") == customerID
              select new WCFCustomerType
              {
                CustomerID = (Int32)cust.Attribute("ID"),
                FirstName = (string)cust.Element("CustomerFName"),
                LastName = (string)cust.Element("CustomerLName")
              };
    
            // The following will throw InvalidOperationException if the
            // customerNameElements collection is empty meaning a
            // customer with the specified ID does not exist.
            return customerList.First();
          }
          catch (InvalidOperationException)
          {
            throw new ObjectNotFoundException(
              "Unable to read data for the customer with ID = " +
              customerID.ToString() +
              ". The customer no longer exists.");
          }
          catch (Exception generalException)
          {
            throw new RuntimeException(
              "There was a problem reading customer data.",
              generalException);
          }
        }
    
        public WCFCustomerType Create(WCFCustomerType newCustomer)
        {
          try
          {
            XElement customers = XElement.Load(dataFilePath);
            Int32 nextCustomerId =
              (Int32)customers.Attribute("NextCustomerId");
    
            WCFCustomerType returnCustomer = new WCFCustomerType();
    
            returnCustomer.CustomerID = nextCustomerId;
            returnCustomer.FirstName = newCustomer.FirstName;
            returnCustomer.LastName = newCustomer.LastName;
    
            XElement newCustomerElement = new XElement("Customer");
            newCustomerElement.SetAttributeValue("ID",
              nextCustomerId);
    
            XElement newCustomerFNameElement =
              new XElement("CustomerFName", returnCustomer.FirstName);
    
            XElement newCustomerLNameElement =
              new XElement("CustomerLName", returnCustomer.LastName);
    
            newCustomerElement.Add(newCustomerFNameElement);
            newCustomerElement.Add(newCustomerLNameElement);
            customers.Add(newCustomerElement);
    
            customers.SetAttributeValue("NextCustomerId",
              nextCustomerId + 1);
            customers.Save(dataFilePath);
    
            return returnCustomer;
          }
          catch (Exception generalException)
          {
            throw new RuntimeException(
              "There was a problem creating a new customer.",
              generalException);
          }
        }
    
        public void Update(WCFCustomerType customer)
        {
          try
          {
            XElement customers = XElement.Load(dataFilePath);
    
            var customerFNameElements =
              from cust in customers.Elements("Customer")
              where (Int32)cust.Attribute("ID") == customer.CustomerID
              select cust.Element("CustomerFName");
    
            XElement customerFNameElement =
              customerFNameElements.First();
            customerFNameElement.SetValue(customer.FirstName);
    
            var customerLNameElements =
              from cust in customers.Elements("Customer")
              where (Int32)cust.Attribute("ID") == customer.CustomerID
              select cust.Element("CustomerLName");
    
            // The following will throw InvalidOperationException if
            // the customerNameElements collection is empty meaning a
            // customer with the specified ID does not exist.
            XElement customerLNameElement =
              customerLNameElements.First();
            customerLNameElement.SetValue(customer.LastName);
    
            customers.Save(dataFilePath);
          }
          catch (InvalidOperationException)
          {
            throw new ObjectNotFoundException(
              "Unable to update the customer with ID = " +
              customer.CustomerID.ToString() +
              ". The customer no longer exists.");
          }
          catch (Exception generalException)
          {
            throw new RuntimeException(
              "There was a problem updating the customer with ID = " +
              customer.CustomerID.ToString() + ".", generalException);
          }
        }
    
        public void Delete(int customerID)
        {
          try
          {
            XElement customers = XElement.Load(dataFilePath);
    
            var customerElements =
              from cust in customers.Elements("Customer")
              where (Int32)cust.Attribute("ID") == customerID
              select cust;
    
            // The following will throw InvalidOperationException if the
            // customerElements collection is empty meaning a customer
            // with the specified ID does not exist.
            XElement customer = customerElements.First();
            customer.Remove();
    
            customers.Save(dataFilePath);
          }
          catch (InvalidOperationException)
          {
            throw new ObjectNotFoundException(
              "Unable to delete the customer with ID = " +
              customerID.ToString() +
              ". The customer no longer exists.");
          }
          catch (Exception generalException)
          {
            throw new RuntimeException(
              "There was a problem deleting the customer with ID = " +
              customerID.ToString() + ".", generalException);
          }
        }    
      }
    }
    
  6. Save the changes to the WCFCustomer.cs file.

  7. Press F6 to build the service solution.

Hosting the Service with IIS

WCF services can be hosted in many ways. This walkthrough shows how to host the service by using Internet Information Services (IIS). For more information about hosting WCF services with IIS, see How to: Host a WCF Service in IIS and Deploying an Internet Information Services-Hosted WCF Service

To host the service with IIS

  1. Launch Internet Information Services (IIS) Manager.

  2. In the Connections pane of IIS Manager, expand the node for the server that will host the service. Right-click the Sites node for the server, and then click Add Web Site.

  3. In the Add Web Site dialog box, specify CustomerService for the Site name, enter the fully qualified path to the folder that contains the Visual Studio service project files for the Physical path, and specify an unused port number in the Binding settings. This is shown in Figure 9.

    Figure 9. Adding website to host the WCF service

    Adding website to host the WCF service

  4. To create the website, click OK.

  5. Grant Modify permission to the Network Service account on the App_Data folder that is located under the CustomerService project folder. This is required so that the WCF service can modify the contents of the customer data file.

Configuring the Service to Publish Its Metadata

The following steps describe how to configure the service so that it can publish the metadata that describes the operations and types it supports, enabling this information to be discovered by tools such as SharePoint Designer.

To configure the service to publish its metadata

  1. In Visual StudioSolution Explorer, double-click the web.config file to open it in the XML editor.

  2. Replace the existing <system.serviceModel> node in the web.config file with the following markup.

    <system.serviceModel>
      <services>
        <service behaviorConfiguration="CustomerServiceBehavior"
          name="CustomerService.WCFCustomer">
          <endpoint
            address="http://<Site>:<Port>/WCFCustomer.svc"
            binding="wsHttpBinding"
            contract="CustomerService.IWCFCustomer"
            bindingNamespace="urn:CustomerService:WCFCustomer" />
          <endpoint address="mex" binding="mexHttpBinding"
            contract="IMetadataExchange" />
        </service>
      </services>
      <behaviors>
        <serviceBehaviors>
          <behavior name="CustomerServiceBehavior">
            <serviceMetadata httpGetEnabled="true"/>
            <serviceDebug includeExceptionDetailInFaults="true"/>
          </behavior>
        </serviceBehaviors>
      </behaviors>
    </system.serviceModel>
    
  3. Replace the <Site> and <Port> placeholders in the markup with values that match the site created previously in IIS Manager, and then save the changes to the web.config file.

  4. Verify that the service is correctly hosted in IIS and that it can return the metadata that describes the operations and types it supports. Open the web browser and navigate to the service endpoint under the website created previously, appending the ?wsdl string to the end of the URL. For example:

    https://www.contoso.com:32003/WCFCustomer.svc?wsdl

    .The browser should display the WSDL for the service, as shown in Figure 10.

    Figure 10. WSDL for service hosted in IIS

    WSDL for service Hosted in IIS

Using SharePoint Designer to Create an External Content Type from the Service

In this section of the walkthrough, you create a connection to the WCF service, and define the supported operations for the external content type.

Creating a Connection to the WCF Service

Follow these steps to create a connection to the WCF service.

To create a connection to the WCF service

  1. Start SharePoint Designer 2010, and then open the SharePoint site where you want to store the artifacts for the external content type.

  2. In the left navigation pane, click External Content Types.

  3. In the contextual ribbon for entities, click External Content Type. This opens a new External Content Type page.

  4. Click the New External Content Type link next to the Name, and then type WCFCustomer.

  5. From Office Item Type, select Contact to enable external lists based on the external content type to be taken offline to Outlook. The offline list scenario is not addressed in this walkthrough, but specifying the Office Item Type enables the external content type to be mapped to the Outlook Contact object, making the offline scenario possible. Figure 11 shows the general settings for the WCFCustomer external content type.

    Figure 11. WCFCustomer external content type general settings

    WCFCustomer external content type settings

  6. Click the Click here to discover external data sources and define operations link next to the External System setting. The Operation Designer page is displayed, as shown in Figure 12.

    Figure 12. Operation Designer page

    Operation Designer page

  7. Click Add Connection. In the External Data Source Type Selection dialog box that opens (see Figure 13), click WCF Service in the Data Source Type drop-down list, and then click OK. This is shown in Figure 13.

    Figure 13. External Data Source Type Selection dialog box

    External data source type selection

  8. In the WCF Connection dialog box that opens, do the following:

    1. For the Service Metadata URL setting, type the URL of the service endpoint under the website created previously, appending the ?wsdl string to the end of the URL. For example, type https://www.contoso.com:32003/WCFCustomer.svc?wsdl.

    2. In the Metadata Connection Mode field, select WSDL.

    3. In the Service Endpoint URL field, type the service endpoint URL. For example, type https://www.contoso.com:32003/WCFCustomer.svc.

    4. In the Name field, type WCFCustomer.

      Figure 14 shows the completed WCF Connection dialog box.

      Figure 14. WCF Connection settings

      WCF Connection settings

  9. Click OK to create the new connection. SharePoint Designer validates the connection and then adds it to the Data Source Explorer in the Operation Designer page. Expand the nodes under the WCFCustomer data source to display the methods supported by the service, as shown in Figure 15.

    Figure 15. New connection in Data Source Explorer

    New connection in Data Source explorer

Defining Supported Operations for the External Content Type

Use the following steps to define supported operations for the external content type.

To define supported operations for the external content type

  1. Create a Create operation, as follows:

    1. Right-click the Create method in the Data Source Explorer, and then click New Create Operation on the context menu. This context menu displays all the operation stereotypes supported by SharePoint Designer.

    2. In the Create wizard that opens, click Next on the Operation Properties page to keep the default Operation Name and Operation Display Name values for the operation.

    3. On the Input Parameters Configuration page, expand the newCustomer node in the Data Source Elements group box, and deselect the CustomerID parameter. Doing this prevents the CustomerID parameter from being marked as a Creator field, which means that SharePoint will not render a control for the field on the new item form for external lists. This is required because users should not specify CustomerID values, as this field is managed by the back-end. The wizard displays an error referring to the requirement that at least one identifier be specified. Ignore this error for now. The requirement is taken care of in the next step. Figure 16 shows the Create operation input parameters.

      Figure 16. Create operation input parameters

      Create operation input parameters

    4. Click Next to continue to the Return Parameter Configuration page.

    5. On the Return Parameter Configuration page, expand the Create node in the Data Source Elements group box, and then select the CustomerID parameter.

    6. Select the Map to Identifier check box in the Properties group box. The error message disappears, as shown in Figure 17.

      Figure 17. Create operation return parameter

      Create operation return parameter

    7. Click Finish to complete the Create operation.

  2. Create a Read Item operation, as follows:

    1. Right-click the ReadItem method in the Data Source Explorer, and then click New Read Item Operation on the context menu.

    2. In the Read Item wizard that opens, click Next on the Operation Properties page to keep the default Operation Name and Operation Display Name values for the operation.

    3. On the Input Parameters Configuration page, notice that there is an error in the Error Messages group box about an identifier field for this external content type that should be specified. To do this, select the CustomerId field in the Data Source Elements group box, and then select the Map to Identifier check box in the Properties group box. This sets the CustomerID field as an identifier for this external content type, not only for this parameter. The error message disappears. Figure 18 shows the Read Item operation input parameters.

      Figure 18. Read Item operation input parameters

      Read Item operation input parameters

    4. Click Next to continue to the Return Parameter Configuration page.

    5. On the Return Parameter Configuration page expand the ReadItem node in the Data Source Elements group box.

    6. Click the CustomerID parameter in the Data Source Elements group box.

    7. Select the Map to Identifier check box in the Properties group box. Notice that field is automatically marked Read-Only.

    8. Map the FirstName field and LastName field to Office properties. Select the FirstName field in the Data Source Elements group box, and then select the First Name (FirstName) property from the drop-down list for the Office Property setting in the Properties group box. Repeat this process for the LastName field, mapping it to the Last Name (LastName) Office property. Figure 19 shows the Read Item operation return parameter.

      Figure 19. Read Item operation return parameter

      Read Item operation return parameter

    9. Click Finish to complete the creation of the Read Item operation.

  3. Create a Read List operation, as follows:

    1. Right-click the ReadList method in the Data Source Explorer, and then click New Read List Operation on the context menu.

    2. In the Read List wizard that opens, click Next on the Operation Properties page to keep the default Operation Name and Operation Display Name values for the operation.

    3. On the Input Parameters Configuration page, click Next to continue to the Return Parameter Configuration page.

    4. On the Return Parameter Configuration page, expand the ReadList node, and then in the Data Source Elements group box, expand the ReadListElement node in the Data Source Elements group box. Select the CustomerID parameter.

    5. Select the Map to Identifier check box in the Properties group box.

    6. Select the LastName field, and then select the Show In Picker check box to enable the LastName field to be displayed in the External Data Item Picker. Repeat this step for the FirstName field.

    7. Click Finish to complete the creation of the Read List operation.

  4. Create an Update operation.

    1. Right-click the Update method in the Data Source Explorer, and then click New Update Operation on the context menu.

    2. In the Update wizard that opens, click Next on the Operation Properties page to keep the default Operation Name and Operation Display Name values for the operation.

    3. On the Input Parameters page, expand the customer node in the Data Source Elements group box, and then select the CustomerID parameter.

    4. Select the Map to Identifier check box in the Properties group box.

    5. Click the Finish button to complete the creation of the Update operation.

  5. Create a Delete operation, as follows:

    1. Right-click the Delete method in the Data Source Explorer, and then click New Delete Operation on the context menu.

    2. In the Delete wizard that opens, click Next on the Operation Properties page to keep the default Operation Name and Operation Display Name values for the operation.

    3. On the Input Parameters page, select the customerID parameter.

    4. Select the Map to Identifier check box in the Properties group box.

    5. Click Finish to complete the creation of the Delete operation.

      Figure 20 shows the external content type with Create, Read Item, Read List, Update, and Delete operations created.

      Figure 20. External content type operations created

      External content type operations created

      Press CTRL+S to save the external content type to the Business Data Connectivity service (BDC) Metadata Store on the server running SharePoint Server.

Using the External Content Type in a Word Document Created from a SharePoint Document Library

The following steps show how to create a SharePoint document library that contains an external data column from the WCFCustomer external content type that you created previously. Finally, a Word 2010 document is created from the document library document template and external data from the WCFCustomer external content type is displayed in the document by using external data content controls.

Creating the SharePoint Document Library with External Data Column

Use the following steps to create the document library with the external data column.

To create the SharePoint document library with the External Data Column

  1. Browse to the SharePoint site that the WCFCustomer external content type was previously saved to.

  2. Click Site Actions, and then click View all site content.

  3. Click Create, and then on the Create page, select the Document Library type.

  4. Click More Options.

  5. Name the new document library WCFCustomer Documents and ensure that the Document Template setting is set to Microsoft Word document, as shown in Figure 21.

    Figure 21. Creating a document library

    Creating document library

  6. Click Create to create the document library.

  7. To add an external data column, click the Library tab on the Server ribbon, and then click Library Settings.

  8. On the Document Library Settings page, click Create Column in the Columns section.

  9. On the Create Column page that appears, type the name of the column as Customer, and then choose External Data as the column type. This displays the External Content Type Picker, as shown in Figure 22.

    Figure 22. External Content Type Picker

    External Content Type Picker

  10. Click the picker, and then select the WCFCustomer external content type from the External Content Type Picker dialog box, as shown in Figure 23.

    Figure 23. Selecting the WCFCustomer external content type

    Selecting WCFCustomer external content type

  11. Select the CustomerID field as the field to show in the column, and then select the FirstName field and LastName fields as additional fields to display in the document library. This is shown in Figure 24.

    Figure 24. Specifying fields

    Specifying fields

  12. Click OK to add the columns to the document library.

Displaying External Data in a Word Document

Use the following steps to display external data in a Word document.

To display external data in a Word document

  1. Navigate to the WCFCustomer Documents document library created previously.

  2. To add a new Word document to the document library, click the Documents tab on the Server ribbon, and then click New Document. When SharePoint displays a warning about opening the file, click OK. Microsoft Word 2010 starts and prompts for credentials to access the site. Type the required credentials.

    A new Word document is created and the document information panel that contains the external content type fields is displayed

  3. In Word, click the Insert tab on the ribbon.

  4. In the Text Ribbon group, click Quick Parts, and then click Document Property. In the Document Property flyout menu, the external data fields from the WCFCustomer external content type will be visible, as shown in Figure 25.

    Figure 25. External data fields

    External data fields

  5. Click the Customer field. Word adds an external data content control to the document. Add a space after the inserted content control, and then repeat the process for the Customer: FirstName field and Customer:LastName field, adding content controls to the document for each field.

  6. Select the Customer content control. Word displays the Select External Item picker. Click the picker and Word displays the Choose External Data form, as shown in Figure 26.

    Figure 26. Choose External Data form

    Choose External Data form

  7. Select one of the items from the list, and then click OK. Word fills the three content controls with the values of the customer ID, last name, and first name fields pulled from the back-end data file, and displays the values in the document information panel.

    Figure 27. External data in Word document

    External data in Word document

Conclusion

This walkthrough shows how to use Microsoft SharePoint Designer 2010 to create a Microsoft Business Connectivity Services (BCS) external content type that connects to a Windows Communication Foundation (WCF) service. The service is created with Microsoft Visual Studio 2010 and uses XLINQ to provide read/write access to data that is stored in a simple XML data file. The external content type is shown being used from external data content controls in a Microsoft Word document. The Word document is created from a SharePoint document library that exposes external data via an external data column.

For more information about other ways that external data can be displayed on the server and client, see Where Can You Show External Data?

Additional Resources

For more information, see the following resources: