What is it?
The Stream Accessor method allows you to pull System.Byte[] data from your External System and make it available from within SharePoint.
Why would I want to do it?
If you have documents or images stored in your External Systems then it is possible using the Business Connectivity Services (BCS) give your end users the ability to access and download these artifacts with ease.
How to do it?
We are going to be using BCS Meta Man to create our 3 standard methods (Finder Method, Specific Finder Method and IdEnumerator Method)
Walkthrough
- Create a new empty SharePoint 2010 project in Visual Studio 2010.
- Add a new item to this project. From the Lightning Tools project items group add a BCS Meta Man project item
- From the main menu click on BCS Meta Man –> BCS Meta Man Data Explorer and connect to your database you wish to use.
- Drag and Drop any table you want. For this walkthrough we have used the AdventureWorks2000 Product table.
- Select Database as the ‘Model Type’ when prompted
- Save the generated Model
- Right Click on the ‘*.bcsmm’ model in ‘Solution Explorer’ and click ‘Open With…’
- Choose ‘XML (Text) Editor’
- This will show you our generated Model (This is similar to how we had to develop the Application Definition File in SharePoint 2007)
- Collapse the ‘Model’ to the following so that you can see the 3 generated Methods
- After the final Method paste the following code in:
<Method Name="GetPhoto"> <Properties> <Property Name="RdbCommandText" Type="System.String">select "ThumbNailPhoto" from "SalesLT"."Product" where "ProductID"=@ProductID</Property> <Property Name="RdbCommandType" Type="System.Data.CommandType, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">Text</Property> </Properties> <Parameters> <Parameter Direction="In" Name="@ProductID"> <TypeDescriptor TypeName="System.Int32" IdentifierName="ProductID" Name="ProductID" /> </Parameter> <Parameter Name="StreamData" Direction="Return"> <TypeDescriptor TypeName="System.Data.IDataReader, System.Data, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" IsCollection="true" Name="StreamData"> <TypeDescriptors> <TypeDescriptor TypeName="System.Data.IDataRecord, System.Data, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Name="StreamData Element"> <TypeDescriptors> <TypeDescriptor TypeName="System.Byte[]" Name="ThumbNailPhoto" /> </TypeDescriptors> </TypeDescriptor> </TypeDescriptors> </TypeDescriptor> </Parameter> </Parameters> <MethodInstances> <MethodInstance Type="StreamAccessor" ReturnParameterName="StreamData" ReturnTypeDescriptorPath="StreamData[0].ThumbNailPhoto" Default="true" Name="ThumbNailPhoto"> <Properties> <Property Name="MimeType" Type="System.String">image/gif</Property> <Property Name="FileNameField" Type="System.String">ThumbnailPhotoFileName</Property> </Properties> </MethodInstance> </MethodInstances> </Method>
- If we look at each line we can see what we are achieving
- The Property here is our SQL query to return just the ThumbNailPhoto column from our Products Database, we have a where clause of the ProductID
<Property Name=“RdbCommandText” Type=“System.String“>select “ThumbNailPhoto” from “SalesLT”.”Product” where “ProductID”=@ProductID</Property>
- Next we define our In parameter, the parameter which is being supplied to the SQL query
<Parameter Direction=“In” Name=“@ProductID“> <TypeDescriptor TypeName=“System.Int32” IdentifierName=“ProductID” Name=“ProductID” /> </Parameter>
- Next we define the Data coming back from our method, this is our ThumbNailPhoto which is a System.Byte array.
- The Method Instance tells the BCS that this is a ‘StreamAccessor’ method and also tells SharePoint what the ‘MimeType’ of the Data is and also which Field you want to display the link on for the Data
- The Property here is our SQL query to return just the ThumbNailPhoto column from our Products Database, we have a where clause of the ProductID
- Press F5 to deploy, this will load up your SharePoint Page once deployed
- Add a new Business Data List Web Part to your SharePoint page
- Click on the ‘Open the tool pane’ link
- Click on the icon to show the available External Content Types
- Select the External Type of the Product model we created, Click OK
- Click OK on the Tool Part
- The return data should now have a ‘Click here to download’ link which will download the file for you.
Although this post contains some manual steps, we’ll soon have BCS Meta Man generating it all automatically for you! Watch this space!
<phill/>