Using. Net assembly as a business data connectivity data source (1)

Source: Internet
Author: User
Tags knowledge base visual studio 2010

The most powerful BCS in SharePoint 2010 is the ability to use. net.ProgramSet as a data source. This dll class library is called the assembly system. The business data directory (BDC) can directly connect to the SQL server data source, but when we want to do something more complex (for example, we need to get data from two different data sources and use it together, previously, we had to build a web service layer for implementation. Now, you can directly use the. Net assembly and no longer need to build auxiliary Web Services. Of course, if you prefer the WCF Service, it is also a good solution.

Visual Studio 2010 has a set of built-in BCS modeling tools, including the designer,CodeCompile and debug tools.

Next, let's take a look at how to use Visual Studio 2010 to build a. Net assembly for BCS.

1. Open Visual Studio 2010 and create a new project. Select the business data connectivity model template under SharePoint 2010 project type.

2. After the project is created, you can see an object on the model design interface.

In addition, the bdcmodel1 folder contains two classes: entity1.cs and entity1service. CS. We will not touch these default items so that they can be used as a reference when building our own custom BCS entities.

 

3. In this drill, we still use the adventureworks2000 sample database.

4. We will create a department table mapped to the database. Drag an entity shape from the Visual Studio 2010 toolbox to the BCS model design interface.

5. You will notice that when you create a blank BCS object, you will also create an entityservice. CS file for you. BCS will put all the C # methods related to the object here.
Rename our object to -- Department

Right-click the object's methods section and add a new method named getments ments.

Note that after we change the object name to department, entityservice. CS is automatically renamed to departmentservice. CS. If you open departmentservice. CS, you will see that the created method is also in it.

 

6. Now, we start to write some code to retrieve the required data. Right-click the project and add a LINQ 2 SQL class. Connect to the adventureworks2000 database through the server resource manager in the open LINQ 2 SQL view, and drag and drop the departments table to the design interface.

7. In this way, we can use the automatically generated LINQ 2 SQL Department class to return the data we need. Here, I found that it is better to define a department class under our BCS model folder. It is mainly based on the following considerations:

-- You need to define all columns returned by BCS. The class of the LINQ 2 SQL statement may return some attributes that we do not need to use in the BCS model. If you directly use the class of LINQ 2 SQL, which contains some class attributes that are not defined in the BCS model, an error is thrown.

-- In this way, you can also obtain data from two different sources. A single object is displayed.

-- Besides, I feel that using my own type is easier and more comfortable :) this will also help you do unit tests in the future and keep the code clear.

Therefore, add a new C # class to the bdcmodel1 folder, name it department. CS, and add attributes for each column in the data that you want to use in BCS later.

Public   Class Department
{
Public Int16 extends mentid { Get ; Set ;}
Public   String Name { Get ; Set ;}
Public   String Groupname { Get ; Set ;}
}

8. Next, open the departmentservice. CS file, and change the getdocumments method declaration:

Public static ienumerable <Department> getdepartments ()

 

9. Write the LINQ 2 SQL code to obtain and replace the data.

Throw new system. notimplementedexception ();

Is

Departmentdatacontext DB =   New Departmentdatacontext ( " Server = localhost; database = adventureworks2000; uid = ***; Pwd = *** " );
Return From d In DB. analytics
Select New Department
{
Departmentid = D. initialize mentid,
Name = D. Name,
Groupname = D. groupname
};

 

10. Next, we need to enable our BCS model to recognize the data returned by our method. Return to the BCS Model View and right-click the identifiers Section of the Department object. Add a new identifier and name it inclumentid. When you select this identifier in your object, you can see more information from the property panel. Change its type to system. int16.

11. Before describing our data for the BCS model, we need to create a response parameter. On the BDC method details panel, click <Add a parameter> In the parameters drop-down box to create a new parameter.

Set the direction of this parameter to return.

Next, some work needs to be moved back and forth between different panels. However, I personally think this is the most appropriate way to build the BCS model.

 

12. Go back to the BDC explorer view. Expand the tree and find our entities and methods. Select the typedescriptor we created when we just added the parameter. After being elected, you will find that We can edit the attribute value on the property panel. Set its attributes:

Name: departmentlist
Type name:
System. Collections. Generic. ienumerable '1 [[bdcmodelproject1.bdcmodel1. Department, bdcmodel1]

Iscollection: True

 

13. Right-click the departmentlist node and select Add type descriptor.

14. Select the newly added type descriptor and set its attribute value:

Name: Department
Type name: bdcmodelproject1.bdcmodel1. Department, bdcmodel1

Iscollection: false

 

15. Next we will create a type descriptor for each column to be returned from the department table. Right-click the Department type descriptor and add the following three typedescriptors:

 

Name: Required mentid
Type name: system. int16
Identifier: specified mentid

Name: Name

Type name: system. String

Name: groupname

Type name: system. String

 

The BDC explorer tree should look like this:

16. Then, we need to create a methodinstance element, which describes the type of our method (such as (finder, specificfinder) for BCS ).

Click the Department object to view its BDC details panel. Select "create finder instance" from the methodinstance drop-down box in the instance section ".

Select the instance you just created and set its attribute value:

Default: True
Default display name: Department List
Return Parameter Name: Parameter

Return type descriptor name: departmentlist

 

17. Open departmentservice. CS before deploying our model. After the preceding steps for creating the BCS model, the second getparameters () method may be generated. If yes, you only need to delete it. (I am not sure which step of the repeated method is generated. I will update the content after finding it)

 

18. Finally, we can compile our project. If no problem occurs, run F5 directly. During the process, it is automatically packaged into a WSP package and deployed to SharePoint.

 

It is our BCS external data displayed through the business data list Web component in my SharePoint site.

Note:: When the error "Access denied by service data connection" occurs, refer to the Microsoft Knowledge Base.Article982271.

 

In the next series of articles, we will learn:

-Create a specificfinder and idenumerator method.

-Create a method for writing back and updating data

-Create an association method

And so on.

 

References

Business data connectivity model-finder Method

BCS shims-. Net assembly as a data source for business connectivity services

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.