Stream Accessor BCS Method

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

  1. Create a new empty SharePoint 2010 project in Visual Studio 2010.
  2. Add a new item to this project. From the Lightning Tools project items group add a BCS Meta Man project item
  3. From the main menu click on BCS Meta Man –> BCS Meta Man Data Explorer and connect to your database you wish to use.
  4. Drag and Drop any table you want. For this walkthrough we have used the AdventureWorks2000 Product table.
  5. Select Database as the ‘Model Type’ when prompted
  6. Save the generated Model
  7. Right Click on the ‘*.bcsmm’ model in ‘Solution Explorer’ and click ‘Open With…’

     Open model file with XML editor

  8. Choose ‘XML (Text) Editor’
  9. This will show you our generated Model (This is similar to how we had to develop the Application Definition File in SharePoint 2007)
  10. Collapse the ‘Model’ to the following so that you can see the 3 generated Methods

    Collapse BCS Model methods

  11. 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>

  12. 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=RdbCommandTextType=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=InName=@ProductID> <TypeDescriptor TypeName=System.Int32IdentifierName=ProductIDName=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
  13. Press F5 to deploy, this will load up your SharePoint Page once deployed
  14. Add a new Business Data List Web Part to your SharePoint page
  15. Click on the ‘Open the tool pane’ link
  16. Click on the icon to show the available External Content Types

    Select the External Content Type

  17. Select the External Type of the Product model we created, Click OK
  18. Click OK on the Tool Part
  19. 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/>