In our previous blog posts we have created BCS Model with Microsoft Visual Studio 2010 and have shown how to define Finder, Specific Finder and ID Enumerator methods for our Department Entity:
BCS model in Visual Studio 2010 – SpecificFinder and IdEnumerator
In this blog post we’ll extend our BCS Model and explain how to define Creator and Updater methods for Department Entity which will allow users to edit/insert records to the Department Entity.
First we are going to define our Creator method.
1) Right click on the Methods section of the Department Entity and choose “Add New Method”.
2) Give the method a name of “AddDepartment”.
Next, we should create parameters for the Creator method. You should define all the fields that you want to see in the “New Item” dialog as Input parameters for the Creator method. The method also needs a return parameter which should be a structure which will contain information about the identifier(s) of the Department Entity.
3) Select Add Department method in Department entity and add new parameter in Bdc Method Details window.
4) In this example we will create three parameters with “In” direction and name them “Name”, “GroupName” , ”DepartmentId”.
5) Next we need to modify the properties of the type descriptors of each parameter. In BDC Explorer window expand the tree until you get to the type descriptors of the parameters.
You should set the following properties to the type descriptors.
DepartmentID:
Name: DepartmentId
TypeName: System.Int16
Identifier: DepartmentId
GroupName:
Name: GroupName
TypeName: System.String
CreatorField:true
Name:
Name: Name
TypeName: System.String
CreatorField: true
6) Now we need to define the return parameter for the “AddDepartment” method. The return parameter should be a structure which will describe the identifier of the Department entity.
Open DepartmentService.cs class that we used in our previous blog posts to add C# code, and add the following class just under the Service class.
public class DepartmentSimple
{
public short DepartmentId
{
get;
set;
}
}
7) Open Bdc Methods Explorer window and create new “Return” parameter for the AddDepartment method and name it “returnParameter”.
8) Open the BDC explorer window, navigate to returnParameter of the AddDepartment method and change the type descriptor’s properties.
Name: DepartmentSimple
Type Name: BdcModelProject2.BdcModel1.DepartmentSimple,BdcModel1
9) Right click on the DepartmentSimple type descriptor and select “Add Type Descriptor”. Set the following properties.
Name: DepartmentId
TypeName: System.Int16
Identifier: DepartmentId
10) Here is how the parameters and type descriptors should be declared.
11) Next, we need to create a Method Instance for the AddDepartment method. Open the Bdc Method Details window, select AddDepartment method and create a new method instance. Set the properties as has been done in the screenshot below:
12) Open DepartmentService.cs file and you’ll find there an AddDepartment method generated for you.
Replace the body of that method with the following code.
DataClasses1DataContext db = new DataClasses1DataContext(“server=localhost;database=adventureworks2000;uid=***;pwd=***”); BdcModelProject2.Department createdEntity = new BdcModelProject2.Department
{
DepartmentID = DepartmentId,
Name = Name,
GroupName = GroupName,
ModifiedDate = DateTime.Now,
rowguid = System.Guid.NewGuid(),
};
db.Departments.InsertOnSubmit(createdEntity);
db.SubmitChanges();
DepartmentSimple IdentifiersStruct = new DepartmentSimple
{
DepartmentId = DepartmentId,
};
return IdentifiersStruct;
13) Rebuild the project and press CTRL+F5 to redeploy it. Open your SharePoint site and create a new external list on this BCS External Content Type. Select the “Items” ribbon on the top and you’ll see that “New Item” button is enabled which means that we have declared Creator method in BCS model properly, otherwise it will be disabled.
14) Click on “New Item” and it will show you the form to insert your data. In step 5 we declare input parameters and set CreatorField=true only for Name and GroupName, for that reason in this form we see only the Name and GroupName fields. If you need to add new fields, you need to declare them as input parameters and set Creator Field to true. For the Department table, DepartmentId is auto incremental PK, for that reason we didn’t set it as Creator field, but if your PK is not auto incremental then you need to define it as a Creator field as well.
Click on Save button in this form.
15) Here is the result.
Now, let’s create our BCS Updater method for our Department entity.
16) Right click on the Methods section of the Department entity and select “Add New Method”.
17) Give our new method a name of “UpdateDepartment”.
We need to create input parameters for updater method.
18) In Bdc Method explorer window choose UpdateDepartment method and add new “In” parameter.
19) Open Bdc Explorer window , expand the tree and navigate to this parameter. Choose the type descriptor and change the properties.
Name: Department
Type Name: BdcModelProject2.BdcModel1.Department,BdcModel1
20) Right click on Department type descriptor and add two type descriptors with the following properties.
DepartmentID:
Name: DepartmentId
TypeName: System.Int16
Identifier: DepartmentId
UpdaterField:true
GroupName:
Name: GroupName
TypeName: System.String
UpdaterField:true
Name:
Name: Name
TypeName: System.String
UpdaterField: true
NOTE: In this blog post we use Department table, and its PK field is set as auto incremental. To get updater method executed properly, we need to declare DepartmentId as Read Only in our Specific finder method’s return parameter.
21) Parameter and type descriptors should be looked like this. If you want to update more fields you need to add them as type descriptors set UpdaterField=true, as it is shown in step 21.
22) We need to create a method instance for UpdateDepartment method. In Bdc Method explorer window choose UpdateDepartment , add new method instance and set method instance type to Updater.
23) Open DepartmentService.cs and you’ll find there UpdateDepartment method generated for you.
Replace the body with the following C# code.
DataClasses1DataContext db = new DataClasses1DataContext(“server=localhost;database=adventureworks2000;uid=***;pwd=***”); BdcModelProject2.Department updatedEntity = (from entity in db.Departments
where entity.DepartmentID == parameter.DepartmentId
select entity).FirstOrDefault();
updatedEntity.Name = parameter.Name;
updatedEntity.GroupName = parameter.GroupName;
dcontext.Submi
tChanges();
24) Rebuild the project, click CTRL+F5 to redeploy, go to the External List that we have already created in step 14, select any record and you’ll see that “Edit Item” button is enabled, so we are allowed to edit the record cause we configure updater method in BCS Model properly.
25) Click on “Edit Item” and you’ll see Name and GroupName fields, which you can update. Change the GroupName and click on Save button.
26) Here you can see the updated result.
Hopefully you found this post useful – in my next blog post I will be showing you how to create a BCS association between two External Content Types in Visual Studio 2010.
<hrayr/>