Programming with the SharePoint 2010 Object Model
In this exercise you will create a new console application that uses the SharePoint Foundation2010 object model to create new lists and add items to a SharePoint 2010 site.
Task 1 – Configure a C# Console Project to use the SharePoint Object Model
In this task you will configure a Visual Studio 2010 C# project to use the SharePoint object model.
1. Open Internet Explorer and navigate to the site http://intranet.contoso.com/sites/lab01/.
This blank site was created in the Lab Setup. This is a blank site with no lists.
This blank site was created in the Lab Setup. This is a blank site with no lists.
2. Click Start | All Programs | Microsoft Visual Studio 2010| Microsoft Visual Studio 2010.
Figure 1 - Visual Studio Start Page
4. Click the Target Framework dropdown at the top of the page and select .NET Framework 3.5.
This step enables you to add references to the SharePoint object library. SharePoint 2010 is based on .Net 3.5 and not version 4.0.
This step enables you to add references to the SharePoint object library. SharePoint 2010 is based on .Net 3.5 and not version 4.0.
5. In the Installed Templates section, in the Visual C# group, click Windows, then click Console Application.
6. In the Name textbox, type Lab01_OM
Figure 2 - Template Selection
8. In the Solution Explorer window, right-click Lab01_OM, and then click Properties.
9. Click the Build tab.
Figure 3 - Setting Platform Target x64
11. In the Solution Explorer window, right-click References, and then click Add Reference.
12. Click the Browse tab, and then browse to the following location:
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI.
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI.
Figure4 - Setting a Reference to Microsoft.SharePoint.dll
14. In the Solution Explorer window, right-click References, and then click Add Reference.
15. Click the .NET tab.
16. Click System.Web and then click OK.
Task 2 – Enabling the Developer Dashboard in a Console Application
In this task you will modify the code in the Console application to enable the Developer Dashboard on a SharePoint Site. You will then alter the code and disable the dashboard.
1. In the Solution Explorer window, double-click Program.cs.
2. Paste the following code to replace the code in Program.cs :
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.");
}
}
}
Note: if your contentService variable turns out to be null, you have to check your project properties. Good chance you haven’t selected the x64 platform.
3. Press [CTRL] + [F5] to run the console application.
4. Press a key to close the console application.
5. Return to the Internet Explorer window that you left open in Task 1 at the following site:
http://intranet.contoso.com/sites/lab01/default.aspx
http://intranet.contoso.com/sites/lab01/default.aspx
6. Press [F5] to refresh Internet Explorer.
Scroll to the bottom of the page, and note that the developer dashboard is enabled.
Scroll to the bottom of the page, and note that the developer dashboard is enabled.

Figure 5 - Developer Dashboard Page Footer
1. In Visual Studio, update the single line of code so that the SPPerformanceMonitoringLevel value is set to off. The code should look as follows:
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.Off;
developerDashboard.Update();
Console.WriteLine("Developer Dashboard updated.");
}
}
}
7. Press [CTRL] + [F5] to run the console application.
8. Press any key to close the console application.
9. Return to Internet Explorer.
10. Press [F5] to refresh Internet Explorer.
Scroll to the bottom of the page, and note that the developer dashboard is now disabled.
Scroll to the bottom of the page, and note that the developer dashboard is now disabled.
Task 3 – Using the SPSite object in a Console application
In this task you will write code that programs against the top-level site you created at the beginning of this exercise. In this task you will:
· Modify the Main method in program.cs to create a new SPSite object to program against the new site collection.
· Structure your code inside a using construct so that your code makes an implicit call to the Dispose method to prevent leakage.
· Obtain a reference to the SPWeb object for the top-level site and print the Title property of the site to the console window.
1. Return to Visual Studio.
2. In the Solution Explorer window, double-click Program.cs.
3. Paste the following code to replace the code in Program.cs :
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace Lab01_OM
{
class Program {
static void Main()
{
string targetSiteUrl = "http://intranet.contoso.com/sites/Lab01A";
using (SPSite siteCollection = new SPSite(targetSiteUrl))
{
SPWeb site = siteCollection.RootWeb;
Console.WriteLine(site.Title);
Console.ReadLine();
}
}
}
}
4. Press [CTRL] +[F5] to run the console application.
Figure 6 - SharePoint Site Name in Console Application
6. When the site title is displayed, press [ENTER] to close the application.
Task 4 – Creating Lists and Web Parts from Console application
In this task you will:
· Add existing source files containing utility classes that program against the SharePoint 2010 object model.
· Modify the Main method in the console application to programmatically create a Task, Announcement and Web Part.
1. Return to Visual Studio.
2. In the Solution Explorer window, right-click the projectLab01_OM, and then click Add Existing Item

Figure 7 - Project Right-Click Options
3. Browse to the following location:
C:\Student\Labs\01_Roadmap\StarterFiles.
C:\Student\Labs\01_Roadmap\StarterFiles.
4. Click Lab01_Utilties.cs and then click Add.
The Lab01_Utilties.cs source file 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.
The Lab01_Utilties.cs source file 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.
5. Paste the following code to replace the code in Program.cs :
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace Lab01_OM
{
class Program {
static void Main()
{
string targetSiteUrl = "http://intranet.contoso.com/sites/Lab01A";
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, "home.aspx");
WebPartPageDesigner.AddXsltListViewWebPart(site, "home.aspx",
"Tasks", "Left");
WebPartPageDesigner.AddXsltListViewWebPart(site, "home.aspx",
"Announcements", "Left");
}
}
}
}
6. Press [F5] to run the console application.
The Console application adds two new lists to the target site and two new web parts to the home page to display the contents of these two lists.
The Console application adds two new lists to the target site and two new web parts to the home page to display the contents of these two lists.
7. Return to Internet Explorer and refresh the page http://intranet.contoso.com/sites/Lab01A
Review the modifications made by the console application.
Review the modifications made by the console application.

Figure 8- Site Modifications





Nice post.Explanation is very clear and good.Thanks uday
ReplyDeleteUday... please keep post new things on this blog
ReplyDeletesure , vijay ...
ReplyDeletenow a days i m little bit busy , so i don't have time to post , i will post soon ..