Exercise 4: Programming with the SharePoint 2010 object model
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 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.
- If you haven't already done so, you need to run the batch file named LabSetup01.bat to create the site located at https://intranet.contoso.com/sites/Lab01.
- Open the browser and navigate to the new site at https://intranet.contoso.com/sites/Lab01/default.aspx. You should see that this new site has been created as a Blank site that contains 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.
- Launch Visual Studio 2010 from Windows Start menu.
Create a new console application, using your language choice of C# or VB.Net, named Lab01_OM. Create this new project at a location of c:\Student\Labs\01_Roadmap. 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. Also, INCREDIBLY IMPORTANT, remove the check from the Create directory for solution checkbox. Failure to do so will result in a namespace of Lab01_OM.Lab01_OM being generated (instead of Lab01_OM) which will cause issues later in this lab.
Figure 1
Create a Console application
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.
Error: Note format was corrupted. Title should be bold. If using C# 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.
Figure 2 C#
The target Framework should be set to .NET Framework 3.5
Error: Note format was corrupted. Title should be bold. If using VB.Net Go to the Compile tab of project properties and click on Advanced Compile Options… near the bottom right corner. 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.
Figure 23 VB.Net
The target Framework should be set to .NET Framework 3.5
In the Solution Configurations drop-down box, located near the center of the toolbar, select Configuration Manager…
Figure 3
The target platform must be set to x64
- On the Configuration Manager dialog box, in the Active solution platform drop-down box select x64.
- If x64 is not available select <New…> and in the New Solution Platform dialog box select x64 as the new platform copying settings from x86 and checking the Create new project platforms check box. Now Click OK. Now repeat this step to add an Any CPU choice as well. Finally, select the x64 option.
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. (Note: to add a reference rt-click on the Lab01_OM project in the Solution Explorer and select Add reference...)
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.
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.SharePoint.dll
Next, add a reference to the main assembly for ASP.NET named System.Web as shown below.
Figure 4
Add Reference dialog
- Now it's time to write some code against the SharePoint object model to enable the Developer Dashboard.
- If Using C#: Modify program.cs to look like this.
using System; using Microsoft.SharePoint; using Microsoft.SharePoint.Administration; namespace Lab01_OM { class Program { static void Main() { SPWebService contentService = SPWebService.ContentService; SPDeveloperDashboardSettings developerDashboard =contentService.DeveloperDashboardSettings; developerDashboard.DisplayLevel = SPDeveloperDashboardLevel.On; developerDashboard.Update(); Console.WriteLine(“Developer Dashboard updated.”); } } }
Error: Note format was corrupted. Title should be bold.contentService variable turns out to be null, you have to check your project properties. Good chance you haven’t selected the x64 platform.
- If Using VB.Net: Modify Module1.vb to look like this.
Imports System Imports Microsoft.SharePoint Imports Microsoft.SharePoint.Administration Module Module1 Sub Main() Dim contentService As SPWebService = SPWebService.ContentService Dim developerDashboard As SPDeveloperDashboardSettings = contentService.DeveloperDashboardSettings developerDashboard.DisplayLevel = SPDeveloperDashboardLevel.On developerDashboard.Update() Console.WriteLine("Developer Dashboard updated.") End Sub End Module
Error: Note format was corrupted. Title should be bold.contentService variable turns out to be null, you have to check your project properties. Good chance you haven’t selected the x64 platform.
Run the application by pressing [CTRL]+[F5]. Once the program runs, return to the Internet Explorer and site at https://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.
Figure 5
The Developer Dashboard
- Go back to Visual Studio and update the code in the C#/VB.Net 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.
- 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/Module1.vb 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 window. 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.
static void Main() { string targetSiteUrl = "https://intranet.contoso.com/sites/Lab01"; using (SPSite siteCollection = new SPSite(targetSiteUrl)) { using (SPWeb site = siteCollection.RootWeb) { Console.WriteLine(site.Title); } } Console.ReadLine(); }
Sub Main() Dim targetSiteUrl As String = "https://intranet.contoso.com/sites/Lab01" Using siteCollection As New SPSite(targetSiteUrl) Using site As SPWeb = siteCollection.RootWeb Console.WriteLine(site.Title) End Using End Using End Sub
- 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:\Student\Labs\01_Roadmap\StarterFiles folder named:C#: Lab01_Utilties.csVB.Net: Lab01_Utilties.vb
- The new source file named Lab01_Utilties.cs/vb 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/vb in order to understand how it is leveraging the SharePoint 2010 object model.
- Now it’s time to modify the Main method in program.cs or Module1.vb to use the three utility classes in Lab01_Utilities.cs/vb. Modify the code within the using construct in Main so that it looks like the code below.
using (SPSite siteCollection = new SPSite(targetSiteUrl)) { SPWeb site = siteCollection.RootWeb; string TasksListTitle = "Tasks"; string TasksListDescription = "Contoso Tasks"; TasksListFactory.Create(site, TasksListTitle, TasksListDescription); string AnnouncementsListTitle = "Announcements"; string AnnouncementsListDescription = "Contoso Announcements"; AnnouncementsListFactory.Create(site, AnnouncementsListTitle, AnnouncementsListDescription); WebPartPageDesigner.ClearAll(site, "default.aspx"); WebPartPageDesigner.AddXsltListViewWebPart(site, "default.aspx", "Tasks", "Left"); WebPartPageDesigner.AddXsltListViewWebPart(site, "default.aspx", "Announcements", "Left"); }
if you have a problem with IntelliSense not finding the TasksListFactory, this could be the result of not removing the check in the Create directory for solution checkbox. To remedy this add a usingLab01_OM.Lab01_OM to the top of the Program.cs class.
Using siteCollection As New SPSite(targetSiteUrl) Dim site As SPWeb = siteCollection.RootWeb Dim TasksListTitle As String = "Tasks" Dim TasksListDescription As String = "Contoso Tasks" TasksListFactory.Create(site, TasksListTitle, TasksListDescription) Dim AnnouncementsListTitle As String = "Announcements" Dim AnnouncementsListDescription As String = "Contoso Announcements" AnnouncementsListFactory.Create(site, AnnouncementsListTitle, AnnouncementsListDescription) WebPartPageDesigner.ClearAll(site, "default.aspx") WebPartPageDesigner.AddXsltListViewWebPart(site, "default.aspx", "Tasks", "Left") WebPartPageDesigner.AddXsltListViewWebPart(site, "default.aspx", "Announcements", "Left") End Using
if you have a problem with IntelliSense not finding the TasksListFactory, this could be the result of not removing the check in the Create directory for solution checkbox. To remedy this add an Imports Lab01_OM.Lab01_OM to the top of the Module1.vb class.
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 lists to the target Blank site and add two Web Parts to the home page at https://intranet.contoso.com/sites/Lab01/default.aspx to display the contents of these two lists as shown in the following figure:
Figure 6
The ListView web parts on the Home page
In this exercise you learned how to enable the Developer Dashboard and how to work with the SharePoint object model to modify an existing SharePoint site.
|
|