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]; }
internal Dictionary <string, BdcLobSysInst> GetLobSystemInstances() { #if SP12 return(ApplicationRegistry.GetLobSystemInstances()); #else INamedLobSystemDictionary dict; Dictionary <string, BdcLobSysInst> result = new Dictionary <string, BdcLobSysInst> (); BdcLobSysInstDict tmp; if (bdcCat == null) { if (bdcService == null) { bdcService = new BdcService(); } bdcCat = bdcService.GetDatabaseBackedMetadataCatalog(SPServiceContext.Current); } if ((dict = bdcCat.GetLobSystems("*")) != null) { foreach (KeyValuePair <string, ILobSystem> kvp in dict) { if ((tmp = kvp.Value.GetLobSystemInstances()) != null) { foreach (KeyValuePair <string, BdcLobSysInst> kvp2 in tmp) { result [kvp2.Key] = kvp2.Value; } } } } return(result); #endif }
/// <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); }
public ArtDevBCS(string ModelName) { this.ModelName = ModelName; // Get the Catalog for the SharePoint site BdcService service = SPFarm.Local.Services.GetValue <BdcService>(String.Empty); SPAdministrationWebApplication centralWeb = SPAdministrationWebApplication.Local; SPSite AdminSite = new SPSite(centralWeb.Sites.FirstOrDefault <SPSite>().Url); SPServiceContext context = SPServiceContext.GetContext(AdminSite); this.catalog = service.GetAdministrationMetadataCatalog(context); this.catalog.GetModels(ModelName)?.ToList().ForEach(m => m.Delete()); // Create a new Model // NOTE: Assume that the "ModelName" Model // does not already exist. this.Model = Model.Create(ModelName, true, catalog); }
internal BdcLobSysInst GetLobSystemInstanceByName(string name) { #if SP12 return(ApplicationRegistry.GetLobSystemInstanceByName(name)); #else if (bdcCat == null) { if (bdcService == null) { bdcService = new BdcService(); } bdcCat = bdcService.GetDatabaseBackedMetadataCatalog(SPServiceContext.Current); } foreach (KeyValuePair <string, BdcLobSysInst> kvp in GetLobSystemInstances()) { if ((kvp.Value != null) && (kvp.Value.Name == name)) { return(kvp.Value); } } return(null); #endif }
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); }
// Uncomment the method below to handle the event raised after a feature has been activated. public override void FeatureActivated(SPFeatureReceiverProperties properties) { // Get the Catalog for the SharePoint site BdcService service = SPFarm.Local.Services.GetValue <BdcService>(String.Empty); SPSite site = new SPSite("http://sp2016:2016/"); SPServiceContext context = SPServiceContext.GetContext(site); AdministrationMetadataCatalog catalog = service.GetAdministrationMetadataCatalog(context); catalog.GetModels("EmployeeModel")?.ToList().ForEach(m => m.Delete()); // Create a new Employee Model // NOTE: Assume that the "EmployeeModel" Model // does not already exist. Model EmployeeModel = Model.Create("EmployeeModel", true, catalog); // Make a new Employee LobSystem // NOTE: Assume that the "AdventureWorks" LobSystem // does not already exist. LobSystem adventureWorksLobSystem = EmployeeModel.OwnedReferencedLobSystems.Create("AdventureWorks", true, SystemType.Database); // Make a new AdventureWorks LobSystemInstance. LobSystemInstance adventureWorksLobSystemInstance = adventureWorksLobSystem.LobSystemInstances.Create("AdventureWorks", true); // Set the connection properties. adventureWorksLobSystemInstance.Properties.Add( "ShowInSearchUI", ""); adventureWorksLobSystemInstance.Properties.Add( "DatabaseAccessProvider", "SqlServer"); adventureWorksLobSystemInstance.Properties.Add( "RdbConnection Data Source", "SP2016"); adventureWorksLobSystemInstance.Properties.Add( "RdbConnection Initial Catalog", "AdventureWorks2016"); adventureWorksLobSystemInstance.Properties.Add( "AuthenticationMode", "RdbCredentials"); adventureWorksLobSystemInstance.Properties.Add( "SsoProviderImplementation", "Microsoft.Office.SecureStoreService.Server.SecureStoreProvider, Microsoft.Office.SecureStoreService, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"); adventureWorksLobSystemInstance.Properties.Add( "SsoApplicationId", "Adventure"); adventureWorksLobSystemInstance.Properties.Add( "RdbConnection Pooling", "true"); // Create a new Employee Entity. Entity EmployeeEntity = Entity.Create( "Employee", "AdventureWorks", true, new Version("1.0.0.4"), 10000, CacheUsage.Default, adventureWorksLobSystem, EmployeeModel, catalog); // Set the identifier to the EmployeeID column. EmployeeEntity.Identifiers.Create( "BusinessEntityID", true, "System.Int32"); // Create the Finder Method, // i.e. the method to return all rows. CreateReadListMethod(catalog, EmployeeEntity); // Create the Specific Finder Method, // i.e. the method to return one row. CreateReadItemMethod(catalog, EmployeeEntity); // Validate the Employee Entity. ActivationError[] activationErrors = EmployeeEntity.Validate(); // Check if the validation failed. if (activationErrors.Count() == 0) { // The validation was successful so publish the Employee Entity. EmployeeEntity.Activate(); } }
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; } }
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; } }
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; } }