In this series of previous articles, we created the BCS model in Microsoft Visual Studio 2010 and defined the finder, specific finder, and Id enumerator methods for our department entities. This article will continue to expand our BCS model to see how to define the creator and Updater methods for the department entity so that users can insert/edit records in the department entity.
First, create our Creator method.
1) Right-click the Methods Section of the Department object and select "Add new method ".
2) Name "adddepartment" for the method ".
Next, we will create parameters for the creator method. You need to define all fields that you want to appear in the "New Project" dialog box as input parameters of the Creator method. This method also requires a response parameter and should be a struct that contains the identifier class information of the Department object.
3) Select the adddepartment method in the department object, and then click "<Add a parameter>" in the BDC method details panel"
4) in this example, we will create three parameters with the direction of "in", namely "name", "groupname", and "dimension mentid ".
5) Next we need to modify the type descriptor attribute of each parameter. Expand the Tree View in the BDC Explorer window and find the type descriptor of these parameters.
The attributes of these type descriptor are set:
Departmentid:
Name: Required mentid
Typename: system. int16
Identifier: specified mentid
Groupname:
Name: groupname
Typename: system. String
Creatorfield: True
Name:
Name: Name
Typename: system. String
Creatorfield: True
6) Now we need to define the return parameters for the adddepartment method. The returned parameter should be a struct that describes the identifier of the Department object.
Open the departmentservice. CS class and add the following classes after the service class:
Public Class Departmentsimple
{
Public Short Departmentid
{
Get ;
Set ;
}
}
7) create a "return" parameter for the adddepartment method in the BDC method details panel and name it "returnparameter"
8) Open the BDC Explorer window, navigate to the returnparameter parameter of the adddepartment method, and modify its type descriptor attribute.
Name: departmentsimple
Typename: bdcmodelproject1.bdcmodel1. departmentsimple, bdcmodel1
9) Right-click the departmentsimple type descriptor and select "add type descriptor ". Set the property:
Name: Required mentid
Typename: system. int16
Identifier: specified mentid
10) after the parameters and type descriptors are defined, see:
11) Next, we need to create a Method Instance for the adddepartment method. On the BDC method details panel, click Create Method Instance under adddepartment method. Set the attributes as shown in:
12) Open the departmentservice. CS file and you will see that an adddepartment method has been generated for you. Use the followingCodeCode to replace this method:
Public Static Departmentsimple Adddepartment ( String Name, String Groupname, Short Departmentid)
{
Departmentdatacontext DB = New Departmentdatacontext ( " Server = localhost; database = adventureworks; uid = ***; Pwd = *** " );
Bdcmodelproject1. Department Createdentity = New Bdcmodelproject1. Department
{
Departmentid = Departmentid,
Name = Name,
Groupname = Groupname,
Modifieddate = Datetime . Now
};
DB. Parameters. insertonsubmit (createdentity );
DB. submitchanges ();
Departmentsimple Identifiersstruct = New Departmentsimple
{
Departmentid = Departmentid,
};
Return Identifiersstruct;
}
13) recompile the project and press Ctrl + F5 to redeploy it. Open your SharePoint site and create an external list based on the BCS external content type. Select the "project" functional area on the top and you will find that the "new project" button is now available. In this way, it means that the Creator method defined above is okay, otherwise it is unavailable.
14) Click "new project" and a form will pop up for us to insert data. In step 2, we defined the input parameter and set creatorfield = true only on name and groupname. Therefore, we only see the name and groupname fields in this form. If you need to add a new field, you must not only define it as an input parameter, but also set the Creator field to true. For the Department table, mongomentid is the auto-increment primary key, so we didn't set it as the creator field, but if your primary key is not auto-incrementing, you also need to define it as a creator field.
Click Save in the form.
15) The result is as follows.
Now, let's create the BCS Updater Method for our department entity.
16) Right-click the Methods Section of the Department object and select "Add new method ".
17) create a new method named "updatedepartment ".
We need to create input parameters for the Updater method.
18) in the BDC method details panel, click the Add parameter operation under the updatedepartment method to add a parameter with the direction of "in.
19) in the BDC Explorer window, navigate to this parameter from the expanded Tree View. Select the type descriptor of this parameter and modify its attributes as follows:
Name: departmet
Typename: bdcmodelproject1.bdcmodel1. Department, bdcmodel1
20) Right-click the Department type descriptor, add two types descriptors to it, and set the attributes as follows:
Departmentid:
Name: Required mentid
Typename: system. int16
Identifier: specified mentid
Updaterfield: True
Groupname:
Name: groupname
Typename: system. String
Updaterfield: True
Name:
Name: Name
Typename: system. String
Updaterfield: True
Note: In the department table we use in this article, its primary key is auto-incrementing. To ensure that the Updater method is correctly executed, we need to define the departmentid in the return value of the specific finder method mentioned in the previous article as read-only.As shown in:
21) at this point, the parameter and type descriptor should be shown in. If you want to update more fields, first add them as type descriptors, and then set their updaterfield to true.
22) We need to create a Method Instance for the updatedepartment method. On the BDC method details panel, click Add Method Instance under the updatedepartment method to add a Method Instance and set its Method Instance type to Updater.
23) Open departmentservice. CS, find the automatically generated updatedepartment method, and replace the Code with the following code:
Public Static Void Updatedepartment ( Department Parameter)
{
Departmentdatacontext DB = New Departmentdatacontext ( " Server = localhost; database = adventureworks2000; uid = ***; Pwd = *** " );
Bdcmodelproject1. Department Updatedentity = ( From Entity In DB. analytics
Where Entity. inclumentid = Parameter. Specified mentid
Select Entity). firstordefault < Bdcmodelproject1. Department > ();
Updatedentity. Name = Parameter. Name;
Updatedentity. groupname = Parameter. groupname;
DB. submitchanges ();
}
24) recompile the project and press Ctrl + F5 to redeploy the project. Open your SharePoint site. Because we have added a new method to the Department objectYou need to delete the previously created external list and recreate it.. After the project is created, select a record and you will see that the "Edit project" button becomes available. Because we have correctly configured the Updater method for the BCS model, now we can edit the record.
25) Click "Edit Project". In the displayed form, you will see the name and groupname fields for us to update. Modify the name and click Save.
26) updated results.
I hope this article will be useful to you. In the followingArticleWe will discuss how to use Visual Studio 2010 to create a BCS association between two external content types.
References
BCS creator and Updater methods for Dot NET assembly
How to: Add a creator Method
Design a business data connectivity Model