You can also define external content types and external lists through Visual Studio. As you will see Visual Studio provides you complete control over your BCS entities.
Try It Yourself: Create an External Content Type and List in Visual Studio
Now try creating an external content type and list through Visual Studio 2010. Get data from the ContactType table in the AdventureWorks database. Follow these steps to create an external content type and external list in Visual Studio.
1. Create a new empty SharePoint Project named as BCSDemo.
2. In the next screen select the Deploy as a Farm Solution option and enter the appropriate site URL.
3. Add a new Item of type Business Data Connectivity Model and name it BdcContactType as shown in Figure 14.12. Note the name is still Business Data Connectivity Model and not Business Connectivity Services Model. Unfortunately, although the Business Data Connectivity is renamed to Business Connectivity Services, there are still a lot of artifacts for Business Connectivity Services that are using Business Data Connectivity in their names. To avoid confusion any references to Business Data Connectivity in SharePoint 2007 will be explicitly called out.
4. You see a new item named BdcContactType added to the solution. In addition you see a couple of classes added to the solution, and a feature named Feature1 is added. Rename the feature to BdcContactType. The project structure looks as shown in Figure 14.13.
5. The BdcContactType.bdcm is an XML file representing your BCS Model. An entity named Entity1 is automatically created for you by Visual Studio. You can see and design this entity in the SharePoint BDC Designer in the left section of Visual Studio. This is shown in Figure 14.14.
6. Entity1.cs defines a class that contains the properties for Entity1, which the Entity1Service.cs defines as the various methods such as ReadItem and ReadList. All these are linked together through the BdcContactType.bdcm file. You can verify this by opening the file in a text editor.
The ReadItem returns a single instance of the entity and is equivalent to the SpecificFinder method in BDC 2007. As the name indicates, ReadList returns multiple items.
7. In the BDC Designer select the Entity1 entity, and in the properties window change the name from Entity1 to ContactType as shown in Figure 14.15.
8. Notice that the Entity1Service class gets automatically renamed to ContactTypeService.
9. Manually rename the Entity1.cs file to ContactType.cs and edit the file as shown here:
using System;
namespace BCSDemo.BdcContactType
{
public partial class ContactType
{
public int ContactTypeID { get; set; }
public string Name { get; set; }
public DateTime ModifiedDate { get; set; }
}
}
10. Now go back to the BDC Designer and select Identifier1. In the properties window change the Name to ContactTypeID and Type Name to System.Int32 as shown in Figure 14.16.
11. Now open the BDC Explorer window by going to View, Other Windows, BDC Explorer. Expand the ReadList node. Select the Entity1list Node under the @returnParameter. Figure 14.17 shows the BDC Explorer.
The BDC Explorer and the BDC Method Details windows allow you to edit the various configurations for your entity such as the methods and their parameters. You will find yourself using these windows extensively when working with BCS in Visual Studio 2010.
12. Change the Name to ContactTypeList. Select the drop-down list of Type Name and from the Current Project select ContactType. Keep the Is Enumerable check box selected. This is shown in Figure 14.18.
13. After setting the values the properties window should look similar to Figure 14.19.
14. Right-click and delete the Entity1 node below the ContactTypeList node. Right-click on the ContactTypeList node and click Add Type Descriptor. In the properties window change the Name to ContactType and Type Name to ContactType class.
15. Add the Type descriptors below the ContactType Node as shown in Table 14.1.
The ContactType Node below the ContactTypeList node defines the structure for the ContactTypeList collection. If you don’t add the ContactType type descriptor you get an error indicating “TypeDescriptor with name ‘ContactTypeList’ is defined as a collection but does not have a child TypeDescriptor. Collection TypeDescriptors must define a single child TypeDescriptor, representing the structure of each element of the collection.”
16. Now Expand the ReadItem node in the BDC Explorer. Select the @Identifier Node under the ID node and change the Name to ContactTypeID and Type Name to System.Int32.
17. Select the Entity1 node under the @returnParameter node and change the Name to ContactType and select the ContactType as the Type Name.
18. Delete the Identifier1 and Message Nodes. Add the Type Descriptors below the ContactType Node as shown in Table 14.2.
19. The final structure should look as shown in Figure 14.20.
20. Modify the ReadItem and ReadList methods so that you can retrieve the data into the entities from the AdventureWorks database.
21. Edit the code of the ContactTypeService.cs as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace BCSDemo.BdcContactType
{
/// <summary>
/// All the methods for retrieving, updating
///and deleting data are implemented in this class file.
/// The samples below show the finder and specific finder method for Entity1.
/// </summary>
public class ContactTypeService
{
private const string ConnectionString =
"data source=.;initial catalog=AdventureWorks;Integrated Security=SSPI";
public static ContactType ReadItem(int id)
{
SqlConnection conn = new SqlConnection(ConnectionString);
try
{
string query =
" select * from Person.ContactType where ContactTypeID = @ContactTypeId";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@ContactTypeId", id);
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.Read())
{
ContactType contactType = new ContactType();
contactType.ContactTypeID =
int.Parse(rdr["ContactTypeID"].ToString());
contactType.Name = rdr["Name"].ToString();
contactType.ModifiedDate =
DateTime.Parse(rdr["ModifiedDate"].ToString());
return contactType;
}
}
finally
{
if (conn != null)
{
conn.Close();
}
}
return null;
}
public static IEnumerable<ContactType> ReadList()
{
List<ContactType> list = new List<ContactType>();
SqlConnection conn = new SqlConnection(ConnectionString);
try
{
string query = "select * from Person.ContactType";
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
ContactType contactType = new ContactType();
contactType.ContactTypeID =
int.Parse(rdr["ContactTypeID"].ToString());
contactType.Name = rdr["Name"].ToString();
contactType.ModifiedDate =
DateTime.Parse(rdr["ModifiedDate"].ToString());
list.Add(contactType);
}
}
finally
{
if (conn != null)
{
conn.Close();
}
}
return list; ;
}
}
}
22. Build and deploy the solution.
23. Browse to Central Administration and go to Application Management, Manage Service Applications. Go to the administration page of the Business Data Connectivity Service. You should see the ContactType content type listed there. Set the permissions on the ContactType for the appropriate users.
24. Browse to your site and create an external list based on the new content type.
25. You should see the external data in your new external list (Figure 14.21).