ENTERPRISE

Sharepoint 2010 : Creating Security Trimmed CRUD Operations on a SQL Database Using Visual Studio 2010

11/13/2012 3:02:26 AM
1. Connecting Model to Data Source

To begin with, create a new BDC Model project and give it a proper name. Here it is named BdcCustomersModel. To make it easy to use the data obtained from the Customers database, the best way is to add a LINQ to SQL model. LINQ is not the fastest implementation method performance-wise, so if performance is critical, you might prefer to implement a dedicated data adaptor instead.

  1. Select the project by left-clicking it.

  2. Click Add New Item on the projects menu to open the Add New Item dialog.

  3. Select Data templates from the Installed Templates panel.

  4. Choose the LINQ to SQL Classes project type from the Templates panel.

  5. Give the project a proper name—here it is called "Customer"—and then click Add.

  6. Open the Server Explorer, and add a connection to the Customers database.

  7. Drag Customers tableand drop it on the Customer.dbml design surface.



At this point, a designer class named CustomerDataContext is automatically added. To allow a connection to be created using a custom connection string, a new class should be added. Name the class CustomerDataContext.cs. Make this class a partial class. Then pass the connection string to the base class through the constructor, as in Listing 1.

Example 1. Data Context for Connecting to a Database with a Custom Connection String
public partial class CustomerDataContext
{
    private const string ConnectionString = @"Data Source=localhost\SQLEXPRESS;Initial
Catalog=CustomersDatabase;Integrated Security=True;Pooling=False";

public CustomerDataContext() :
        base(ConnectionString, mappingSource)
    {
        OnCreated();
    }
}

NOTE

We made the connection string a constant in the code for exemplifying it. In a production environment, it should be added to the Web.Config file in encrypted format. When using your own database, the connection string should be modified to match your database and credential requirements.

2. Mapping BDC Model to Data Source

At this point, the BDC model should be mapped to the data source. This involves making a number of entities and specifying appropriate methods. The purpose is to create the interpretation layer between the database and the BDC model. First an entity with an identifier key needs to be created:

  1. An entity named "Entity1" is automatically created. Delete it.

  2. Create a new entity. This can be done using "Drag and Drop" on the Entity from Toolbox and dropping it on the design surface.

  3. Change the default name of the entity to "Customer."

  4. Create a new identifier named, CustomerKey, on the entity "Customer." This is most easily done by right-clicking the entity and selecting the Add Identifier option. This adds a new identifier to the entity.

  5. Give the identifier the name "CustomerKey".

To add functionality, a number of method instances must be added to the entity. This is most easily done by selecting the entity and clicking the <Add a Method> button that appears in the Method Details panel. Create a Specific Finder method on the entity. This will add the ReadItem, as shown in Figure 1.

Figure 1. Specific Finder method

As shown in the Method Details panel, the ReadItem method has two parameters, namely an In parameter, which takes the identifier key, and a Return parameter, which is an object instance of the identity type. VS2010 offers some functionality for making it easy to create new methods by copying type descriptors automatically when possible. Therefore it is a good idea to configure those for the Specific Finder method before adding the other methods.

To complete the identifier key configuration, the type descriptor for the return parameter named CustomerKey should be added.

  1. Open the Method Details panel.

  2. Choose the <Edit> command from the type descriptor menu named CustomerKey.

  3. In the BDC Explorer, add a type descriptor by right-clicking the CustomerKey, as shown in Figure 2, and choose the Add Type Descriptor option. This will create a new type descriptor.

    Figure 2. Adding type descriptors
  4. Rename the just-created type descriptor to "CustomerKey", using the Properties panel.

  5. Change the Identifier property to CustomerKey, as in Figure 3. This is how the BCS runtime knows that this type descriptor maps to the CustomerKey identifier.

  6. Change the Type Name property to match the type from the LINQ model. In this example, it is not required to change it.

  7. Repeat steps 1–6 for all required type descriptors.

Figure 3. Configuring type descriptors

When all type descriptors are added as in steps 1 through 7, the type descriptors shown in Figure 4 should be visible. It is always a good idea to check the spelling and Type Name properties at this point, as updating them later on can be a pain. Refactoring does not currently support this.

Figure 4. All type descriptors configured

At this point, the other methods available need to be created the same as the ReadItem (Specific Finder) method. These are the methods that support the BDC operations that are the CRUD operations. To do this, repeat the steps in this section for each of the following methods: ReadList, Create, Update, and Delete. Also counting the ReadItem method, a total of five methods should be defined for the entity named Customer. It is, however, much easier to create the last four methods, as the type descriptors of the return parameters are automatically defined the same way as with the ReadItem method. The BDC Designer automatically applies type descriptors defined in the other methods of an entity and copies them to the newly created methods.

With the required type descriptors in place for the methods, the LOB system–qualified type name of the type descriptor Customer should be defined. This is done by selecting the Customer type descriptor in the BDC Explorer panel. In the Properties panel, its value should be changed from System.String to BdcCustomer.Customer, BdcModel1. This is now the underlying data type of the data structure that the Customer type descriptor returns.

3. Adding Code-Behind to Access External Data Source

To implement the logic required for performing the actual CRUD operations, the method body of the methods in the CustomerService.cs code file should be changed to match the code in Listing 2.

Example 2. Implementation of CRUD Operations in the BDC Method Instances
public static Customer ReadItem(string customersKey)
 {
     CustomerDataContext context = new CustomerDataContext();
     Customer cust = context.Customers.Single(c => c.CustomerKey == customersKey);
     return cust;
 }

 public static Customer Create(Customer newCustomer)
 {
     CustomerDataContext context = new CustomerDataContext();
     context.Customers.InsertOnSubmit(newCustomer); context.SubmitChanges();
     Customer cust= context.Customers.Single(c => c.CustomerKey ==newCustomer.CustomerKey);
     return cust;
 }

 public static void Delete(string customersKey)
 {
     CustomerDataContext context = new CustomerDataContext();
     Customer cust = context.Customers.Single(c => c.CustomerKey == customersKey);
     context.Customers.DeleteOnSubmit(cust);
     context.SubmitChanges();
 }

					  

public static IEnumerable<Customer> ReadList()
 {
     CustomerDataContext context = new CustomerDataContext();
     IEnumerable<Customer> custList = context.Customers;
     return custList;
 }
 public static void Update(Customer customer)
 {
     CustomerDataContext context = new CustomerDataContext();
     Customer cust = context.Customers.Single(c => c.CustomerKey == customer.CustomerKey);
     cust.CustomerKey = customer.CustomerKey;
     cust.CompanyName = customer.CompanyName;
     cust.ContactPerson = customer.ContactPerson;
     cust.EmailAddress = customer.EmailAddress;
     cust.PhoneNumber = customer.PhoneNumber;
     context.SubmitChanges();
 }					  
Other  
 
Top 10
Extending LINQ to Objects : Writing a Single Element Operator (part 2) - Building the RandomElement Operator
Extending LINQ to Objects : Writing a Single Element Operator (part 1) - Building Our Own Last Operator
3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
3 Tips for Maintaining Your Cell Phone Battery (part 1) - Charge Smart
OPEL MERIVA : Making a grand entrance
FORD MONDEO 2.0 ECOBOOST : Modern Mondeo
BMW 650i COUPE : Sexy retooling of BMW's 6-series
BMW 120d; M135i - Finely tuned
PHP Tutorials : Storing Images in MySQL with PHP (part 2) - Creating the HTML, Inserting the Image into MySQL
PHP Tutorials : Storing Images in MySQL with PHP (part 1) - Why store binary files in MySQL using PHP?
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
VIDEO TUTORIAL
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
Popular Tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS