/// <summary> /// Get the fields that have been defined for the return entity of a method. /// </summary> /// <param name="methodInstance">The method to examine.</param> /// <returns>A collection of fields defined on the return entity of the the method.</returns> protected virtual ITypeDescriptorCollection GetEntityFields(Microsoft.BusinessData.MetadataModel.IMethodInstance methodInstance) { ITypeDescriptor returnType = methodInstance.GetReturnTypeDescriptor(); ITypeDescriptorCollection entityFields = null; if (returnType.IsCollection) { ITypeDescriptorCollection childTypes = returnType.GetChildTypeDescriptors(); if (childTypes.Count > 0) { entityFields = childTypes[0].GetChildTypeDescriptors(); } } else { entityFields = returnType.GetChildTypeDescriptors(); } return(entityFields); }
protected void UpdateCustomer_Click(object sender, EventArgs e) { // Make sure we have values for the entity namespace and name. if (!EntityValuesAreSet) { DisplaySetPropertyValuePrompt(true); return; } else { DisplaySetPropertyValuePrompt(false); } // Do simple validation of the customer ID. Make sure it is // an integer. int customerID = -1; if (!ValidateCustomerID(CustomerID.Text, ref customerID)) { StatusLabel.ForeColor = Color.Red; StatusLabel.Text = "Please enter an integer for the Customer ID value."; return; } try { using (new Microsoft.SharePoint.SPServiceContextScope( SPServiceContext.GetContext(SPContext.Current.Site))) { // Get the BDC service and metadata catalog. BdcService service = SPFarm.Local.Services.GetValue <BdcService>(String.Empty); IMetadataCatalog catalog = service.GetDatabaseBackedMetadataCatalog( SPServiceContext.Current); // Get the entity using the specified name and namespace. IEntity entity = catalog.GetEntity(EntityNamespace, EntityName); ILobSystemInstance LobSysteminstance = entity.GetLobSystem().GetLobSystemInstances()[0].Value; // Create an Identity based on the specified Customer ID. Identity identity = new Identity(customerID); // Get a method instance for the Updater method. IMethodInstance method = entity.GetMethodInstance("UpdateCustomer", MethodInstanceType.Updater); // The UpdateCustomer method of the external content type // maps to the UpdateCustomer method in the AdventureWorks // web service. Looking at the source for the web service // shows that the UpdateCustomer method has the following // signature: // // public void UpdateCustomer(SalesCustomer customer) // // The SalesCustomer type has the following layout: // // public class SalesCustomer // { // public int CustomerId { get; set; } // public String Title { get; set; } // public String FirstName { get; set; } // public String MiddleName { get; set; } // public String LastName { get; set; } // public String EmailAddress { get; set; } // public String Phone { get; set; } // public DateTime ModifiedDate { get; set; } // } // Get the collection of parameters for the method. // In this case there is only one. IParameterCollection parameters = method.GetMethod().GetParameters(); // Use type reflection to get an instance of a // SalesCustomer object to pass as a parameter. ITypeReflector reflector = parameters[0].TypeReflector; ITypeDescriptor rootTypeDescriptor = parameters[0].GetRootTypeDescriptor(); object[] methodParamInstances = method.GetMethod().CreateDefaultParameterInstances( method); Object instance = methodParamInstances[0]; // Get type descriptors for each of the SalesCustomer // members. ITypeDescriptor customerIDTypeDescriptor = rootTypeDescriptor.GetChildTypeDescriptors()[0]; ITypeDescriptor titleTypeDescriptor = rootTypeDescriptor.GetChildTypeDescriptors()[1]; ITypeDescriptor firstNameTypeDescriptor = rootTypeDescriptor.GetChildTypeDescriptors()[2]; ITypeDescriptor middleNameTypeDescriptor = rootTypeDescriptor.GetChildTypeDescriptors()[3]; ITypeDescriptor lastNameTypeDescriptor = rootTypeDescriptor.GetChildTypeDescriptors()[4]; ITypeDescriptor emailAddressTypeDescriptor = rootTypeDescriptor.GetChildTypeDescriptors()[5]; ITypeDescriptor phoneTypeDescriptor = rootTypeDescriptor.GetChildTypeDescriptors()[6]; ITypeDescriptor modifiedDateTypeDescriptor = rootTypeDescriptor.GetChildTypeDescriptors()[7]; // Set the values of the SalesCustomer object members // with the values specified by the user. reflector.Set(customerIDTypeDescriptor, rootTypeDescriptor, ref instance, customerID); reflector.Set(titleTypeDescriptor, rootTypeDescriptor, ref instance, Title.Text); reflector.Set(firstNameTypeDescriptor, rootTypeDescriptor, ref instance, FirstName.Text); reflector.Set(middleNameTypeDescriptor, rootTypeDescriptor, ref instance, MiddleName.Text); reflector.Set(lastNameTypeDescriptor, rootTypeDescriptor, ref instance, LastName.Text); reflector.Set(emailAddressTypeDescriptor, rootTypeDescriptor, ref instance, Email.Text); reflector.Set(phoneTypeDescriptor, rootTypeDescriptor, ref instance, Phone.Text); reflector.Set(modifiedDateTypeDescriptor, rootTypeDescriptor, ref instance, DateTime.Now); // Execute the updater method, passing the parameter. entity.Execute(method, LobSysteminstance, ref methodParamInstances); StatusLabel.ForeColor = Color.Green; StatusLabel.Text = "Customer successfully updated."; } } catch (Exception ex) { StatusLabel.ForeColor = Color.Red; StatusLabel.Text = "Unable to find customer with ID = " + CustomerID.Text + ". " + ex.Message; } }