.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

SharePoint 2010 - SharePoint Object Model

Posted By:Jean Paul       Posted Date: October 24, 2012    Points: 200    Category: SharePoint    URL: http://www.dotnetspark.com  

In this article we will explore the SharePoint Object Model and some basic coding examples with List and Library.The advanced coding examples will be provided at later point of time
 

For programming against the SharePoint items, we need to retrieve the properties and the methods to operate on them. The SharePoint Object Model provides various classes to accomplish this. In this article we can discuss the Object Model with the core classes involved with little amount of coding.
Please note that there are 2 Object Models in SharePoint 2010

1. Server Object Model
2. Client Object Model

Here we are discussing only about the Server Object Model. Server Object Model classes are used for server side programming and will be executed on the SharePoint Server Farm.


Namespace


The Server Object Model classes are residing in the Microsoft.SharePoint assembly. The general classes are available inside Microsoft.SharePoint namespace and the administration classes inside Microsoft.SharePoint.Administration namespace.


Core Classes


Following are the important classes and their corresponding SharePoint items.

Class SharePoint Item
SPFarm Farm
SPServer Server
SPSite Site Collection
SPWeb Web site
SPControl Control
SPList List
SPDocumentLibrary Document Library
SPContentType Content Type
SPUser User
SPException SharePoint Exception


Programming

Now we can start with experimenting some of the above classes. For starting with we can create a SharePoint Console Application. Use the New
Project > SharePoint > Console Application project item.




Accessing the List Items in a List


For proceeding with this example create a list derived from the Contacts template and name it as “My Contacts”. Add some items inside it with First Name and Last Name set.


Use the following code inside the Console application and execute it.


using (SPSite site = new SPSite("http://appes-pc/my/personal/dotnet")) // Site Collection
{ 
    using (SPWeb web = site.OpenWeb()) // Site
    {
        SPList list = web.Lists["My Contacts"]; // List
        int i = 1;
        foreach (SPListItem item in list.Items)
        {
            Console.WriteLine("Item: " + (i++).ToString());

            Console.WriteLine(item.Name);
            Console.WriteLine(item.Title);
            Console.WriteLine(item.GetFormattedValue("First Name"));
            Console.WriteLine(item.GetFormattedValue("Last Name"));

            Console.WriteLine(string.Empty);
        }
    }
}

Console.ReadKey(false);

If you receive any build errors regarding namespace, change the Project Properties > Target to .Net Framework 3.5.




Now try building the application and it should work fine. You can see the following output.




Please note the usage of Title, Name properties and the GetFormattedValue() method.


Accessing the items in a Library


Now we can proceed with accessing the document library items programmatically. For proceeding with you need to create a document library inheriting from Document Library template and name it as “My Docs”. Upload one or two documents into it. We are proceeding to get the file name and length in this example.


Enter the following code in the console application.


using (SPSite site = new SPSite("http://appes-pc/my/personal/dotnet")) // Site Collection
{
    using (SPWeb web = site.OpenWeb())                    // Site
    {
        SPDocumentLibrary library = web.Lists["My Docs"] as SPDocumentLibrary; // Library
        int i = 1;
        foreach (SPListItem item in library.Items)
        {
            Console.WriteLine("Item: " + (i++).ToString());

            Console.WriteLine(item.File.Name);
            Console.WriteLine(item.File.Length);

            Console.WriteLine(string.Empty);
        }
    }
}

Executing the application you can see the following results.




SPControl


The SPControl class acts as the base class while developing server controls. It reseides in the namespace Microsoft.SharePoint.WebControls.

SPControl provides static methods that returns reference to the current site, web, web application, module. The methods are:

SPControl.GetContextSite()
SPControl.GetContextWeb()
SPControl.GetContextWebApplication()
SPControl.GetContextModule()

SPException

The SPException class can be used to handle exception inside a try catch block. It represents the exceptions thrown by the server object model.


try
{
    // Code here
}
catch (SPException ex)
{
    // Handle Exception
}

SPUser

The SPUser class can be used to access user information for a SharePoint site. Enter the following code to get the number of users and their names.

try
{
    // Code here
}
catch (SPException ex)
{
    // Handle Exception
}

On running the code you can see the results based on your machine users.



Note

You can add a new list item or library item using the Items.Add() method. For updation and deletion use the item.Update() and item.Delete() methods respectively. More code coverage on these areas will be provided later.

References

http://msdn.microsoft.com/en-us/library/ms473633.aspx
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.spcontrol.aspx
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spuser.aspx

Summary

In this article we have explored the SharePoint Object Model and some basic coding examples with List and Library. The advanced coding examples will be provided at later point of time.


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend