Learning SharePoint   Enhance Your SharePoint Knowledge

Subscribe to Learning SharePoint Subscribe
Twitter Learning SharePointFollow Us
in Learning SharePoint
Check Out Our New SharePoint 2013 series!

Sharepoint 2010 Object model Tutorial

07/13/2010  
Category: SharePoint 2010, Tutorials


This is a step-by-step tutorial to learn Sharepoint 2010′s Server and client object model.

Check out : Part 1 Part 2 Part 3

In this post I will discuss various classes and methods that are used to extract data from sharepoint 2010′s list\libraries, sites, site collections, webs, webapplictaions etc using Server object model only. For Client Object model see Client Object Model Tutorial.
As we go along we also will discuss the detailed code snippets for these object models and some notes and tips about the best practices.
Server Object Model –
Here we will look at how to use SharePoint API’s, LINQ, REST and SharePoint web service to extract data from sharepoint server.

Lets Start with using the API’s in Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces.

Firstly, to work with SharePoint 2010 components, your code must first establish the site context or site collection context for requests that are made to the server.

Please Note : In SharePoint, the SPsite object also referred to as Site is actually a “Site Collection” object, not a website
and the SPweb object also refered to as “web” is a single site in the site collection.(It can be a top-level site collection site).
also, object of type SPWebApplication is a highest level object which has reference to the web applictaion that contains the site collection.

To get the reference to site context in your code use the recommended Microsoft.SharePoint.SPContext class and its members.

Lets look at how it is used

To get a reference to the site collection -

SPSite oSiteCollection = SPContext.Current.Site;

To get a reference to the current web site or web in the site collection -

SPWeb oWebSite = SPContext.Current.Web;

or

SPWeb oWebSite = SPControl.GetContextWeb(Context);

Note : if your are using Microsoft.SharePoint.SPContext class, you should not dispose any SPSite or SPWeb object obtained by any of the above methods. The SharePoint Foundation runtime will dispose of them after page completion.

To get a reference to all the webs or sites in a site collection -

SPWeb oWebSite = SPContext.Current.Site.AllWebs["mySite"];

oWebSite.Dispose();

Note : You should explicitly dispose of references to objects that are obtained through the AllWebs() or Openweb() property. You can also use using clause
like below to avoid calling the dispose off method and let sharepoint do this for you.

using can be something like

using (SPWeb oWebSite = SPContext.Current.Site.AllWebs["mySite"]);
{

}

You can also use the Openweb() as below

using (SPWeb oWebSite = mySiteCollection.OpenWeb(“mySite”))
{

}

Lets look at some other components of the SharePoint farm that you can get using SPContext

To get a reference to the current top-level server farm object -

SPFarm myFarm = SPContext.Current.Site.WebApplication.Farm;

To get a reference to the site collection database -

SPSite oSiteCollection = SPContext.Current.Site.CurrentDatabase

Lets look at some of the general code snippets

To return the collection of site collections in a SharePoint Web application -

SPWebApplication webApplication = SPContext.Current.Site.WebApplication;

using (SPSiteCollection siteCollections = webApplication.Sites)
{

foreach (SPSite siteCollection in siteCollections)
{
Label1.Text += siteCollection.Url + “<BR>”;

siteCollection.Close();
}

}

Note : To run the above code reference the Microsoft.SharePoint.Administration.SPWebApplication assembly in your code.

To return the collection of The all the Webs or sites within a site collection, including the top-level site and all subsites.

SPSite oSiteCollection = SPContext.Current.Site;
using(SPWebCollection collWebsite = oSiteCollection.AllWebs);
{

for (int i = 0; i < collWebsite.Count; i++)
{
using (SPWeb oWebsite = collWebsite[i])
{
SPListCollection collList = oWebsite.Lists;

for (int j = 0; j < collList.Count; j++)
{
Label1.Text += SPEncode.HtmlEncode(collWebsite[i].Title) + ” ”
+ SPEncode.HtmlEncode(collList[j].Title) + “<BR>”;
}
}
}
}

To return the all the subsites and lists of the current site

using (SPWeb oWebSite = mySiteCollection.OpenWeb())
{

using(SPWebCollection subSites = oWebsite.Webs)
{

foreach (SPWeb subSite in subSites)
{
Label1.Text += SPEncode.HtmlEncode(subSite.Title) + “<BR>”;

SPListCollection collList = subSite.Lists;

foreach (SPList oList in collList)
{
Label1.Text += SPEncode.HtmlEncode(oList.Title) + ” ” +
oList.ItemCount.ToString() + “<BR>”;

}subSite.Close();
}
}}

Next we will look at how to use Client Object Model >>

Subscribe to Learning SharePoint Subscribe
Twitter Learning SharePointFollow Us
in Learning SharePoint
Advertisement

Random Posts

.Net 4 with SharePoint Foundation 2010
.Net 4 with SharePoint»

Since this has been asked a million times in various forums. Here is the Final word from Michael Foertsch (secretsofsharepoint.com/cs).»

Create filters in Dashboard Designer Sharepoint 2010
Create filters in Dashboard»

In continuation with our series on Performancepoint Dashboards in SharePoint 2010 in this post however we will discuss about using»

Add,Delete,Retrieve list item REST Sharepoint 2010
Add,Delete,Retrieve list item REST»

Here is a quick example below of adding an item to the Announcements list using REST in SharePoint 2010. For»

Recent Forum Posts

Linq to Sharepoint

by RS on May 18, 2012

comments on the code

by spbsmile on May 18, 2012

connection(add) EditControlBlock and my program

by spbsmile on May 18, 2012

Custom master pages used in multiple SharePoint 2010 sites

by ubersteve on May 18, 2012

upload file and click on button but cant upload file into list by using below code

by santhoshreddy on May 18, 2012