Professional Documents
Culture Documents
SharePoint 2010 Developer Guide 1
SharePoint 2010 Developer Guide 1
This document is provided as-is. Information and views expressed in this document, including URL and
other Internet Web site references, may change without notice. You bear the risk of using it.
Some examples depicted herein are provided for illustration only and are fictitious. No real association or
connection is intended or should be inferred.
This document does not provide you with any legal rights to any intellectual property in any Microsoft
product. You may copy and use this document for your internal, reference purposes.
2010 Microsoft. All rights reserved.
Microsoft, SharePoint, Visual Studio, InfoPath, and Visio are trademarks of the Microsoft group of
companies. All other trademarks are property of their respective owners.
Page 1
Contents
Exercise 1 Web Part and Linq Walkthrough ....................................................................................................... 4
Task 1 - Create a new SharePoint Project ........................................................................................................ 4
Task 2 - Generate LINQ-to-SharePoint proxy class to access list data ............................................................ 7
Task 3 - Access the SharePoint list data in Visual Web Part ............................................................................ 9
Task 4 Build and Deploy the Visual Web Part .............................................................................................. 11
Exercise 2 Build a BCS External Content Type ................................................................................................ 16
Task 1 Create a Business Data Catalog Model project ................................................................................ 16
Task 2 Update the Schema of the Entity Class ............................................................................................ 18
Task 3 Update the Service Code .................................................................................................................. 28
Task 4 Build and Deploy ............................................................................................................................... 30
Exercise 3 Create a Silverlight Application for the SharePoint Client Object Model ........................................ 34
Task 1 Create a Silverlight Application Project ............................................................................................. 34
Task 2 Write code to access and render SharePoint List Data .................................................................... 36
Task 3 Deploy and Test using the SharePoint Silverlight web part .............................................................. 40
Exercise 4 Creating a graph using the SharePoint Object Model and Silverlight Graphing controls ............... 42
Task 1 Create a Silverlight Application Project ............................................................................................. 42
Task 2 Write code to access Employee SharePoint List Data and display it in a Silverlight Graph Control 46
Task 3 Deploy and Test using the SharePoint Silverlight Charting web part ............................................... 49
Exercise 5 Create a Web Part for a Sandboxed Solution ................................................................................. 53
Task 1 Create a Sandboxed Solution Project with a web part ..................................................................... 53
Task 2 Add code to provide querying and rendering functionality ................................................................ 55
Task 3 Build and Deploy the Sandboxed Solution ....................................................................................... 58
Exercise 6 Creating an Activity for use in a SharePoint Designer Workflow .................................................... 63
Task 1: Create and Prepare Project ................................................................................................................ 63
Task 2: Create a new Workflow Activity for use by the Reusable Workflow ................................................... 64
Task 3: Add code to create a document library based on parameters passed to our activity. ........................ 66
Task 4: Configure activity for deployment. ....................................................................................................... 69
Task 5: Add the SPDActivityDemo activity to be deployed with SPCHOL305Ex1. ......................................... 73
Task 6: Configure a Feature using Feature Designer ..................................................................................... 74
Task 7: Add and code a Feature Receiver ...................................................................................................... 75
Task 8: Create a re-usable workflow using SharePoint Designer ................................................................... 76
Task 9: Import the reusable workflow into Visual Studio ................................................................................. 84
Task 10: Associate the workflow with a list in SharePoint ............................................................................... 91
Walkthrough Summary ......................................................................................................................................... 93
Page 2
The following six walkthroughs are C# walkthroughs of select scenarios that professional developers
would do in SharePoint 2010 with Visual Studio 2010.
You should find there are sufficient screenshots in these walkthroughs to read the document without
having hands on access to the software. These developer walkthroughs are also available as hands on
labs as part of this online eLearning course: http://MSSharePointDeveloper.com. There are additional
labs in the course, there are also videos, code samples and online virtual lab environments, and the
hands on labs are available in both C# and VB.NET.
You are welcome to discuss these walkthroughs at http://mssharepointforums.com
To provide feedback on this guide, send an e-mail message to sp2010fb@Microsoft.com.
Page 3
10. Visual Studio will create the new SPCHOL200-Ex1 project and add the necessary files.
Page 5
11. Notice that Visual Studio also creates a Visual Web Part named VisualWebPart1. Within the
Solution Explorer, expand VisualWebPart1 and open VisualWebPart1.webpart.
12. Change the value of the property element with the name attribute value of Title to
SPLinqDemoTitle and the value of the property element with the name attribute value of
Description to SPLinqDemoPart Description. This will change the Title and Description property
of the Visual Web Part once it is deployed. Save the file.
<properties>
<property name="Title" type="string">SPLinqDemoTitle</property>
<property name="Description" type="string">SPLinqDemoPart Description</property>
</properties>
Page 6
3. Type the following command in the command prompt and press Enter to set the path to the
SharePoint 2010 folder:
set path=%path%;c:\program files\common files\microsoft shared\web server
extensions\14\bin
4. Type the following command in the command prompt and press Enter to generate the Linq-toSharePoint proxy code.
spmetal.exe /web:http://intranet.contoso.com /namespace:SPCHOL200_Ex1.VisualWebPart1
/code:SPLinq.cs
Note you may get warnings about content types for list Form Templates. You can safely
ignore this warning and continue
5. Close the command window and switch back to Visual Studio.
6. In Visual Studio, right click on SPCHOL200-Ex1 project and select Add | Existing Item.
Page 7
7. Select SPLinq.cs from the Add Existing Item dialog window and click Add:
8. In the Solution Explorer, right click on References and select Add Reference.
9. Switch to Browse tab and enter C:\Program Files\Common Files\Microsoft Shared\Web
Server Extensions\14\ISAPI in the File Name text box. Press Enter to change directories. Your
Add Reference window should now look like Fig. 11.
10. Select Microsoft.SharePoint.Linq.dll.
Page 8
Page 9
Page 10
Page 11
5. Switch to Insert tab in the Ribbon and click on Web Part to insert a Web Part to the page.
Page 12
Put your cursor in the area of the page where you want the Web Part to appear. This must be a
zone that accepts Web Parts. In this case, put your cursor in the zone below the Shared
Page 13
Documents control.
8. Click Add to add the web part to the page. This will add the SPLinqDemoTitle web part to the
selected layout zone.
Page 14
Page 15
Page 16
8. Click Finish.
Page 17
2. If its not already open, double-click on Customer.cs in the Solution Explorer. Replace the code
in the body of the Customer class with the following code. This is adding two extra properties
and changing the type of a third as a mapping for the type descriptors we added and modified in
the BDC definition.
Note when you change the Identifier1 to CustomerId, you can use the glyph that appears in the
editor to rename all references to the field Identifier1. This doesnt help you in this case but is a
good practice.
public
public
public
public
Page 18
Task 3 Extend the BCS Metadata for the External Content Type
1. In the Solution Explorer, open the BdcModel1.bdcm if the BDC designer isnt already open.
3. Rename the Entity1 to Customer by clicking on the Entity1 name in the designer and pressing
F2 (alternatively you can change the name in the Property Grid).
4. Highlight Identifier1 in the Entity designer and press F4 to open the Properties Grid. Use the
Properties Grid to rename Identifier1 to CustomerId and set its Type Name to System.Int32.
Page 19
5. In the BDC Explorer in Visual Studio, expand Model | BDCModel1 | BDCModel1 | Customer |
ReadList | returnParameter. Click Entity1List to open its Property Grid.
6.
Page 20
9. In the BDC Explorer right-click Customer and select Add Type Descriptor.
10. Select TypeDescriptor and, using the Properties grid, change Name to FirstName.
11. In the BDC Explorer right-click Customer and select Add Type Descriptor.
12. Select TypeDescriptor and, using the Properties grid, change Name to LastName.
Figure 27 - Customer entity in BDC Explorer after the addition of FirstName and LastName type descriptors
14. In the BDC Explorer expand the ReadItem method, right-click returnParameter and select
Paste.
Page 22
16. Expand returnParameter | Customer and confirm FirstName and LastName type descriptors
have successfully copied.
Page 23
Page 24
17. In the BDC Explorer expand the ReadItem | id and select Identifier1.
18. Using the Properties grid, rename Identifier1 to CustomerId, set the TypeName to
System.Int32.
19. Using the BDC Method Details pane add an update method
Page 25
Page 26
21. Open the new Update method in the BDC Explorer window
22. Copy the type descriptor from the CustomerId type description from @id parameter in the
ReadItem method to the @id parameter in the Update method
23. Change the Pre-Updater property on the id parameter to be True.
Page 27
Page 28
3. In the Solution Explorer, expand the Features folder and double click the Feature1.feature node.
This opens the feature designer.
4. Click the Manifest tab at the bottom of the feature designer.
5. Click the + glyph next to Edit Options to open the template editor.
6. Add a new Property for the Site URL
<Property Key="SiteUrl" Value="http://intranet.contoso.com" />
Page 29
4. Click on Create.
Page 30
Page 31
Page 32
8. Click Create.
9. You have just created an external content type and a list to host that content type.
Page 33
7. Press OK to continue.
8. From the menu, select File | Add | New Project.
Page 34
9. In the New Project dialog box, expand the Installed Templates left hand menu to Visual C# |
Silverlight, and choose the Silverlight Application project type in the project type list in the middle
section of the screen.
10. Select .NET Framework 3.5
11. Name the project SPSilverlightExample.
12. Leave the location unchanged.
15. Within the SPSilverlightExample project we will now add the reference assemblies to the
SharePoint Silverlight Client Object Model. Right-click References in the SPSilverlightExample
project and select Add References.
16. Browse to the C:\Program Files\Common Files\Microsoft Shared\Web Server
Extensions\14\TEMPLATE\LAYOUTS\ClientBin folder.
17. Select Microsoft.SharePoint.ClientSilverlight.dll and
Microsoft.SharePoint.Client.Silverlight.Runtime.dll (hold CTRL to select multiple files)
18. Press OK to add the select dll references.
Page 36
3. Expand the DataGrid to take up the entire page by going to the properties grid and setting its
Width and Height property to Auto, its HoriztonalAlignment and VerticalAlignment properties to
Stretch, and its Margin to 0..
Page 37
4. Also make sure to check AutoGenerateColumns (or set its value to true in the XAML).
5. Open App.xaml.cs and add the following using statements to the top of the file:
using Microsoft.SharePoint.Client;
using System.Threading;
Page 38
Page 39
Task 3 Deploy and Test using the SharePoint Silverlight web part
To deploy the solution to SharePoint the resulting .xap file created by the Silverlight project needs to be
in the C:\Program Files\Common Files\Microsoft Shared\Web Server
Extensions\14\TEMPLATE\LAYOUTS\ClientBin folder.
1. Right-click the SPSilverlightExample project, select properties and select the Build tab.
2. Change the output path to C:\Program Files\Common Files\Microsoft Shared\Web Server
Extensions\14\TEMPLATE\LAYOUTS\ClientBin.
3. Build the solution. The .xap file has been copied to the required SharePoint directory and you are
ready to add the Silverlight web part to the SharePoint website.
4. Open Internet Explorer and browse to http://intranet.contoso.com.
5. Select the Edit icon at the top of the page.
6. Select the Insert tab and the click Web Part.
7. From the Category list select Media and Content, choose the Silverlight Web Part from the
web part list, and click Add.
8. In the Silverlight Web Part dialog that pops up enter
/_layouts/ClientBin/SPSilverlightExample.xap as the URL.
Page 40
In this exercise you have created a basic Silverlight application that displays a SharePoint list inside a
datagrid. The data for the list is retrieved from the server so it is available in Silverlight using the
SharePoint Client Object Model.
Page 41
6. Press OK to continue.
7. From the menu, select File | Add | New Project.
Page 42
8. In the New Project dialog box, expand the Installed Templates left hand menu to Visual C# |
Silverlight, and choose the Silverlight Application project type in the project type list in the middle
section of the screen.
9. Name the project SilverlightEmployeeContributionsGraph.
10. Leave the location unchanged.
Page 43
Page 44
12. Add a reference to the Silverlight Charting Controls assembly. It is available on the .NET tab
and is called the System.Windows.Controls.DataVisualization.Toolkit.
Page 45
Task 2 Write code to access Employee SharePoint List Data and display it in a Silverlight
Graph Control
1. In the Solution Explorer right-click on the App.xaml file and select View Code. In the open
App.xaml.cs and add the following using statements to the top of the file:
using Microsoft.SharePoint.Client;
using System.Threading;
4. Add the following Silverlight Charting control inside the Grid element:
<chartingToolkit:Chart x:Name="chart" Width="350" Height="250" Title="Team
Contributions">
<chartingToolkit:Chart.Series>
<chartingToolkit:ColumnSeries ItemsSource="{Binding}"
DependentValuePath="Contributions"
IndependentValuePath="Name"
AnimationSequence="FirstToLast"
Title="Contributions" IsSelectionEnabled="True" />
</chartingToolkit:Chart.Series>
</chartingToolkit:Chart>
Page 46
5. Open MainPage.xaml.cs and add the following using statement to the top of the file:
using Microsoft.SharePoint.Client;
Page 47
10. Like the previous exercise the SharePoint Silverlight client object model is used to retrieve data
from a SharePoint list. Once the employee contribution items have been populated into a list,
LINQ is then used to group employees into teams and their contributions summed together.
Team contributions are then set as the charts data source.
Task 3 Deploy and Test using the SharePoint Silverlight Charting web part
To deploy the solution to SharePoint the resulting .xap file created by the Silverlight project needs to be
in the C:\Program Files\Common Files\Microsoft Shared\Web Server
Extensions\14\TEMPLATE\LAYOUTS\ClientBin folder.
1. Right-click the SilverlightEmployeeContributionsGraph project, select properties and select
the Build tab.
2. Change the output path to C:\Program Files\Common Files\Microsoft Shared\Web Server
Extensions\14\TEMPLATE\LAYOUTS\ClientBin.
3. Build the solution. The .xap file has been copied to the required SharePoint directory and you are
ready to add the Silverlight web part to the SharePoint website.
4. Open Internet Explorer and browse to http://intranet.contoso.com.
Page 49
5. We will update the Silverlight web part added in the previous exercise to point toward the new
Silverlight Charting control we have just made. Click the dropdown icon in the top right hand
corner of the Silverlight web part and select Edit Web Part.
6. Click the Configure button (you may have to scroll the window to the right to see the Configure
button) and then enter /_layouts/ClientBin/SilverlightEmployeeContributionsGraph.xap in
the Silverlight Web Part dialog.
Page 50
7. Click OK.
8. Click OK at the bottom of the Silverlight Web Part sidebar.
Page 51
Page 52
Page 53
6. Press OK to continue.
7. In the SharePoint Customization Dialog, change the local site to use for debugging to
http://intranet.contoso.com/.
8. Leave the trust level for the SharePoint solution as Deploy as a sandboxed solution.
9. Press Finish to continue. Visual Studio will create the new project and add the necessary files.
10. Right-click on the SBSolutionDemo project in the Solution Explorer and select Add | New item.
11. Ensure Visual C# | SharePoint | 2010 | is selected in the Installed Templates pane.
12. In the Add New Item dialog, select to add a new Web Part and name it SBWebPart.
Page 54
Page 55
Page 56
Page 57
Page 58
In the Upload Document dialog that pops up, browse to the .wsp file at
C:\SPHOLs\SPCHOL307\CS\Ex1\SBSolutionDemo\SBSolutionDemo\bin\Debug\SBSolution
Demo.wsp.
8. Click OK to upload the SBSolutionDemo.wsp to SharePoint. Leave the Overwrite existing files
box checked.
9. Click Activate in the Solution Gallery - Activate Solution dialog. The Sandboxed Solution web
part is now ready to be used.
10. Open the Site Actions menu and select More Options.
11. Under the Page section of the Create dialog, select Web Part Page. Click Create.
12. Name the new Web Part Page SBSolutionDemoPage, set the Layout to Full Page, Vertical.
And the Save Location to Shared Documents.
Page 59
Figure 67 - SBWebPart
Page 60
18. Select System Account in the toolbar at the top right hand side of the page and choose Sign in
as Different User.
19. For the User name enter andyj and for the password enter pass@word1.
Page 61
Page 62
5. Click OK.
6. In the SharePoint Customization Wizard, change the URL to http://intranet.contoso.com and
select Deploy as a farm solution. Then click Finish.
Page 63
7. Visual Studio will create the new SPCHOL305Ex1 project and add the necessary files.
Task 2: Create a new Workflow Activity for use by the Reusable Workflow
1. In Visual Studio 2010, add a new project by going to File | Add | New Project.
2. Change the Framework to .NET Framework 3.5
Page 64
5. In Solution Explorer, right-click the SPDActivityDemo project and select Add Reference.
6. Switch to the Browse tab. Enter C:\Program Files\Common Files\Microsoft Shared\Web Server
Extensions\14\ISAPI in the File name textbox and hit Enter.
7. Select Microsoft.SharePoint.dll and Microsoft.SharePoint.WorkflowActions.dll, and click OK.
Page 65
Task 3: Add code to create a document library based on parameters passed to our activity.
1. Right-click on the CreateDocumentLibrary.cs file in the Solution Explorer and select View
Code.
2. Change the CreateDocumentLibrary base class from SequenceActivity to Activity, as seen
below.
public partial class CreateDocumentLibrary : Activity
3. Add the following using statements at the top of the file under the existing using statements.:
Page 66
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowActions;
Page 67
Page 68
2. Right-click on the SPDActivityDemo project in the Solution Explorer and select Properties.
3. Click on the Signing tab, check Sign the Assembly, and then select SPDActivityDemo.snk.
Figure 76 - Signing
Page 69
5. Right-click on the SPCHOL305Ex1 project and click Add, SharePoint Mapped Folder
6. Select Template\1033\Workflow in the Add SharePoint Mapped Folder dialog and select OK.
Page 70
Page 71
Page 72
Page 73
4. Click the Add button and select Add Assembly from Project Output.
5. In the Add Existing Assembly from Project Output dialog click the Source Project combo
and select SPDActivityDemo\bin.
6. Click the button under Safe Controls. Within Safe Controls add the following:
Assembly Name:
SPDActivityDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken= a66e91d2ee2fa8f8
Name Space: SPDActivityDemo
Safe: Checked
Type Name: *
7. When you have finished filling out the safe control details, press Enter to confirm them.
8. Click OK
Task 6: Configure a Feature using Feature Designer
1. Right-click on the Features folder in the Solution Explorer under the SPCHOL305Ex1 project
and select Add Feature.
Page 74
Page 75
4. Build and Deploy the SPCHOL305Ex1Feature project by Right-clicking on the project name
and selecting Deploy.
Page 76
Page 77
Page 78
5. Click the Reusable Workflow button and when prompted name the workflow SPDWorkflow
and then click the OK button to create the reusable workflow.
Page 79
6. Click the Actions button in the ribbon and scroll down to Labs and click Create a Document
Library. Note: This is the activity we made in Visual Studio previously.
Page 80
7. Click into Step 1. In the box next to Document Library Name click the Fx button.
8. Select Current Item as the Data Source, and Title as the value of Field from source. Then
Press OK.
Page 81
11. Once saved click the Publish button in the ribbon. Wait for the workflow to publish.
Page 82
15. Click Site Actions -> View All Site Content -> Site Assets Library.
16. Click on the SPDWorkflow dropdown menu and click Send To -> Download a Copy.
Page 83
17. Save the WSP to a location that you can access in a subsequent step (like the Desktop)
Page 84
4. Click Ok.
Page 85
Page 86
6. When prompted for the .WSP file to import, browse to the SPDWorkflow.wsp file you saved
previously. Click Next.
7. Click Finish.
Page 87
Page 88
8. Double-click on Package in the SPCHOL305Ex1 project and add the Converted workflows
feature to the Package.
Page 89
14. Save the project, build the solution and deploy the SPCHOL305Ex1 project.
Page 90
Browse to http://intranet.contoso.com.
Click on Site Actions | More Options
Select Custom List from the items list.
Name the list Customers and press Create.
Open List Settings
Page 91
8. Click OK
9. Navigate to the Customers list and select Items | New Item.
Page 92
10. Enter Northwind as name of the new customer and press Save when the Customers New
Item dialog appears.
11. Wait while a new document library of the same name should be created automatically.
Walkthrough Summary
In this walkthrough document you reviewed walking through six developer scenarios for SharePoint
2010. You can get the hands on labs that these walkthroughs were based on from
http://MSSharePointDeveloper.com
Page 93