Using the library in your Expression Blend 2 Project
In order to start using the Silverlight SPInterface library in an
Expression Blend project you will need to first add the library as a resource to the
Expression Blend project. In you are a beginner to
Silverlight and
Expression Blend, just follow the simple steps, outlined below, to get started.
STEP 1: Download the library
Visit the
Silverlight SPInterface Download page and download the zipped library file.
STEP 2: Extract the library files
Open the zipped library file and extract the contents to a new directory.
STEP 3: Create a Project
Create an
Expression Blend Project
STEP 4: Add a Reference
Once you have created an Expression Blend project you must first add a reference to the Silverlight SPInterface Library before in can be used in your application.
To do this go to the project explorer window and right-click on the references folder. Then click on "Add Reference", as in the picture below:
 Adding a Reference |
Then navigate to the DaisleyHarrison.SPInterface.dll file you extracted from the library download zip file, and click open.
 Find the Daisley-Harrison.SPInterface.dll |
Adding the SPListDataSource as a Resource to your Silverlight page
STEP 1: Create a namespace prefix in XAML
Add the namespace prefix for the SPInterface library namespace to your SilverLight Page or User Control
For example:
For a new page
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:spinterface="clr-namespace:DaisleyHarrison.SPInterface;assembly=DaisleyHarrison.SPInterface"
x:Class="SilverlightApplication2.Page"
Width="640" Height="480">
<Grid x:Name="LayoutRoot" Background="White"/>
</UserControl>
or for a new user control
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:spinterface="clr-namespace:DaisleyHarrison.SPInterface;assembly=DaisleyHarrison.SPInterface"
mc:Ignorable="d"
x:Class="SharePointListTest.UserControl1"
d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot"/></UserControl>
STEP 2: Add a Resources section
Add a resources section to you XAML page or user control. For example:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:spinterface="clr-namespace:DaisleyHarrison.SPInterface;assembly=DaisleyHarrison.SPInterface"
x:Class="SilverlightApplication2.Page"
Width="640" Height="480">
<UserControl.Resources>
</UserControl.Resources>
...
</UserControl>
STEP 3: Add the SPListDataSource as a Resource
Add the SPListDataSource as a resource to your new resources section. You will need to give the resource a unique key name.
To read SharePoint list data you must specify at least the SiteUrl and the ListName property. For additional optional property names that can be used to further defined how the SPListDataSource works please see
SPListDataSource Properties below.
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:spinterface="clr-namespace:DaisleyHarrison.SPInterface;assembly=DaisleyHarrison.SPInterface"
x:Class="SilverlightApplication2.Page"
Width="640" Height="480">
<UserControl.Resources>
<spinterface:SPListDataSource x:Key="MyData" SiteUrl="http://mysharepointhost.com/sites/mysite" ListName="MyList"/>
</UserControl.Resources>
...
</UserControl>
If you would also like to access individual data fields by name, you will also need to include the SPListItemConverter in the resource section also. See below:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:spinterface="clr-namespace:DaisleyHarrison.SPInterface;assembly=DaisleyHarrison.SPInterface"
x:Class="SilverlightApplication2.Page"
Width="640" Height="480">
<UserControl.Resources>
<spinterface:SPListDataSource x:Key="MyData" SiteUrl="http://mysharepointhost.com/sites/mysite" ListName="MyList"/>
<spinterface:SPListItemConverter x:Key="FromListItem"/>
</UserControl.Resources>
...
</UserControl>
SPListDataSource Properties
The SPListDataSource class contains the following Properties that can be set from XAML:
SiteUrl
The SharePoint site URL from which to retrieve list data.
Example
<spinterface:SPListDataSource x:Key="MyData" SiteUrl="http://mysharepointhost.com/sites/mysite" ListName="MyList"/>
ListName
The name of the SharePoint list from which to retrieve list data.
Example
<spinterface:SPListDataSource x:Key="MyData" SiteUrl="http://mysharepointhost.com/sites/mysite" ListName="MyList"/>
ViewName
The name of the SharePoint list view from which to retrieve list data. If no view name is specified then the default view specified by the SharePoint list. The list view will be used to determine which fields, and in, what order they will be returned.
Example
<spinterface:SPListDataSource x:Key="MyData" SiteUrl="http://mysharepointhost.com/sites/mysite" ... ViewName="MySpecialView"/>
RowLimit
The RowLimit parameter allows you to specify the maximum number of rows to be returned by the SPListDataSource. If no RowLimit is specified then all rows will be returned.
Example
<spinterface:SPListDataSource x:Key="MyData" SiteUrl="http://mysharepointhost.com/sites/mysite" ... RowLimit="20"/>
FieldNames
A comma delimited list of field names to be returned from the SharePoint list. If no fields are specified then all fields specified by the SharePoint view being referenced will be returned. Note if no ViewName parameter is specified then the fields and field order specified default view specified by the SharePoint list will be used.
If field names are specified, then fields will be returned in the order specified in this list.
Example
<spinterface:SPListDataSource x:Key="MyData" SiteUrl="http://mysharepointhost.com/sites/mysite" ... FieldNames="Clicks,ID,Rating,URLwMenu,Notes"/>
Binding to
Value0 would return data in the field named "Clicks",
Value1 would return the field named "ID", etc.
Timeout
The Timeout parameter allows you to specify the maximum amount of time to when for the SharePoint list to respond with data. The time out is specified in milliseconds. If no Timeout parameter is specified then the default timeout of 60000 or one minute will be used.
Example
Setting the timeout to 20 seconds:
<spinterface:SPListDataSource x:Key="MyData" SiteUrl="http://mysharepointhost.com/sites/mysite" ... Timeout="20000"/>
Binding Silverlight Controls to the SPListDataSource
The absolute minimum necessary to bind a control to the SPListDataSource resource is to bind the ItemsSource of the control as follows:
<ListBox ... ItemsSource="{StaticResource MyData}"/>
If properly constructed this will result in a few test rows being displayed in the designer. For example:
 Designer Sample Lines |
NOTE: When a data template has not been specified the default behavior is to display the text of the list items which are represented as an XML row set. To display individual fields of SharePoint list items it will be necessary to create a data template to format them appropriately.
Binding To A List Item Data Field
Binding to the first ten list item data fields can be done without specifing the field name. The list items returned by the SPListDataSource have a similified set of property names built-in to access the first ten data fields.
- For numeric data types: NumericValue0, NumericValue1, NumericValue2, … NumericValue9.
- For boolean data types: BooleanValue0, BooleanValue1, BooleanValue2, … BooleanValue9.
- For any other object data types: Value0, Value1, Value2, … Value9.
Note that the Silverlight binding expression syntax does not allow you to specify an index, like "ListItem.Fields[3]", so the library cheats a little by hard-coding the first ten property names.
The order that the list item fields are returned in is specified by:
- The order of the fields in specified by view of the SharePoint list.
- This is the default view unless a specific view is specified by the ViewName property of SPListDataSource.
- The order of the fields specified in the Fields property.
- If no Fields property is specified then fields are returned in view order.
To access field data by field name see
Binding To A List Item Data Field By Name below.
Examples
<ListBox ItemsSource="{StaticResource MyData}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value0}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
or
<ListBox Height="112" Margin="104,56,120,0" VerticalAlignment="Top" ItemsSource="{StaticResource MyData}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Value0}"/>
<TextBlock Text="{Binding Value1}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Binding To A List Item Data Field By Name
Binding to a list item data field by name requires the use of the SPListItemConverter that comes as part of the library. To use the converter, it must first be referenced in the Resources section of your Silverlight page .
For example:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:spinterface="clr-namespace:DaisleyHarrison.SPInterface;assembly=DaisleyHarrison.SPInterface"
x:Class="SilverlightApplication2.Page"
Width="640" Height="480">
<UserControl.Resources>
<spinterface:SPListDataSource x:Key="MyData" SiteUrl="http://mysharepointhost.com/sites/mysite" ListName="MyList"/>
<spinterface:SPListItemConverter x:Key="FromListItem"/>
</UserControl.Resources>
...
</UserControl>
Once the SPListItemConverter is part of the Resouces section of the page it is fairly straight forward to use it to access fields by name.
For example:
<ListBox Height="112" Margin="104,56,120,0" VerticalAlignment="Top" ItemsSource="{StaticResource MyData}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Value, Converter={StaticResource FromListItem}, ConverterParameter=Author}"/>
<TextBlock Text="{Binding Value1}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The SPListItemConverter is actual quite powerful and allows you to specify a number of parameters to control how data is access from different SharePoint data types (see the
SPListConverter Parameters section below).
SPListItemConverter Parameters
SPLIstItemConverter parameter are specified it two ways. The first is to specify global parameter in the resource declaration, the second is two specify a comma delimited list of parameter using the
ConverterParameter keyword in each binding. The first parameter specified in the comma delimited list is always the
field name.
BaseUri (global)
Currently the only global parameter that can be specified is
BaseUri, which defines the base url used to resolve any relative urls returned in the list data.
BaseUrl example
<spinterface:SPListItemConverter x:Key="FromListItem" BaseUri="http://someserver.com/something"/>
baseuri=
Setting the baseuri as a ConverterParameter overrides any setting made to the global BaseUri setting in the Resources section.
Example
<TextBlock Text="{Binding Value, Converter={StaticResource FromListItem}, ConverterParameter=URL,baseuri=http://myotherserver.com/here}"/>
Html
Indicates that the field is the be treated as if it contains Html data. This parameter should be applied to any RTF fields in order for any HTML encoding to be removed from the data before it is displayed.
Example
<TextBlock Text="{Binding Value, Converter={StaticResource FromListItem}, ConverterParameter=My RTF Field Name,Html}"/>
item=
Indicates the item number to be returned when the field contains multiple values. Note that this parameter is only recognized if the
Multi or
MultiLookup parameter is also specified.
Example
The follow example returns the third value of a field containing multiple values:
<TextBlock Text="{Binding Value, Converter={StaticResource FromListItem}, ConverterParameter=MyFieldName,Multi,item=3}"/>
Multi
Specifies that the field data returned from the SharePoint list contains multiple values. When specified, the
item= parameter can also be used.
Example
The follow example returns the third value of a field containing multiple values:
<TextBlock Text="{Binding Value, Converter={StaticResource FromListItem}, ConverterParameter=MyFieldName,Multi,item=3}"/>
MultiLookup
Specifies that the field data returned from the SharePoint list contains multiple lookup values. When specified, the
item= parameter can also be used.
Note a
MultiLookup field is actual a
Muti field that contains a pair of values for each lookup, the first being the reference ID to the referenced list, the second being the actual reference value to be displayed. When
MultiLookup is specified then only the display values are returned.
Example
The follow example returns the third value of a field containing multiple lookup values:
<TextBlock Text="{Binding Value, Converter={StaticResource FromListItem}, ConverterParameter=MyFieldName,MultiLookup,item=3}"/>
Uri
Specifies that the field data returned from the SharePoint list contains a URL values. The URL of the field will be resolved against the base uri and returned as an absolute URI. Note that if neither the local baseuri= or global BaseUri parameters are specified then a relative URI will be returned.
Example
The follow example returns the third value of a field containing multiple values:
<TextBlock Text="{Binding Value, Converter={StaticResource FromListItem}, ConverterParameter=MyFieldName,Uri}"/>
Url
Specifies that the field data returned from the SharePoint list contains a URL value. The URL portion of the field data will be returned. Note that SharePoint URL field data contains both a URL and a Title sepparated by a comma.
Example
The follow example returns the third value of a field containing multiple values:
<TextBlock Text="{Binding Value, Converter={StaticResource FromListItem}, ConverterParameter=MyFieldName,Url}"/>
UrlTitle
Specifies that the field data returned from the SharePoint list contains a URL value. The
title portion of the field data will be returned. Note that SharePoint URL field data contains both a URL and a Title sepparated by a comma.
Example
The follow example returns the third value of a field containing multiple values:
<TextBlock Text="{Binding Value, Converter={StaticResource FromListItem}, ConverterParameter=MyFieldName,UrlTitle}"/>
Preparing your SharePoint site
If you are only running your Silverlight application from your SharePoint site (see
Adding the Silverlight Control to your SharePoint Page below) then the SPInterface library should be able to ready your list data without additional changes to your SharePoint site. Make sure that the account you are running under has permission to see the list data.
If you are planning on debugging the Silverlight application from a remote machine, like your laptop, and you wish to access a SharePoint site on another machine, you will most likely have to install a cross domain policy file on the root of your site before you can access that sites list web services (how SPInterface communicates with SharePoint).
To do this, open your site using Micrsoft Office SharePoint Designer and place a file called "crossdomain.xml" on the root directory of your Site. For debugging the cross domain policy file could be as simple as the following code:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-http-request-headers-from domain="*" headers="*"/>
</cross-domain-policy>
NOTE: The above cross domain policy file should be used for
DEBUGGING ONLY, for production sites you should consider restricting access to only those domains that actual require access.
For more information on cross domain policy files see
Network Security Access Restrictions in Silverlight 2.
Adding Silverlight Controls to SharePoint Pages
There currently are a couple of ways to add a Silverlight control to your SharePoint Page. The simplest approach is to use the Silverlight web part that is avaliable on CodePlex. If you do not have adminstrator access to the SharePoint server or access to an administrator you can still run Silverlight application on your SharePoint page using the content editor WebPart and Microsoft SharePoint Desiger. See below for more details on each approach.
CodePlex Silverlight WebPart
There is a fairly simple to use Silverlight WebPart that is available from
CodePlex. If you have administrator priviledges on your SharePoint server or can get someone to install this WebPart, this would be the best approach if you are planning on using several Silverlight applications on your server. The WebPart can be found at
SilverPart.
Content Editor WebPart
Silverlight applications can be embedded on your SharePoint pages by using a
Content Edtior Web Part, which is already built in to SharePoint 2007.
Step1: First you will need to prepare your SharePoint page to use the Silverlight application on. Edit the page using Microsoft SharePoint Designer and add the follow script to the page. If you are wanting to use Silverlight on several pages it is a good idea to add the script to your master page.
Javascript to be added to your page or master page:
<script type="text/javascript">
//<![CDATA[
function onSilverlightError(sender, args) {
var appSource = "";
if (sender != null && sender != 0) {
appSource = sender.getHost().Source;
}
var errorType = args.ErrorType;
var iErrorCode = args.ErrorCode;
var errMsg = "Unhandled Error in Silverlight 2 Application " + appSource + "\n" ;
errMsg += "Code: "+ iErrorCode + " \n";
errMsg += "Category: " + errorType + " \n";
errMsg += "Message: " + args.ErrorMessage + " \n";
if (errorType == "ParserError")
{
errMsg += "File: " + args.xamlFile + " \n";
errMsg += "Line: " + args.lineNumber + " \n";
errMsg += "Position: " + args.charPosition + " \n";
}
else if (errorType == "RuntimeError")
{
if (args.lineNumber != 0)
{
errMsg += "Line: " + args.lineNumber + " \n";
errMsg += "Position: " + args.charPosition + " \n";
}
errMsg += "MethodName: " + args.methodName + " \n";
}
throw new Error(errMsg);
}
//]]>
</script>
Step 2: If you don't already have a document library on your site, go ahead and create one. The example below uses a document library called "SiteComponents". Add your compiled Silverlight XAP file to the document library, so it can be referenced from the page.
Step 3: Add a content editor web part to your page and using the source editor of the content editor web part add the following HTML code, substituting your Silverlight application xap file path:
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
<param name="source" value="http://aarondh-laptop/SiteComponents/MySilverlightApp.xap"/>
<param name="onerror" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="2.0.31005.0" />
<param name="autoUpgrade" value="True" />
<param name="InitParams" value="" />
<a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>
</a>
</object>
<iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe>
SharePoint 2010
SharePoint 2010 will come with a built-in Silverlight WebPart, however it is not much more functional that the Silverlight WebPart available from
CodePlex (see above).