Just launched our site at http://jardalu.com
Also all our blogging will eventually move to http://blog.jardalu.com
Tuesday, July 17, 2012
Tuesday, May 18, 2010
SPD (WCF connection) - Cannot find any matching endpoint
When adding a WCF connection to SPD 2010, you will hit "Cannot find any matching endpoint configuration" error, if you did not know the service endpoint URL (see figure 2).
If you look into the SharePoint ULS logs, you will see a corresponding error message "Could not initialize the endpoint components".
If you are connecting to ASMX based web service, the "Service Endpoint URL" is the ASMX service url. For example, if the ASMX service is hosted at http://myserver/service.asmx then the values for "Service Metadata URL" and "Service Endpoint URL" are as follows -
Service Metadata URL - http://myserver/service.asmx?wsdl
Service Endpoint URL - http://myserver/service.asmx
If you are connecting to WCF service, these values can get interesting. Now with WCF service, the service may not expose metadata endpoint, can expose WSDL endpoint or can expose MEX endpoint.
a) Metadata endpoint not exposed
When the metadata endpoint is not exposed by the WCF service, SPD (BCS) cannot consume that WCF service.
b) Metadata endpoint exposed as WSDL
When the WCF service exposes metadata as WSDL, its "Service Endpoint URL" can be determined by looking in the WSDL. Simply load the WSDL in the web browser ( http://myserver/service.svc?wsdl) and search for soap12:address in the WSDL. Typically it will appear in the end of the WSDL (see figure 3).
There can be more than one soap address in the WSDL, if the service exposes different bindings for these addresses. Depending on your requirement you can choose one of the service address.
c) Metadata endpoint exposed as MEX
When the metadata is exposed as MEX, you will need to read the endpoint addresses using svcutil.exe (http://msdn.microsoft.com/en-us/library/aa347733.aspx). When you execute svcutil.exe against the MEX endpoint, it will generate a config file. In the config file, search for "client" tag (it appears at the end of config file). Look at the addresses for the endpoint.
The addresses mentioned in the config file are the "Service Endpoint URL" that SPD will understand.
Figure 1: Cannot find any matching endpoint.
Figure 2: WCF connection properties
If you look into the SharePoint ULS logs, you will see a corresponding error message "Could not initialize the endpoint components".
If you are connecting to ASMX based web service, the "Service Endpoint URL" is the ASMX service url. For example, if the ASMX service is hosted at http://myserver/service.asmx then the values for "Service Metadata URL" and "Service Endpoint URL" are as follows -
Service Metadata URL - http://myserver/service.asmx?wsdl
Service Endpoint URL - http://myserver/service.asmx
If you are connecting to WCF service, these values can get interesting. Now with WCF service, the service may not expose metadata endpoint, can expose WSDL endpoint or can expose MEX endpoint.
a) Metadata endpoint not exposed
When the metadata endpoint is not exposed by the WCF service, SPD (BCS) cannot consume that WCF service.
b) Metadata endpoint exposed as WSDL
When the WCF service exposes metadata as WSDL, its "Service Endpoint URL" can be determined by looking in the WSDL. Simply load the WSDL in the web browser ( http://myserver/service.svc?wsdl) and search for soap12:address in the WSDL. Typically it will appear in the end of the WSDL (see figure 3).
<wsdl:service name="Service"> <wsdl:port name="WSHttpBinding_IService" binding="tns:WSHttpBinding_Service"> <soap12:address location="http://myserver/Service.svc" /> <wsa10:EndpointReference> <wsa10:Address>http://myserver/Service.svc</wsa10:Address> <Identity xmlns="http://schemas.xmlsoap.org/ws/2006/02/addressingidentity"> <Spn>host/myserver.com</Spn> </Identity> </wsa10:EndpointReference> </wsdl:port> </wsdl:service>
Figure 3: Service Endpoint URL in WSDL
There can be more than one soap address in the WSDL, if the service exposes different bindings for these addresses. Depending on your requirement you can choose one of the service address.
c) Metadata endpoint exposed as MEX
When the metadata is exposed as MEX, you will need to read the endpoint addresses using svcutil.exe (http://msdn.microsoft.com/en-us/library/aa347733.aspx). When you execute svcutil.exe against the MEX endpoint, it will generate a config file. In the config file, search for "client" tag (it appears at the end of config file). Look at the addresses for the endpoint.
<client> <endpoint address="http://myserver/service.svc/basicHttpBinding" binding="basicHttpBinding" bindingConfiguration="MyService" contract="IServiceInterface" name="ConsoleService" /> </client>
Figure 4: Service Endpoint URL through MEX endpoint
The addresses mentioned in the config file are the "Service Endpoint URL" that SPD will understand.
Tuesday, May 11, 2010
Secure Store and .Net Connectivity Assembly
Awesome post at BCS blog which shows how to use Secure Store API.
http://blogs.msdn.com/bcs/archive/2010/05/11/how-do-i-leverage-secure-store-from-within-my-net-connectivity-assembly.aspx
http://blogs.msdn.com/bcs/archive/2010/05/11/how-do-i-leverage-secure-store-from-within-my-net-connectivity-assembly.aspx
Friday, March 5, 2010
Writing Custom Connector for BCS
BCS in SharePoint 2010 provides following connectors - Database, WebService, WCF, .NET and custom connector. This blog explains how you can write and deploy your own custom connector, if one of the BCS connectors do not meet your requirements.
This documentation (http://msdn.microsoft.com/en-us/library/ee554911(office.14).aspx) explains when to use .NET assembly connector and when to write your own custom connector.
Assuming that you are required to write your own custom connector, lets look at the steps to achieve the goal.
The steps for custom connectors are
Writing custom connector requires implementing ISystemUtility (http://msdn.microsoft.com/en-us/library/microsoft.businessdata.runtime.isystemutility(office.14).aspx) interface. The simple most custom connector requires implementing ExecuteStatic method in the interface. Other methods/properties can be boiler plate code.
Here is the simple most custom connector. Although the connector does not do anything, technically it can be deployed in SharePoint 2010.
Let's take a look at the various methods and properties in the ISystemUtility interface.
Lets take a look at the method parameters
mi : This parameter represents the MethodInstance that is being executed by BDC. This parameter corresponds to the
si : This parameter represents the LobSystemInstance the method instance is being executed against. This parameter corresponds to the
args : This parameter is theParameters of the MethodInstance that is being executed. The last item in the array is reserved for the return parameter for the method instance.
context : This parameter sets the execution context of BDC. For all practical purposes this parameter can be ignored because External List in SharePoint does not set the execution context and you or BDC will not control this variable. The context can be different for BDC running under Office client.
Ok, now that we understand ISystemUtility interface, lets do some fun stuff.
Sample
In this sample, I will create a custom connector that will return "Movie" entity from an external system. For simplicity, the connector will support only "Finder" and "SpecificFinder" stereotypes. This sample will use in-memory data (external system).
Since the external system returns .NET types, implementation of the custom connector is simple.
In the method CreateEntityInstanceDataEnumerator, the base Enumerator is returned. Off course, for a real external system you may have to write an enumerator.
ExecuteStatic method will check what kind of stereotype is being executed and will execute corresponding methods on the external system. As said before, this connector just supports Finder/SpecificFinder stereotype, so it will throw for other stereotypes.
The method verifies the Finder/SpecificFinder signature in metadata model. This way the connector can ensure that the metadata model is not invalid. Finally it executes the external system and sets the return value in args parameter.
I have attached complete source code at the end of this post.
Deploying Custom Connector in SharePoint
Deploying custom connector in SharePoint 2010 is pretty straight forward. All you need is to GAC the assembly in all the SharePoint machines. This includes web-front ends as well as application servers in the farm. If you have the requirement to execute custom connectors on Office client ( take External List to Outlook or Workspaces ) you will need to GAC the assembly in client machines as well.
Writing Models for Custom Connector
Now that you have written the custom connector and have already deployed in the SharePoint, lets get a sample external list for the custom connector.
The LobSystem Type for custom connector must specify "Custom". When custom connectors are used, the model must contain the SystemUtilityTypeName property for LobSystem.
SystemUtilityTypeName is the fully qualified name for custom connector type. The following figure shows snippet of the metadata model.
The sample metadata model is also attached in the source code (see end of post).
Custom connector in action
Now that we have our metadata model for custom connector, lets fire it up in the SharePoint. The following screen shots show the external list running against custom connector.
Figure 6 shows the external content type displayed for custom connector. At this point the custom connector is not being executed.
Figure 7 and Figure 8 shows Finder and SpecificFinder stereotyped operations running in custom connector. Our custom connector executes the external system methods and returns appropriate data.
Source Code
Sample with complete source code can be downloaded from here.
As-is
The source code/software is provided "as-is". No claim of suitability, guarantee, or any warranty whatsoever is provided. Source Code and executable files can not be used in commercial applications.
This documentation (http://msdn.microsoft.com/en-us/library/ee554911(office.14).aspx) explains when to use .NET assembly connector and when to write your own custom connector.
Assuming that you are required to write your own custom connector, lets look at the steps to achieve the goal.
The steps for custom connectors are
- Write code for custom connector
- Deploy custom connector in SharePoint
- Write model for custom connector
Writing custom connector requires implementing ISystemUtility (http://msdn.microsoft.com/en-us/library/microsoft.businessdata.runtime.isystemutility(office.14).aspx) interface. The simple most custom connector requires implementing ExecuteStatic method in the interface. Other methods/properties can be boiler plate code.
Here is the simple most custom connector. Although the connector does not do anything, technically it can be deployed in SharePoint 2010.
using System; using System.Collections; using System.Collections.Generic; using Microsoft.BusinessData.MetadataModel; using Microsoft.BusinessData.Runtime; using Microsoft.BusinessData.Infrastructure; namespace SharePointConnector { public class Connector : ISystemUtility { #region ISystemUtility Members public IEnumerator CreateEntityInstanceDataEnumerator(object rawStream, ISharedEntityState sharedEntityState) { // implement your enumerator } public IConnectionManager DefaultConnectionManager { get { return null; } } public ITypeReflector DefaultTypeReflector { get { return null; } } public void ExecuteStatic(IMethodInstance mi, ILobSystemInstance si, object[] args, IExecutionContext context) { // implement your logic } #endregion } }
Figure 1: Simple most custom connector
Let's take a look at the various methods and properties in the ISystemUtility interface.
CreateEntityInstanceDataEnumeratorThis method converts line-of-business data stream into an enumerator of raw AdapterObjects. Implementing this method is necessary. If your connector gets IEnumerable as raw stream, you can just return the corresponding enumerator from the method.
DefaultConnectionManagerThis property allows you to manage connections for your external system. You can return null if you don't want connection management. BDC (Business Data Connectivity) will use its own default connection manager.
DefaultTypeReflectorThis property allows you manage the type reflection for your objects. For example, if your external system returns stream, BDC will unable to do a meaningful type reflection and you will have to write your own type reflection. If your external system returns .NET types than you don't need to provide any type reflection. For default implementation this property should return null.
ExecuteStaticThis method is the most important method in the interface. In the method, you should implement CRUDQ (create, read, update, delete and query) stereotypes for the connector.
public void ExecuteStatic(IMethodInstance mi, ILobSystemInstance si, object[] args, IExecutionContext context)
Lets take a look at the method parameters
mi : This parameter represents the MethodInstance that is being executed by BDC. This parameter corresponds to the
<MethodInstance>element with the LobSystem/Entity/Method in the BDC metadata model.
si : This parameter represents the LobSystemInstance the method instance is being executed against. This parameter corresponds to the
<LobSystemInstance>element with the LobSystem in the BDC metadata model.
args : This parameter is the
context : This parameter sets the execution context of BDC. For all practical purposes this parameter can be ignored because External List in SharePoint does not set the execution context and you or BDC will not control this variable. The context can be different for BDC running under Office client.
Ok, now that we understand ISystemUtility interface, lets do some fun stuff.
Sample
In this sample, I will create a custom connector that will return "Movie" entity from an external system. For simplicity, the connector will support only "Finder" and "SpecificFinder" stereotypes. This sample will use in-memory data (external system).
Since the external system returns .NET types, implementation of the custom connector is simple.
public IEnumerator CreateEntityInstanceDataEnumerator(object rawStream, ISharedEntityState sharedEntityState) { IEnumerable enumerableStream = rawStream as IEnumerable; if (enumerableStream != null) { return enumerableStream.GetEnumerator(); } throw new InvalidOperationException("not valid stream returned"); }
Figure 2: CreateEntityInstanceDataEnumerator implementation
In the method CreateEntityInstanceDataEnumerator, the base Enumerator is returned. Off course, for a real external system you may have to write an enumerator.
ExecuteStatic method will check what kind of stereotype is being executed and will execute corresponding methods on the external system. As said before, this connector just supports Finder/SpecificFinder stereotype, so it will throw for other stereotypes.
public void ExecuteStatic(IMethodInstance mi, ILobSystemInstance si, object[] args, IExecutionContext context) { // provide only read functionality switch(mi.MethodInstanceType) { case MethodInstanceType.SpecificFinder: IParameterCollection parameters = mi.GetMethod().GetParameters(); // make sure there is only one input parameter for the method // and one return parameter. if (parameters.Count != 2 ) { string message = "Method " + mi.GetMethod().Name +" must have one input and one return parameter"; throw new InvalidMetadataObjectException(message); } // check if the input parameter is integer type // and the return parameter is "Movie" type Type param1Type = Type.GetType(parameters[0].GetRootTypeDescriptor().TypeName, false); Type param2Type = Type.GetType(parameters[1].GetRootTypeDescriptor().TypeName, false); if ( param1Type == null || param1Type != typeof(Int32)) { string message = "Method " + mi.GetMethod().Name +" must contain input of type System.Int32"; throw new InvalidMetadataObjectException(message); } if ( param2Type == null || param2Type != typeof(Movie)) { string message = "Method " + mi.GetMethod().Name +" must contain input of type " + typeof(Movie).ToString(); throw new InvalidMetadataObjectException(message); } int id = (int)args[0]; args[1] = MovieData.GetMovie(id); break; case MethodInstanceType.Finder: args[args.Length-1] = MovieData.GetMovies(); break; default: throw new NotImplementedException(); } }
Figure 3: ExecuteStatic implementation
The method verifies the Finder/SpecificFinder signature in metadata model. This way the connector can ensure that the metadata model is not invalid. Finally it executes the external system and sets the return value in args parameter.
I have attached complete source code at the end of this post.
Deploying Custom Connector in SharePoint
Deploying custom connector in SharePoint 2010 is pretty straight forward. All you need is to GAC the assembly in all the SharePoint machines. This includes web-front ends as well as application servers in the farm. If you have the requirement to execute custom connectors on Office client ( take External List to Outlook or Workspaces ) you will need to GAC the assembly in client machines as well.
Writing Models for Custom Connector
Now that you have written the custom connector and have already deployed in the SharePoint, lets get a sample external list for the custom connector.
The LobSystem Type for custom connector must specify "Custom". When custom connectors are used, the model must contain the SystemUtilityTypeName property for LobSystem.
<LobSystems> <LobSystem Type="Custom" Name="CustomLobSystem"> <Properties> <Property Type="System.String" Name="SystemUtilityTypeName">SharePointConnector.Connector, SharePointConnector, Version=1.0.0.0, Culture=neutral, PublicKeyToken=dc97b363e814985e</Property> </Properties>
Figure 4: LobSystem properties for custom connector
SystemUtilityTypeName is the fully qualified name for custom connector type. The following figure shows snippet of the metadata model.
Figure 5: Metadata model snippet for custom connector
The sample metadata model is also attached in the source code (see end of post).
Custom connector in action
Now that we have our metadata model for custom connector, lets fire it up in the SharePoint. The following screen shots show the external list running against custom connector.
Figure 6: Choosing external content type
Figure 6 shows the external content type displayed for custom connector. At this point the custom connector is not being executed.
Figure 7: Finder stereotype executed in custom connector (via External list)
Figure 8: SpecificFinder stereotype executed in custom connector
Figure 7 and Figure 8 shows Finder and SpecificFinder stereotyped operations running in custom connector. Our custom connector executes the external system methods and returns appropriate data.
Source Code
Sample with complete source code can be downloaded from here.
As-is
The source code/software is provided "as-is". No claim of suitability, guarantee, or any warranty whatsoever is provided. Source Code and executable files can not be used in commercial applications.
Labels:
BCS,
custom connector,
ISystemUtility,
SharePoint,
SharePoint 2010
Monday, March 1, 2010
SharePoint - Non root site collection caution
SharePoint allows one to create non root site collection without having a root site collection. For example, you can create a site collection as "http://sharepoint/sites/" without having any site collection at "http://sharepoint".
Caution : Even though SharePoint does not prevent you creating a non-root site collection without a root site collection, not having a root site collection is not a supported configuration. Some SharePoint features break without a root site collection.
Sunday, January 24, 2010
Client OM (Microsoft.SharePoint.Client) Samples for SharePoint 2010
SharePoint 2010 introduces a new client side object model for retrieving data from SharePoint. The client OM is included in Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll. Library reference for client OM is at http://msdn.microsoft.com/en-us/library/ee536622(office.14).aspx
Client OM is counterpart to the Server OM with notable differences
In this blog, I will covering few samples on how to use the Client OM.
Basics
First you will need to reference Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll and then use Microsoft.SharePoint.Client namespace.
To connect to the SharePoint server, you will need SharePoint URL and create a ClientContext object.
At this moment the client context for SharePoint is defined but no connection has been made. Now lets initialize the Web object (equivalent to SPWeb in Server OM).
To connect/fetch data from the SharePoint, ExecuteQuery needs to be call on ClientContext object.
If you run the above code, it will throw PropertyOrFieldNotInitializedException exception with following message
The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
The code throws exception because client OM does NOT fetch data implicitly. So, we will modify the code to fetch the data the data explicitly. Since we are only interested in Id and the Title of the Web, the code will explicitly request those data. Retreive method in the Web class to tell the client OM which data needs to be fetched. The complete code will look like
The above code will print the title and the id for the Web.
ClientObject class is the base class for all Client OM object. ClientObject exposes the following important methods that code will use again and again.
IsPropertyAvailable(string propertyName) - Returns a flag that indicates whether the specified property has been retrieved or set, or has not been retrieved or set.
Retreive() - Retreives all properties associated with the object
Retreive(params string[] propertyNames) - Retreives the specified properties associated with the object
All classes deriving from ClientObject class has a property names class ( for example, Web class has WebPropertyNames class ) which tells what properties can be fetched for the object.
Sample 1: Get all lists in web
The following sample will get all the lists in a web. For the list, the code will fetch the list id, list title and the type of the list.
Sample 2: Get list details
In this sample, given a list the code gets the details for the list. The details include the Fields and Views of the list.
Sample 3: Get list items
The following code gets the list items in the given list.
Sample 4: Add item to a list
The following code adds a list item in the given list.
Client OM is very useful however it takes time to understand how to use the API and get the best out of it. Enjoy !!
Client OM is counterpart to the Server OM with notable differences
- Client OM works on both on SharePoint server and client
- Client OM does not fetch data implicitly
- Client OM is supported for .NET as well as for ECMA (javascript etc)
In this blog, I will covering few samples on how to use the Client OM.
Basics
First you will need to reference Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll and then use Microsoft.SharePoint.Client namespace.
using Microsoft.SharePoint.Client;
To connect to the SharePoint server, you will need SharePoint URL and create a ClientContext object.
ClientContext context = new ClientContext("http://sharepoint");
At this moment the client context for SharePoint is defined but no connection has been made. Now lets initialize the Web object (equivalent to SPWeb in Server OM).
Web web = context.Web;
To connect/fetch data from the SharePoint, ExecuteQuery needs to be call on ClientContext object.
context.ExecuteQuery(); Console.WriteLine("Web '{0}' [Id:{1}]",web.Title, web.Id);
If you run the above code, it will throw PropertyOrFieldNotInitializedException exception with following message
The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
The code throws exception because client OM does NOT fetch data implicitly. So, we will modify the code to fetch the data the data explicitly. Since we are only interested in Id and the Title of the Web, the code will explicitly request those data. Retreive method in the Web class to tell the client OM which data needs to be fetched. The complete code will look like
ClientContext context = new ClientContext("http://sharepoint"); Web web = context.Web; web.Retrieve( WebPropertyNames.Id, WebPropertyNames.Title ); context.ExecuteQuery(); Console.WriteLine("Web '{0}' [Id:{1}]",web.Title, web.Id);
The above code will print the title and the id for the Web.
ClientObject class is the base class for all Client OM object. ClientObject exposes the following important methods that code will use again and again.
IsPropertyAvailable(string propertyName) - Returns a flag that indicates whether the specified property has been retrieved or set, or has not been retrieved or set.
Retreive() - Retreives all properties associated with the object
Retreive(params string[] propertyNames) - Retreives the specified properties associated with the object
All classes deriving from ClientObject class has a property names class ( for example, Web class has WebPropertyNames class ) which tells what properties can be fetched for the object.
Sample 1: Get all lists in web
The following sample will get all the lists in a web. For the list, the code will fetch the list id, list title and the type of the list.
public void GetAllList() { ClientContext context = new ClientContext("http://sharepoint"); ClientObjectPrototype allListsPrototype = context.Web.Lists.RetrieveItems(); allListsPrototype.Retrieve( ListPropertyNames.Title, ListPropertyNames.Id, ListPropertyNames.BaseType); context.ExecuteQuery(); foreach (SPClient.List list in context.Web.Lists) { Console.WriteLine("List : {0}, Id: {1}, BaseType : {2}", list.Title, list.Id, list.BaseType); } }
Sample 2: Get list details
In this sample, given a list the code gets the details for the list. The details include the Fields and Views of the list.
public void GetListDetails(string listName) { ClientContext context = new ClientContext("http://sharepoint"); List list = context.Web.Lists.GetByTitle(listName); // get fields name and their types ClientObjectPrototype allFieldsPrototype = list.Fields.RetrieveItems(); allFieldsPrototype.Retrieve( FieldPropertyNames.Id, FieldPropertyNames.Title, FieldPropertyNames.FieldTypeKind); // get view title ClientObjectPrototype allViewsPrototype = list.Views.RetrieveItems(); allViewsPrototype.Retrieve( ViewPropertyNames.Id, ViewPropertyNames.Title); context.ExecuteQuery(); foreach (Field field in list.Fields) { Console.WriteLine("Field '{0}', Type : {1}", field.Title, field.FieldTypeKind); } ViewCollection views = list.Views; foreach (View view in views) { Console.WriteLine("View '{0}', Id : {1}", view.Title, view.Id); } }
Sample 3: Get list items
The following code gets the list items in the given list.
public void GetListItems(string listName) { // build the CAML query to get ALL items CamlQuery query = new CamlQuery(); query.ViewXml = ""; ClientContext context = new ClientContext("http://sharepoint"); List list = context.Web.Lists.GetByTitle(listName); ListItemCollection items = list.GetItems(query); items.RetrieveItems().Retrieve(); context.ExecuteQuery(); foreach (ListItem item in items) { // assumes that the list has a field with name 'Title' Console.WriteLine("Item : {0}, Id : {1}", item.FieldValues["Title"], item.Id); } }
Sample 4: Add item to a list
The following code adds a list item in the given list.
public void AddItemToList(string listName) { ClientContext context = new ClientContext("http://sharepoint"); List list = context.Web.Lists.GetByTitle(listName); ListItemCreationInformation lic = new ListItemCreationInformation(); ListItem item = list.AddItem(lic); //add the item information item["Title"] = "Adding a new Item"; // List has a field name Checkbox which is a checkbox type field item["Checkbox"] = true; item.Update(); item.Retrieve(ListItemPropertyNames.Id); context.ExecuteQuery(); Console.WriteLine("Id : {0}", item.Id); }
Client OM is very useful however it takes time to understand how to use the API and get the best out of it. Enjoy !!
Friday, January 22, 2010
Reading BDC model properties in .NET Assembly Connector
Introduction
BDC in SharePoint 2010 support connectors for "Web Service", "Wcf Service", "Database", ".NET Assembly" and "Custom". ( BDC in MOSS 2007 has support for "Web Service" and "Database" only ).
.NET Assembly Connector basically allows to host a virtual LobSystem. The concept for .NET assembly connector is very similar to my suggestions of hosting .NET assembly in MOSS 2007 - BDC : Beyond Web-Service and Database. Since .NET assembly is natively supported in SharePoint 2010, it has way more capabilities than my suggested approach. The following article (http://msdn.microsoft.com/en-us/library/aa868997.aspx) explains how to create a .NET Assembly Connector in BDC.
This blog explains how you can use BDC model properties within your .NET assembly code.
IContextProperty Interface
To use the BDC model in the .NET assembly, lets first take a look at the IContextProperty interface [r1]. IContextProperty interface is defined in Microsoft.BusinessData.SystemSpecific namespace ( in Microsoft.BusinessData.dll )
.NET assembly connector in BDC uses IContextProperty interface to communicate the context in which BDC is executing the method in the class. If your class implements IContextProperty interface, BDC will initialize the three properties of the interface. Here is an example how you can write your class to implement the interface.
At this time "MyClass" class is ready to get the context from BDC.
Defining properties in BDC Model
Depending on what kind of information you need in the .NET code, you can define your property at appropriate element in the BDC model. For example, if your class needs to connect to different server for different region, LobSystemInstance would be a good place to define your property. In the case method needs to know which Locale is preferred by the user, it can look into the properties of Method or MethodInstance.
In this example, the class connects to a different database server based on which region the code is getting executed.
The above code relies on the region information to format DB connection string. Unfortunately, when the code gets executed in SharePoint, the method has no idea where it is being executed and would default to using north america db server.
To solve this issue, you can define a property in the BDC model which tells the code which DB server to use. Lets say the property name is "Region" and is defined within the LobSystemInstance.
In this example, different LobSystemInstances are used for each region, where the "Region" property is defined in LobSystemInstance. This is particularly useful when the user creates an external list. The user can choose the appropriate LobSystemInstance depending on its region.
Reading properties in the method
So now that the properties are defined in the model, its time to read the property in the code. The class "MyClass" exposes a method ReadAllItems for Finder stereotype. In this method, LobSystemInstance property returns the LobSystemInstance (BDC context). LobSystemInstance.GetProperties() method returns all the properties defined for the LobSystemInstance.
Once we have all the properties, the code checks if the "Region" property has been defined or not and reads the "Region" property accordingly. Once the Region property is read, it is used to create the DB connection string and the DB query is executed against that particular DB server.
Conclusion
BDC model is the right place to define custom properties. These properties should be used in .NET assembly connector code to customize solutions.
References
r1: IContextProperty interface: http://msdn.microsoft.com/en-us/library/microsoft.businessdata.systemspecific.icontextproperty(office.14).aspx
BDC in SharePoint 2010 support connectors for "Web Service", "Wcf Service", "Database", ".NET Assembly" and "Custom". ( BDC in MOSS 2007 has support for "Web Service" and "Database" only ).
.NET Assembly Connector basically allows to host a virtual LobSystem. The concept for .NET assembly connector is very similar to my suggestions of hosting .NET assembly in MOSS 2007 - BDC : Beyond Web-Service and Database. Since .NET assembly is natively supported in SharePoint 2010, it has way more capabilities than my suggested approach. The following article (http://msdn.microsoft.com/en-us/library/aa868997.aspx) explains how to create a .NET Assembly Connector in BDC.
This blog explains how you can use BDC model properties within your .NET assembly code.
IContextProperty Interface
To use the BDC model in the .NET assembly, lets first take a look at the IContextProperty interface [r1]. IContextProperty interface is defined in Microsoft.BusinessData.SystemSpecific namespace ( in Microsoft.BusinessData.dll )
namespace Microsoft.BusinessData.SystemSpecific { public interface IContextProperty { IExecutionContext ExecutionContext { get; set; } ILobSystemInstance LobSystemInstance { get; set; } IMethodInstance MethodInstance { get; set; } } }
Figure 1: IContextProperty interface
.NET assembly connector in BDC uses IContextProperty interface to communicate the context in which BDC is executing the method in the class. If your class implements IContextProperty interface, BDC will initialize the three properties of the interface. Here is an example how you can write your class to implement the interface.
using Microsoft.BusinessData.SystemSpecific; namespace MyNamespace { public class MyClass : IContextProperty { private IExecutionContext context; private ILobSystemInstance lobSystemInstance; private IMethodInstance methodInstance; #region Implementing IContextProperty interface public IExecutionContext ExecutionContext { get { return this.context; } set { this.context = value; } } public ILobSystemInstance LobSystemInstance { get { return this.lobSystemInstance; } set { this.lobSystemInstance = value; } } public IMethodInstance MethodInstance { get { return this.methodInstance; } set { this.methodInstance= value; } } #endregion } }
Figure 2: IContextProperty implementation
At this time "MyClass" class is ready to get the context from BDC.
Defining properties in BDC Model
Depending on what kind of information you need in the .NET code, you can define your property at appropriate element in the BDC model. For example, if your class needs to connect to different server for different region, LobSystemInstance would be a good place to define your property. In the case method needs to know which Locale is preferred by the user, it can look into the properties of Method or MethodInstance.
In this example, the class connects to a different database server based on which region the code is getting executed.
private string GetDbConnectionString(string region) { string dbConnectionStringFormat = "Data Source={0};Initial Catalog=myDataBase;Integrated Security=SSPI;"; // default DB Server string regionDbServer = "northamericaDBServer"; if (region == "emea") { regionDbServer = "emeaDbServer"; } else if (region == "asia") { regionDbServer = "asiaDbServer"; } return string.Format(dbConnectionStringFormat, region); }
Figure 3: Customize connection string
The above code relies on the region information to format DB connection string. Unfortunately, when the code gets executed in SharePoint, the method has no idea where it is being executed and would default to using north america db server.
To solve this issue, you can define a property in the BDC model which tells the code which DB server to use. Lets say the property name is "Region" and is defined within the LobSystemInstance.
<LobSystemInstances> <LobSystemInstance Name="NorthAmerica"> <Properties> <Property Name="Region" Type="System.String">northamerica</Property> </Properties> </LobSystemInstance> <LobSystemInstance Name="Default"> <Properties> <Property Name="Region" Type="System.String">northamerica</Property> </Properties> </LobSystemInstance> <LobSystemInstance Name="EMEA"> <Properties> <Property Name="Region" Type="System.String">emea</Property> </Properties> </LobSystemInstance> <LobSystemInstance Name="Asia"> <Properties> <Property Name="Region" Type="System.String">asia</Property> </Properties> </LobSystemInstance> </LobSystemInstances>
Figure 4: BDC model properties
In this example, different LobSystemInstances are used for each region, where the "Region" property is defined in LobSystemInstance. This is particularly useful when the user creates an external list. The user can choose the appropriate LobSystemInstance depending on its region.
Reading properties in the method
So now that the properties are defined in the model, its time to read the property in the code. The class "MyClass" exposes a method ReadAllItems for Finder stereotype. In this method, LobSystemInstance property returns the LobSystemInstance (BDC context). LobSystemInstance.GetProperties() method returns all the properties defined for the LobSystemInstance.
public MyItems ReadAllItems() { // get the LobSystemInstance properties via context from BDC INamedPropertyDictionary properties = this.LobSystemInstance.GetProperties(); string region = null; // search for "Region" property in model if (properties.ContainsKey("Region")) { region = properties["Region"] as string; } //get the db connection string string dbConnectionString = GetDbConnectionString(region); //do normal db processing //return the items for method }
Figure 5: Use the BDC model property
Once we have all the properties, the code checks if the "Region" property has been defined or not and reads the "Region" property accordingly. Once the Region property is read, it is used to create the DB connection string and the DB query is executed against that particular DB server.
Conclusion
BDC model is the right place to define custom properties. These properties should be used in .NET assembly connector code to customize solutions.
References
r1: IContextProperty interface: http://msdn.microsoft.com/en-us/library/microsoft.businessdata.systemspecific.icontextproperty(office.14).aspx
Labels:
.Net Assembly Connector,
BCS,
BDC,
SharePoint 2010
Subscribe to:
Posts (Atom)