How to correctly access to WSS Ojbect Model in SharePoint 2007

Thursday, 19 March 2009 21:56 by myro

We cannot start this guide, before you don't understand how to access correctly to mainly objects in Windows SharePoint Services programming.Wrong access to those objects can make worse SharePoint's perfomace. One importat aspect is that you need always to dispose your objects after you are done using them. In a related article, Microsoft writes:

In the SharePoint object model, the Microsoft.SharePoint.SPSite and Microsoft.SharePoint.SPWeb objects are created in managed code as a small wrapper (approximately 2 KB in size). This wrapper then creates unmanaged objects, which can average approximately 1–2 MB in size. If your code resembles the following code example, and if you assume that the SPWeb.Webs collection has 10 subsites, a total of 10 items are created, each with an average of 2 MB of memory (for a total of 20 MB).

When you develop in SharePoint 2007, your applications runs always in the SPContext and you can access directly almost in any object:

LabelSiteName.Text = SPContext.Current.Site.RootWeb.Name;

As you can see, I have started from the SPContext to display the main SPWeb name into a label. This piece of code is correct because you are just getting a property value from the SPWeb, but what happens if you need to make some instensive operations to this object? you should use this:

using (SPSite site = new SPSite("http://localhost/"))

            {

                using (SPWeb web = site.OpenWeb())

                {

                    //your code goes here...

                }

            }

Or if you need to access from the current context:

using (SPSite site = new SPSite(SPContext.Current.Site.ID))

            {

                using (SPWeb web = site.OpenWeb())

                {

                    //your code goes here...

                }

            }

If you want to access to a SPList, never access through the name that SharePoint i2007 displays n your browser. SharePoint uses 2 names to identify the list: the internal name and the display name. Once you create a list, the internal name is based on the display name, but will never change untill you delete the list. Display names can be easly modified by the UI, and that's why you should never use them. The internal name is also used to determine the the list's ur, infact you can access to this obect in this way:

string ListUrl = "/Lists/List";

SPList list = web.GetList(web.ServerRelativeUrl + ListUrl);

Or if you need to access to a document library:

string DocLibUrl = "/DocLib";

SPDocumentLibrary doclib = (SPDocumentLibrary)web.GetList(web.ServerRelativeUrl + DocLibUrl);

more valid snippets:

SPList list = web.Lists["http://localhost/Lists/List/AllItems.aspx"];

SPList list = web.Lists[new Guid("60f58910-28c7-11dd-bd0b-0800200c9a66")];

Let's say you want to access to SPList and get the value of the SPField called "LastName" for the first item(SPListItem). Always  Remeber to get the SPField value from the internal name for the fields too:

 SPField fld = list.Fields.GetFieldByInternalName("LastName");

 SPListItem item = list.Items[0];

 string lastName = item[fld.Id] as string;

or if you need a standard Sharepoint fields (for example 'Title')

string title = item[SPBuiltInFieldId.Title] as string;

You have learned how to correctly access to SPSite, SPWeb, SPList and SPField.... what's next? oh... com'on... it's SPListItem show time!!!
Let's see how to get the first SPListItem and perform and update from my SPList list

SPListItem updateFirstItem = list.Items[0];

newItem["LastName"] = "my New LastName";

updateFirstItem.Update();

But what happens if you need to create a new istance of SPListItem? You cannot create a new SPListItem as a new object but you need to create it from the SPList.Items.Add() method:

SPListItem newItem = list.Items.Add();

newItem[SPBuiltInFieldId.Title] = "my New Title";

newItem["LastName"] = "my New LastName";

newItem.Update();

You are done! Now you know how to correctly access to the mainly used SharePoint objects;

I want to be grateful to Sgart.it because most of this tips have been teach and taken from his website. thx!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   , ,
Categories:   SharePoint 2007
Actions:   Bookmark and Share | Permalink | Comments (0) | Comment RSSRSS comment feed
If you consider this post usefull for your purposes, please consider visiting my sponsors to help me out with the myrocode.com maintenance. Thank you.