Monday, April 7, 2008

The Object Model is your friend

One of the most important things to grasp as a SharePoint developer is the SharePoint object model. Once you have access to the object model you can create anything in code that you could in the user interface and even more. To gain access to the object model you need to decide first if you will be developing your code on the server that runs SharePoint or on a different machine. It is possible to develop the code on a separate machine and install the dlls on the server. Note that this requires a little more work and does not allow for debugging.

Once you have decided where you want to develop your code, you can then begin a Visual Studio project. You will need to add a reference to the Microsoft.SharePoint dll. If you are developing on a remote machine you will need to get a copy of the dll from the SharePoint server. The dll is located in C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI. If you are developing on the SharePoint server then the dll is available under the .Net tab for references.

The first step to getting into the SharePoint object model is to grab an SPSite object:

Dim objSite As SPSite = New SPSite("http://testserver")

You create a new SPSite by passing in the URL to the site. Once you have a Site object you need to get access to an SPWeb object:

Dim objWeb As SPWeb = objSite.OpenWeb

If you make the previous call, it will grab the root web of that site. You can also open a subweb:

Dim objWeb As SPWeb = objSite.OpenWeb("/en-us")

In this instance you can give the URL of the web to open.

You can also access the web through the current context:

Dim objWeb As SPWe = SPContext.Current.Web

This will get you the current web.

Once you have access to the SPWeb object you can manipulate Lists, Libraries, Files, Pages, Columns, and Content Types.

Each one of those items has an equivalent SP object: SPList, SPDocumentLibrary, SPFile, Microsoft.SharePoint.Publishing.PublishingWeb, SPField, and SPContentType.

In the next few posts I will explain using list, library, page, field, and item objects and using the Publishing Namespace for Publishing sites.

1 comment:

  1. Hi,

    Its very helpful to begin with object model in sharepoint and I am waiting for ur further postings on object model of sharepoint.

    thank you ...

    Ganesh N R
    +91 9849255958

    INDIA

    ReplyDelete