金宝搏188亚洲体育真人始乐

SharePoint 2010 Development with Silverlight: Web Part Development

Date: Dec 8, 2011

Sample Chapter is provided courtesy ofAddison-Wesley Professional.

Return to the article

学习如何手动创建Silverlight Web部件that can host a Silverlight application in SharePoint, how to use the Silverlight Web Part extension to help you jump start your Silverlight Web Part projects, and how to connect web parts so you can show master-detail relationships, dashboards, and mash-ups.

WEB PARTS ARE ONE of the most fundamental user interface components in SharePoint. Web parts enable developers to create visual components that can be configured by end users. This is core to the concepts of SharePoint as a composite application model in that you can compose applications from smaller building blocks, such as web parts. Out-of-the-box, Visual Studio supports creating web parts and Visual Web Parts. The difference between the two Visual Studio projects is that Visual Web Parts can be created using a visual designer, and the web part template is written using code only. In this chapter you learn how to leverage Visual Studio to create Silverlight Web Parts. These are web parts that use Silverlight as the user interface.

Silverlight Web Parts

In Chapter 4, "A First Look at Silverlight in SharePoint," you saw a couple of techniques for hosting Silverlight in SharePoint using the built-in Silverlight Web Part and using the Content Editor Web Part (CEWP). Both of these techniques required you to manually upload the Silverlight application's .xap file to SharePoint and then to manually create a web part to host the .xap file. Using this method to host Silverlight is problematic for a couple of reasons. First, it is a totally manual process and as such is prone to user error. Doing this manually doesn't follow good application lifecycle management (ALM) practices such as using source control, testing, and deployment. To avoid all of these issues you want to also package all of your SharePoint applications into a SharePoint solution package, a .wsp file.

It is important to understand what is going on under the covers. First you will see how to manually build a Visual Studio project to package and deploy the Silverlight application. This process is not obvious and requires a number of steps. Because of this Microsoft has released a Silverlight Web Part extension project for Visual Studio that automates the process of creating a Silverlight Web Part. Later in the chapter you use this extension to build a Silverlight Web Part.

Manually Building a Silverlight Web Part

的first task you need to solve is how to package and deploy your Silverlight application in your SharePoint .wsp package. Start by creating a new Silverlight project in Visual Studio. Next, create an empty SharePoint project in the same Visual Studio solution that the Silverlight project is in. You will have something similar to the project structure inFigure 5.1.

Figure 5.1

Figure 5.1Default Silverlight and SharePoint projects

At this point you have two projects in the solution, Silverlight and SharePoint, but they are not connected in any way. Normally to reference another project, you add a project reference to the other project in the solution. A Visual Studio project reference will make sure to build the referenced project before including the output of the referenced project into the refering project. In the case of Silverlight this does not work because the output you want to reference is not a .dll but the .xap Silverlight application's package file. So the SharePoint tools had to come up with a different way to do this. This is the technique that is not totally obvious, but it is not too bad after you see it once.

Now that you have two projects, the task is to get the Silverlight .xap file into the SharePoint project. Modules are the ways in which you deploy files to SharePoint. Add a new Module Project Item to the SharePoint project in Visual Studio. The new Module Project Item contains a Sample.txt file and an Elements.xml file. The Elements.xml, shown in Listing 5.1, describes where to deploy the Sample.txt file. In this the URL property specifies a folder called Module1. If this folder does not exist in SharePoint, it is automatically created.

清单5。1. Elements.xml in a New Module

xmlversion= "1.0"encoding= "utf-8"?><Elementsxmlns="http://schemas.microsoft.com/sharepoint/"><ModuleName="Module1"><FilePath="Module1\Sample.txt"Url="Module1/Sample.txt"/>Module>Elements>

Go ahead and delete the Sample.txt file because it is not needed. You are now ready to add a reference to the Silverlight project. Click the Module1 node in the SharePoint project to view the Properties window. In the Properties window for the Module there is a property named Project Output References. This is a collection property; click the ellipse to open the Project Output References dialog window. Click the Add button to add a new reference. In the reference's properties, set the Deployment Type to Element File. Choose the Silverlight project from the Project Name drop-down property list. InFigure 5.2you can see that the Project Output References dialog adds a reference to the Silverlight project and adds the path to the Elements.xml file.

Figure 5.2

Figure 5.2Project Output References

Also notice that the Module folder only contains the Elements.xml file, which has been updated as shown in Listing 5.2.

清单5。2. Elements.xml to Deploy a Silverlight Application

xmlversion= "1.0"encoding= "utf-8"?><Elementsxmlns="http://schemas.microsoft.com/sharepoint/"><ModuleName="Module1"><FilePath="Module1\SilverlightApplication1.xap"Url="Module1/SilverlightApplication1.xap"/>Module>Elements>

This is because there is only a refence to the Silverlight application. The actual .xap is not stored in the SharePoint project. Visual Studio also ensures the Silverlight is built before the SharePoint project if it is dirty, just like it does when adding a project reference.

Build, package, and deploy the SharePoint project to SharePoint by pressing F5. Visual Studio deploys and activates the feature that then deploys the Silverlight application's .xap file; in this case to the Module1 folder. You can verify that the Silverlight application was deployed using SharePoint Designer. Open SharePoint Designer and browse to the All Files folder of the site you deployed the solution to. Under the All Files folder you see the Module1 folder, which contains the SilverlightApplication1.xap file that you just deployed, as shown inFigure 5.3.

Figure 5.3

Figure 5.3Verify the Silverlight deployment using SharePoint Designer

After you have deployed the solution, users can add Silverlight Web Parts to pages. All they need is the path to the deployed .xap file, for examplehttp://intranet.contoso.com/Module1/SilverlightApplication1.xap. You could also take the next step and add a web part directly to your project. But Microsoft has already done all of these steps for you in a Visual Studio extension, which you can download for free. Let's take a look at how to do this using the extension.

Visual Studio Silverlight Web Parts Extension

Microsoft has created a Visual Studio extension to automatically build Silverlight Web Parts. With this extension you can avoid doing all of the manual steps from the previous section. The extension also has a couple of other nice features, such as it automatically creates the web part for you and creates a test page that hosts the Silverlight Web Part. This makes it simple to add a Silverlight Web Part project item and press F5 and have a fully functional Silverlight Web Part created for you without any other steps for you to do.

Installing the Extension

的Silverlight Web Part project item templates are not part of Visual Studio out-of-the-box, so you need to download them from the Visual Studio Gallery and install them. But Visual Studio has made this process very easy and quick to not only install but uninstall as well. In fact it is built directly into Visual Studio.

Before you install the Silverlight SharePoint web parts extension, you need to install the Visual Studio 2010 SharePoint Power Tools extension. Click Tools and then Extension Manager from the main menu. In the Extension Manager Gallery click Online Gallery from the menu on the left. When the online gallery loads, enterVisual Studio 2010 SharePoint Power Toolsin the search box. Click the Download button. Follow the prompts to download and install the extension.

Click Tools and then Extension Manager from the main menu. In the Extension Manager Gallery click Online Gallery from the menu on the left. When the online gallery loads, enterSilverlight SharePoint Web Partsin the search box. Click the Download button, as shown inFigure 5.4. Follow the prompts to download and install the extension.

Figure 5.4

Figure 5.4Visual Studio Extension Manager

的extension does have a dependency on another extension that Microsoft ships called the Visual Studio 2010 SharePoint Power Tools, as shown inFigure 5.5. This extension adds support for sandboxed compatible Visual Web Parts. If you see this warning, click Close and install the Visual Studio 2010 SharePoint Power Tools first. Also be sure to restart Visual Studio after installing the power tools extension.

Figure 5.5

Figure 5.5Dependency Alert

Click Install to accept the EULA and install the extension, as shown inFigure 5.6.

Figure 5.6

Figure 5.6安装Visual Studio扩展

You can confirm that both extensions are installed from the Extension Manager dialog, as shown inFigure 5.7. In this case you can see that there is a warning to restart Visual Studio, which is required after installing any extension.

Figure 5.7

Figure 5.7Restart Visual Studio after installation

With the Silverlight SharePoint Web Part extension successfully installed you are ready to start creating Silverlight Web Parts. Let's look at the two different types of Silverlight Web Parts that you can create with the extension—Silverlight Web Parts and Custom Silverlight Web Parts.

Building a Silverlight Web Part

SharePoint ships with a web part for hosting Silverlight applications. The Silverlight Web Part project template uses this web part to host the Silverlight application. When developers create Silverlight applications for SharePoint, they generally follow one of two patterns. The first is that a Silverlight developer has created a Silverlight application and now wants to deploy it to SharePoint. The second pattern is that a SharePoint developer wants to add an existing Silverlight application to the SharePoint project. But what both of these patterns have in common is that you have one Silverlight and one SharePoint project in a single SharePoint solution and you want to connect them.

Let's take a look at an example of how to do this using the Silverlight Web Part extension project item. Create or open a Silverlight project in Visual Studio. In this example you can open the SLGrid Silverlight application that is located in the sample code, as shown inFigure 5.8.

Figure 5.8

Figure 5.8SLGrid.sln Silverlight application

A developer iterates on the Silverlight application using Expression Blend or Visual Studio until it is ready to be deployed to SharePoint. The SharePoint developer adds a new empty sandboxed SharePoint project to the SharePoint solution. There are a couple of housekeeping tasks you need to do because SharePoint was added after the Silverlight project. First, set the SharePoint project as the startup project. This causes the SharePoint project to deploy to SharePoint when you press F5. You should always enable Silverlight debugging. This setting is somewhat hidden on the SharePoint tab of the Project Properties page; most of the time you need to scroll down to see the setting, as shown inFigure 5.9.

Figure 5.9

Figure 5.9Enable Silverlight debugging

In your Visual Studio solution you now have one Silverlight and one SharePoint project. Technically it does not matter how you got to this state, whether you started with the SharePoint project or the Silverlight project. After you are in this state you can connect the two projects together by adding a Silverlight Web Part. Right-click the SharePoint project and add a new project item. In the new project item, click the Silverlight Web Part project item in the SharePoint\2010 node, as shown inFigure 5.10.

Figure 5.10

Figure 5.10Add a Silverlight Web Part

Click Add to create the Silverlight Web Part. The Silverlight Web Part project template sees that you only have one SharePoint and one Silverlight project so it automatically connects them. If you had zero or more than one Silverlight project you would be prompted to create a new Silverlight project or select the one you would like to connect. I don't recommend using this feature because the Silverlight project is created using a default name.

的Silverlight Web Part project item template does more than just add a Silverlight Web Part to your project. First, it creates a module to deploy the Silverlight .xap file. This is equivalent to the steps you did manually earlier in this chapter. The main difference is that the Silverlight application is deployed to the Master Pages gallery. Specifically it is deployed to the ClientBin folder, and a subfolder is created that matches the package name, which is typically the same as the Visual Studio project's name. The Visual Studio replacement token,$SharePoint.Package.Name$, is used to dynamically create the folder in the Elements.xml file as shown in Listing 5.3.

清单5。3. Elements.xml with SharePoint Package Name Token

xmlversion= "1.0"encoding= "utf-8"?><Elementsxmlns="http://schemas.microsoft.com/sharepoint/"><ModuleName="SLGrid"Url= "_catalogs/masterpage/ClientBin/$SharePoint.Package.Name$"> <FilePath= "SLGrid\SLGrid.xap"Url= "SLGrid.xap" /> Module></Elements>

的project item template also creates a Silverlight Web Part definition file using the built-in Silverlight Web Part. This is equal to you manually adding a Silverlight Web Part from the Web Parts gallery. Unlike doing this manually, the web part that is created has all of the properties already set, including the URL property, which points to the location of the Silverlight .xap file. You can edit the SLGridWebPart.webpart file's properties to change other values such as description, height, width, and title. The content of SLGridWebPart.webpart is shown in Listing 5.4.

清单5。4. SLGridWebPart.webpart File Sets WebPart Values

<webParts><webPartxmlns= " http://schemas.microsoft.com/WebPart/v3 " > <metaData> <typename= "Microsoft.SharePoint.WebPartPages.SilverlightWebPart, Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" /> <importErrorMessage>Cannot import this Web Part.importErrorMessage> metaData> <数据> <properties> <propertyname= "HelpUrl"type= "string" /> <propertyname= "AllowClose"type= "bool">Trueproperty> <propertyname= "ExportMode"type= "exportmode">Allproperty> <propertyname= "Hidden"type= "bool">Falseproperty> <propertyname= "AllowEdit"type= "bool">Trueproperty> <propertyname= "Direction"type= "direction">NotSetproperty> <propertyname= "TitleIconImageUrl"type= "string" /> <propertyname= "AllowConnect"type= "bool">Trueproperty> <propertyname= "HelpMode"type= "helpmode">Modalproperty> <propertyname= "CustomProperties"type= "string" null="true" /> <propertyname= "AllowHide"type= "bool">Trueproperty> <propertyname= "Description" type="string">SilverlightSLGridproperty> <propertyname= "CatalogIconImageUrl"type= "string" /> <propertyname= "MinRuntimeVersion"type= "string"null= "true" /> <propertyname= "ApplicationXml"type= "string" /> <propertyname= "AllowMinimize"type= "bool">Trueproperty> <propertyname= "AllowZoneChange"type= "bool">Trueproperty> <propertyname= "CustomInitParameters"type= "string"null= "true" /> <propertyname= "Height"type= "unit">480pxproperty> <propertyname= "ChromeType"type= "chrometype">Noneproperty> <propertyname= "Width"type= "单位" > < / 640 pxproperty> <propertyname= "Title"type= "string">SilverlightSLGridproperty> <propertyname= "ChromeState"type= "chromestate">Normalproperty> <propertyname= "TitleUrl"type= "string" /> <propertyname= "Url"type= "string">~site/_catalogs/masterpage/ClientBin/$SharePoint.Package. Name$/SLGrid.xap <propertyname= "WindowlessMode"type= "bool">Trueproperty> </properties> </data> </webPart></webParts>

的final item created by the project item template is a test page called SLGridWebPartPage.aspx that hosts the Silverlight Web Part, SLGridWebPart.webpart. This is a nice feature for developing the solution as you immediately have a page that you can run without taking any other steps. This page uses the SharePoint Wiki Page template. The SLGridWebPartPage.aspx page is a lot of standard wiki page code; the important section is at the very bottom where the Silverlight Web Part is hosted. This is equivalent to you creating a new wiki page in the Site Pages Library and inserting the SLGridWebPart onto the page. Although it is perfectly fine to ship this with the test page, developers generally delete it before going to production. You can see Listing 5.5 for the Silverlight Web Part that is inserted in the wiki field node of the wiki page.

清单5。5. Silverlight Web Part in the Page Wiki Field

<WebPartPages:SilverlightWebPartrunat= "server"Height= "480px"Url= "~site/_catalogs/masterpage/ClientBin /$SharePoint.Package.Name$/SLGrid.xap"ExportMode= "All"ChromeType= "None"ApplicationXml= ""HelpMode= "Modal"Description= "SLGrid Web Part"ID= "g_c24198d9_d504_4132_b56c_585e456d8855"Width= "640px"Title= "SLGrid"__MarkupType= "vsattributemarkup"__WebPartId= "{90D205F0-8BF4-4138-BCB5-7A947C14BDA9}"WebPart= "true"__designer:IsClosed= "false"></WebPartPages:SilverlightWebPart>

Though all of this detail is interesting, there is nothing that you need to change. You simply add the Silverlight Web Part and press F5 to deploy the solution to SharePoint. In this example the SLGrid application uses the client object model to display a grid of contacts, as shown inFigure 5.11.

Figure 5.11

Figure 5.11Silverlight Web Part test page

Adding Silverlight Web Parts using this Project Item template is very simple to use and deploy, but it has some issues. The first is that the built-in Silverlight Web Part has a five-second timeout. This means that your Silverlight application needs to load and start up in five seconds. This is perfect for small applications, but it might be a problem for larger applications or those on slow networks. This is where the Custom Silverlight Web Part comes in. It offers another way to host your Silverlight applications. The second issue is that it is not possible to customize the web part that hosts the Silverlight control. In the next section you learn how to create and extend a custom Silverlight Web Part.

Building a Custom Silverlight Web Part

A custom Silverlight Web Part is very similar to the Silverlight Web Part you created in the previous section. The only difference is that it uses a sandboxed Visual Web Part to host the Silverlight application as opposed to the built-in Silverlight Web Part control that ships with SharePoint. Using the sandboxed Visual Web Part has some advantages over the built in Silverlight Web Part host, such as not having the five-second timeout. A custom Silverlight Web Part enables you to interact with the web part page that the Silverlight application is hosted on. You see an example of this later in the chapter. You can also include other HTML items with your custom web part such as JavaScript, CSS, and images.

创建一个定制的Silverlight Web部件就像你did in the previous section. Add a Silverlight project and a SharePoint project to a Visual Studio solution. In this case you can use the SLGrid project that you used in the previous section. After you create the two projects, you need to connect them together. Open the Add New Item dialog for the SharePoint project. Choose the Silverlight Custom Web Part project item, as shown inFigure 5.12.

Figure 5.12

Figure 5.12Add a Custom Silverlight Web Part

的Visual Studio project has the same items as it did when you added a Silverlight Web Part. There is a module that deploys the Silverlight .xap file. There is a wiki test page that hosts the Silverlight Web Part. The difference is now there is a sandboxed Visual Web Part instead of just a .webpart definition file. The sandboxed Visual Web Part Project Item template comes from the Visual Studio 2010 SharePoint Power Tools extension, which you installed as a prerequisite to the Silverlight Web Parts extension. The sandboxed Visual Web Part is implemented as an ASP.NET user control (.ascx). What makes this user control special is that normally you cannot deploy user controls as part of a SharePoint sandboxed solution because the .ascx file must be written to the _layouts directory in SharePoint. But because this is a sandboxed solution, you are not allowed to do this. To work around this problem the Visual Studio team wrote a custom sandboxed Visual Web Part that compiles the .ascx control to code before deploying it to SharePoint. This avoids the file restrictions of the sandbox as there is nothing to write to the file system. The Silverlight Web Part extension takes advantage of this special Visual Web Part to host the Silverlight application, as shown inFigure 5.13. This is important because Silverlight runs on the client, so there should never be a restriction that it cannot run in a sandboxed solution. Also by using the Visual Web Part it makes it easier for developers to extend the web part using the Visual Design tools in Visual Studio.

Figure 5.13

Figure 5.13Custom Silverlight Web Part

的sandboxed Visual Web Part contains the code to host the Silverlight application. Open this page in Visual Studio to see the hosting code. This code is the same code that the Silverlight project generates in the test pages when you build the project. The only change to that generated code is the insertion of SharePoint tokens for thesourceandinitparams.

First is the Silverlight error handling code in Listing 5.6. This code is unchanged from what is generated by the Silverlight project system.

清单5。6. Error Handling Script in the Custom Silverlight Web Part

 <scripttype= "text/javascript"> function onSilverlightError(sender, args) { var appSource = ""; if (sender != null && sender != 0) { appSource = sender.getHost().Source; } var errorType = args.ErrorType; var iErrorCode = args.ErrorCode; if (errorType =="ImageError"|| errorType =="MediaError") { return; } var errMsg ="Unhandled Error in Silverlight Application "+ appSource +"\n"; errMsg +="Code: "+ iErrorCode +" \n"; errMsg +="Category: "+ errorType +" \n"; errMsg += "Message: " + args.ErrorMessage +" \n"; if (errorType =="ParserError") { errMsg +="File: "+参数。xamlFile +" \n"; errMsg +="Line: "+参数。lineNumber +" \n"; errMsg +="Position: "+参数。charPosition +" \n"; } else if (errorType =="RuntimeError") { if (args.lineNumber != 0) { errMsg +="Line: "+参数。lineNumber +" \n"; errMsg +="Position: "+参数。charPosition +" \n"; } errMsg +="MethodName: "+参数。methodName +" \n"; } throw new Error(errMsg); }</script>

的next section of code, shown in Listing 5.7, is the code that inserts Silverlight on the page. The two properties to call out aresourceandinitParams. Thesourceproperty is the URL to the Silverlight .xap file host in SharePoint. TheinitParamsare parameters passed to the Silverlight application when it is started. There is one special parameter calledMS.SP.url. TheMS.SP.urlparameter is used by the client object model to set theClientContext.Currentvalue. Without theMS.SP.urlparameter,ClientContext.Currentreturns null. You should always pass this parameter if you want to access the client context, even if you are creating your own Silverlight Web Parts.

清单5。7. Silverlight Object Tag in the Custom Silverlight WebPart

 <divid= "silverlightControlHost"style= "position:relative;height:480px; width:640px"> <object数据= "数据:application/x-silverlight-2,"type= "application/x-silverlight-2"width= "100%"height= "100%"> <paramname="source" value="<%= Microsoft.SharePoint.SPContext.Current.Web.Url %>/_catalogs/masterpage/ClientBin/SLGridCustomWebPartPackage/SLGrid.xap" /> <paramname= "onError"value= "onSilverlightError" /> <paramname= "background"value= "white" /> <paramname= "minRuntimeVersion"value= "4.0.50401.0" /> <paramname= "autoUpgrade"value= "true" /> <paramname= "initParams"value= "MS.SP.url=<%= Microsoft.SharePoint.Utilities.SPHttpUtility.HtmlEncode(Microsoft.SharePoint.SP Context.Current.Web.Url) %>" /> <ahref= "http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50401.0"style= "text-decoration: none"> <imgsrc="https://go.microsoft.com/fwlink/?LinkId=161376"alt= "Get Microsoft Silverlight"style= "border-style: none" /> a> object> <iframeid= "_sl_historyFrame"style= "visibility: hidden;height: 0px;width: 0px;border: 0px">iframe> div>

You could run the project at this point. There is nothing more you need to do to deploy this to SharePoint. But let's take a look at one more advantage of using the custom Silverlight Web Part by extending the application to interact with the user control hosting the Silverlight application. Open the Visual Web Part in the Visual Studio Code Editor and add the followingdivtag below the closing script tag and above the Silverlight object tag:

<divid= "SLDiv">div>

Thisdivtag could really go anywhere on the page. In this example it appears above the Silverlight application within the web part. Next you need to add some code to the SLGrid application's MainPage.xaml.cs file to populate thisdivtag with the currently selected user. In theLoadedevent of the MainPage, add the code to handle the selection changed event of the data grid as shown in Listing 5.8.

清单5。8. MainPage_Loaded Event

voidMainPage_Loaded(objectsender,RoutedEventArgse) { clientContext =ClientContext.Current; listDataGrid.SelectionChanged +=newlistDataGrid_SelectionChanged; RefreshData(); }

的selection changed event handler retrieves the current list item from the data grid. Note that because we are using data binding with the client object model, the list item is an actual SharePoint List Item object. Extract theFullNamefield from the list item and call theSetSLDiv()function.SetSlDiv()uses the Silverlight HTML Bridge to get a reference to theSLDivtag that you added to the user control. When you have a reference to thedivtag, you can set theinnerhtmlproperty with theFullNameof the list item, and you could even set other values such as the style properties. Add the code in Listing 5.9 to the MainPage.xaml.cs file in theSilverlight application SLGrid.

listing 5.9. Displaying SharePoint ListItem Properties in a Div Tag

voidlistDataGrid_SelectionChanged(objectsender,SelectionChangedEventArgse) {ListItemselectedListItem = (ListItem)listDataGrid.SelectedItem;stringfullName = "No Selected Item";if(selectedListItem !=null) fullName = selectedListItem["FullName"].ToString(); SetSLDiv(string.Format("

{0}

"
, fullName)); }private voidSetSLDiv(stringInnerHTML) {HtmlElementSLDiv =HtmlPage.Document.GetElementById("SLDiv");if(SLDiv !=null) { SLDiv.SetProperty("innerhtml", InnerHTML); SLDiv.SetStyleAttribute("color","blue"); } }

Run the project by pressing F5, which compiles both the Silverlight project and the SharePoint project. It packages the SharePoint project, adding a copy of the Silverlight .xap file to the package. F5 also deploys the package to the SharePoint sandboxed Solution Gallery, activates the solution, launches Internet Explorer, and attaches the Visual Studio debugger. You can see inFigure 5.14that Bob is selected in the Silverlight grid, thatdivtag has been set with his full name, and the color has been set to blue.

Figure 5.14

Figure 5.14Silverlight interacting with HTML

的custom Silverlight Web Part is as easy to use as the built-in Silverlight Web Part and opens up a number of new scenarios.

Connecting Web Parts

SharePoint web部件的一个很酷的功能是ability to connect them together. This allows business users to compose their own mash-ups and dashboards of related information. For example, a master/detail display could allow users to select an item in one web part and then see details and related information in other web parts on the page. The only thing is that SharePoint's web part connections run on the server, so a page refresh is required to update the connected web parts.

In this section, you will learn to build Silverlight web parts that can be connected, but since Silverlight runs on the client, the update will be immediate with no need for a page refresh. The strategy to do this is to use a SharePoint server-side web part connection to broker a direct Silverlight connection on the web page.Figure 5.15shows the web parts in the ConnectedWebParts sample in the code download. When the web parts are connected, anything that's typed into the source web part also appears in one or more connected target web parts.

Figure 5.15

Figure 5.15Connected web parts

You can try this on your development machine if you place the two web parts on the page. Edit either web part; then pull down the same drop-down next to the web part title you used to edit the web part. This time, a Connections choice appears to let you connect the web parts as shown inFigure 5.16.

Figure 5.16

Figure 5.16Connecting SharePoint web parts

Fortunately, SharePoint allows developers to create any kind of connection they like. In this case the connection is calledISilverlightConnection, and it defines a simple registration method for web parts that wish to connect. Listing 5.10 shows the interface.

清单5。10. The ISilverlightConnection Interface

public interface ISilverlightConnection { void RegisterReceiver(string receiverName); }

的ConnectionSource Web Part implements theISilverlightConnection, and ConnectionTarget consumes it. The strategy is for each ConnectionTarget to register a unique receiver name by calling theRegisterReceiver()method in the source. Both web parts then pass the receiver name to their corresponding Silverlight applications, which can then use Silverlight's messaging API to send messages. The ConnectionSource web part is capable of handling several receiver names if multiple target web parts are connected; go ahead and try this if you like. This is shown inFigure 5.17.

Figure 5.17

Figure 5.17Brokering Silverlight communication with a server-side connection

Using Silverlight in Composite Controls

的sad truth is that sandboxed solutions don't allow web part connections, and the Silverlight SharePoint Web Parts used earlier in this chapter use a sandboxed solution. To handle this, the web parts are written from scratch. A Visual Web Part would work, but this is a good opportunity to show you how to use Silverlight in composite controls, as explained in Chapter 2, "Introduction to SharePoint Development." These concepts are used in other web parts later in the book as well as in editor parts, where a visual solution is not available. It's also used in a navigation control in Chapter 13, "Creating Silverlight Navigation," and a field control in Chapter 15, "Creating a Silverlight Field Control," where, again, a composite control is the only option.

Beginning with a farm solution, each web part was added as a simple, nonvisual web part. As you recall from Chapter 2, instead of using a design surface containing ASP.NET controls and HTML, child controls are added in code by overriding a method calledCreateChildControls().

To facilitate placing Silverlight on the page, a new SilverlightPlugin web control has been provided in the code download. It contains the same Javascript error handler andtag as the standard Silverlight test page, which you might have noticed in the Custom Silverlight Visual Web Part. This time they're in string constants that contain tokens such as{0}and{1}that hold values for thesource,InitParams, and other properties.CreateChildControls()fills in the tokens and adds both the Javascript andtag to the page, as shown in Listing 5.11.

清单5。11. CreateChildControls() in the SilverlightPlugin Control

私人SILVERLIGHT_EXCEPTION_SCRIPT_常量字符串BLOCK = @" "; private const string SILVERLIGHT_OBJECT_TAG = @"