In this exercise you will create a new console application that programs against the SharePoint Foundation 2010 object model to create new lists and add items to a SharePoint 2010 site. This will illustrate some on the aspects of the SharePoint Foundation 2010 object model as well as show some of the issues about which versions of assemblies to use when you do this type of programming.
1. If you haven't already done so, you need to run the batch file named LabSetup01.bat to create the site located at http://intranet.contoso.com/sites/Lab01.
2. Open the browser and navigate to the new site at http://intranet.contoso.com/sites/Lab01/default.aspx. You should see that this new site has been created as a blank site with no existing lists. In the following steps you are going to write a program that uses the SharePoint 2010 object model to create a few new lists in this site and to populate these lists with sample data.
3. Launch Visual Studio 2010 from Windows Start menu.
4. Create a new console application named Lab01_OM. Create this new project at a location of C:\SharePoint2010Developer\Labs\SharePoint2010DeveloperRoadmap. Make sure to create the new project based on .NET Framework 3.5 instead of the default which will be .NET Framework 4.0. This is controlled by one of the drop downs found above the list of project templates as shown in the following figure.
Create a Console application
5. Once the new project has been created, right-click on the Lab01_OM project in the Solution Explorer and click on Properties to see the Project Properties view. Go to the Application tab of project properties and verify that .NET Framework Version 3.5 is select as the Target Framework. It is important for you to acknowledge that SharePoint 2010 is based on .NET Framework version 3.5 and not version 4.0.
The target Framework should be set to .NET Framework 3.5
6. Click on the Build tab and verify that the target Platform is x64. If not, make sure to change the platform to x64 or Any CPU.
The target platform must be set to x64
7. Now add a reference to the assembly Microsoft.SharePoint.dll which contains the core classes of the SharePoint 2010 Foundation object model. The dll should be displayed in the .NET tab.
However, if the assembly cannot be found in the .NET tab, you might have to select the Browse tab and navigate to the following path to add the reference.
8. Next, add a reference to the main assembly for ASP.NET named System.Web as shown below.
Add Reference dialog
9. Now it's time to write some code against the SharePoint object model to enable the Developer Dashboard. Modify program.cs to look like this.
10. Run the application by pressing [CTRL]+[F5]. Once the program runs, return to the Internet Explorer and site at http://intranet.contoso.com/sites/Lab01/default.aspx. Refresh the page and you should see the SharePoint 2010 Developer Dashboard at the bottom of the page.
The Developer Dashboard
11. Go back to Visual Studio and update the code in the static Main method to assign a value of SPDeveloperDashboardLevel.Off instead of SPDeveloperDashboardLevel.On and run the program again. Go back to the Internet Explorer and refresh the page again. You should see that the Developer Dashboard is now gone. Now you know how to make the Developer Dashboard appear and disappear just by running code against the SharePoint 2010 object model.
12. Now, it time to write code that programs against the top-level site you created at the beginning of this exercise. Modify the Main method in program.cs to create a new SPSite object to program against the new site collection. Make sure to structure your code inside a using construct so that your code makes an implicit call to the Dispose method to prevent memory leakage. As a first step, obtain a reference to the SPWeb object for the top-level site and print the Title property of the site to the console windows. Your code should look like the code below. When you are done writing this code, run your program to make sure it runs without errors and displays the proper site title.
13. In this next step you will add an existing source file to your project that contains utility classes which program against the SharePoint 2010 object model and you will use these classes to create lists and add Web Parts. Right-click on the project in the Solution Explorer and select the Add Existing Item command. Add the existing source file inside the C:\SharePoint2010Developer\Labs\SharePoint2010DeveloperRoadmap\Source\StarterFiles folder named Lab01_Utilties.cs.
14. The new source file named Lab01_Utilties.cs contains three utility classes named TasksListFactory, AnnouncementsListFactory and WebPartPageDesigner. The first two classes contain code to create new list instances and to populate them with sample data. The third class named WebPartPageDesigner programs against a class named SPLimitedWebPartManager which is provided by the SharePoint 2010 object model to delete and add Web Part instance from a target Web Part Page. Take a moment to examine the three classes inside Lab01_Utilties.cs in order to understand how it is leveraging the SharePoint 2010 object model.
15. Now it’s time to modify the Main method in program.cs to use the three utility classes in Lab01_Utilities.cs. Modify the code within the using construct in Main so that it looks like the code below.
16. Once you have modified the Main method with the code shown above, run the program by pressing [CTRL]+[F5]. When the program runs, it should add two new lists to the target Blank site and add two new Web Parts to the home page at http://intranet.contoso.com/sites/Lab01/default.aspx to display the contents of these two lists as shown in the following figure:
The ListView web parts on the Home page