Example #1
0
 public PartManagementRepository()
 {
     bdcService = SPFarm.Local.Services.GetValue <BdcService>();
     catalog    = bdcService.GetDatabaseBackedMetadataCatalog(SPServiceContext.Current);
     partsManagementLobSystemInstance = catalog.GetLobSystem(Constants.LobSystemName).GetLobSystemInstances()[Constants.LobSystemName];
     contactsLobSystemInstance        = catalog.GetLobSystem(Constants.ContactsLobSystemName).GetLobSystemInstances()[Constants.ContactsLobSystemName];
 }
        /// <summary>
        /// Get the instance of a Business Data Connectivity entity
        /// </summary>
        /// <param name="site"></param>
        /// <param name="nameSpace"></param>
        /// <param name="entityName"></param>
        /// <param name="entityId"></param>
        /// <returns></returns>
        private static IEntityInstance GetEntityInstance(SPSite site, string nameSpace, string entityName, string entityId)
        {
            //Use the scope of the currently opened site
            SPServiceContext      ctx   = SPServiceContext.GetContext(site);
            SPServiceContextScope scope = new SPServiceContextScope(ctx);

            //Get the BDC service of the local SP farm
            BdcService         service           = SPFarm.Local.Services.GetValue <BdcService>();
            IMetadataCatalog   catalog           = service.GetDatabaseBackedMetadataCatalog(ctx);
            IEntity            entity            = catalog.GetEntity(nameSpace, entityName);
            ILobSystemInstance LobSysteminstance = entity.GetLobSystem().GetLobSystemInstances()[0].Value;
            IEntityInstance    entInstance       = null;

            //Loop through the methods defined in the LOB
            foreach (KeyValuePair <string, IMethod> method in entity.GetMethods())
            {
                IMethodInstance methodInstance = method.Value.GetMethodInstances()[method.Key];
                //Get the Specific Finder method of the LOB
                if (methodInstance.MethodInstanceType == MethodInstanceType.SpecificFinder)
                {
                    //Find the record with the ID from the datasource
                    Microsoft.BusinessData.Runtime.Identity id = new Microsoft.BusinessData.Runtime.Identity(entityId);
                    entInstance = entity.FindSpecific(id, entity.GetLobSystem().GetLobSystemInstances()[0].Value);
                }
            }

            return(entInstance);
        }
Example #3
0
        private static IEntity GetEntity(SPSite site, SPBusinessDataField dataField)
        {
            IEntity result = null;

            try
            {
                SPServiceContext context    = /*SPServiceContext.GetContext(site);*/ SPServiceContext.Current;
                IMetadataCatalog catalog    = null;
                BdcService       bdcService = SPFarm.Local.Services.GetValue <BdcService>(String.Empty);
                if (bdcService != null && dataField != null)
                {
                    catalog = bdcService.GetDatabaseBackedMetadataCatalog(context);
                    if (catalog != null)
                    {
                        result = catalog.GetEntity(dataField.EntityNamespace, dataField.EntityName);
                    }
                }
            }
            catch (Exception e)
            {
                LogError("GetEntity failed with message " + e.Message);
            }
            return(result);
        }
Example #4
0
        protected void CreateNewCustomer_Click(object sender,
                                               EventArgs e)
        {
            // Make sure we have values for the entity namespace and name.
            if (!EntityValuesAreSet)
            {
                DisplaySetPropertyValuePrompt(true);
                return;
            }
            else
            {
                DisplaySetPropertyValuePrompt(false);
            }

            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;

                    // Get the fields on the entity.
                    IView createView =
                        entity.GetCreatorView("CreateCustomer");
                    IFieldValueDictionary valueDictionary =
                        createView.GetDefaultValues();

                    // Set the values of the entity fields.
                    valueDictionary["EmailAddress"] = Email.Text;
                    valueDictionary["FirstName"]    = FirstName.Text;
                    valueDictionary["LastName"]     = LastName.Text;
                    valueDictionary["MiddleName"]   = MiddleName.Text;
                    valueDictionary["Phone"]        = Phone.Text;
                    valueDictionary["Title"]        = Title.Text;

                    // Call the creator method and display the returned
                    // Customer ID.
                    Identity id = entity.Create(valueDictionary,
                                                LobSysteminstance);

                    CustomerID.Text =
                        id.GetIdentifierValues().GetValue(0).ToString();

                    StatusLabel.ForeColor = Color.Green;
                    StatusLabel.Text      = "Customer successfully created.";
                }
            }
            catch (Exception ex)
            {
                StatusLabel.ForeColor = Color.Red;
                StatusLabel.Text      = "Unable to create customer." +
                                        ex.Message;
            }
        }
Example #5
0
        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;
            }
        }
Example #6
0
        protected void FindCustomerByID_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))
            {
                ClearFields(false);
                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 SpecificFinder method.
                    IMethodInstance method =
                        entity.GetMethodInstance("GetCustomerById",
                                                 MethodInstanceType.SpecificFinder);

                    // Execute the Specific Finder method to return the
                    // customer data.
                    IEntityInstance iei =
                        entity.FindSpecific(identity, LobSysteminstance);

                    // Display the data for the returned customer in the UI.
                    Title.Text = iei["Title"] != null ?
                                 iei["Title"].ToString() : string.Empty;
                    FirstName.Text = iei["FirstName"] != null ?
                                     iei["FirstName"].ToString() : string.Empty;
                    MiddleName.Text = iei["MiddleName"] != null ?
                                      iei["MiddleName"].ToString() : string.Empty;
                    LastName.Text = iei["LastName"] != null ?
                                    iei["LastName"].ToString() : string.Empty;
                    Email.Text = iei["EmailAddress"] != null ?
                                 iei["EmailAddress"].ToString() : string.Empty;
                    Phone.Text = iei["Phone"] != null ?
                                 iei["Phone"].ToString() : string.Empty;
                }
            }
            catch (Exception ex)
            {
                ClearFields(false);

                StatusLabel.ForeColor = Color.Red;
                StatusLabel.Text      = "Unable to find customer with ID = " +
                                        CustomerID.Text + ". " + ex.Message;
            }
        }