Search This Blog

Saturday, October 30, 2010

MOSS Web Services – Accessing Sharepoint List data


harepoint 2007 provides rich set of API’s that allows developers to develop custom components in Sharepoint to meet custom requirements.
SharePoint API’s can be classified into two types.
  1. Sharepoint Object Model
  2. Sharepoint Web Services
Sharepoint Object Model enables developers to customize almost anything that is in Sharepoint right from creating a Site Collection to connecting to external data sources. This object model is enclosed in 10 dll’s with 30 namespaces. A series of articles on consuming Sharepoint object model can be found here and here.
In on of my previous posts, I wrote down the steps to access SharePoint list data using SharePoint object model and CAML queries.
Now, lets see how to consume the Sharepoint Web Services in order to retrieve the Sharepoint List Data.
Step By Step
  1. Create a new windows form project.
  2. Add Web Reference to Sharepoint Lists Web Service
    1. Right-click ‘Service Reference’ and select ‘Add Service Reference’.
    2. In the Add Service Reference dialog box, click Advanced button.
    3. In the ‘Service Reference settings’ dialog box, click Add Web Reference.
    4. In the Url dropdown box, enter the url of your sharepoint site’s web service. If you are developing in the same system as the system in which MOSS is installed, then you can find the web services by clicking the link ‘Web Services in this Solution’ available in the Add Web Reference dialog box.
      1. Note : The _vti_bin directory present under the sharepoint website directory lists down all the web services available.
    5. Scroll through the list of Web Services. Click in the ‘Lists’ web service. Check the url in the url column and ensure that this Lists web service corresponds to your sharepoint site.
    6. Lists Web Service
    7. In the Web Reference name text box, give a name to the Lists web reference. Lets name it ListsWS.
    8. Click Add Reference.
    9. AddWebReference
  3. Place a button in the form. Name it btnGetListItems and the Caption can be Get List Items. Double click the button and generate the event handler.
  4. The Lists web service contains an class called Lists. Lets create an object for this Lists class.
    1. ListsWS.Lists list = new ListsWS.Lists();
  5. The List object contains a method called GetListItems(). We need to invoke this method with appropriate parameters to get the items from the list
    1. The first parameter to this method is the name of the list.
    2. The remaining parameters are there to filter the list based on user defined criteria’s. Without any criteria, to fetch all the items from the list, the method should look like the following.
      1. list.GetListItems(“MyListName”, “”, null, null, “”, null,null);
      2. The signature of GetListItems in MSDN
  6. The return type of the GetListItems() is an Xml Node.
  7. The XmlNode’s inner xml looks like the following.
  8. We need to process the xml node in order to access the list values.
    1. Below, I have processed the xml to obtain the value of the column ‘Title’ that is present in the list and displayed it in a label.
    1. The entire code is given below
    ListWS.Lists lists = new ListWS.Lists();
    lists.UseDefaultCredentials = true;
      XmlNode nodes = lists.GetListItems(“MyList1″, “”, null, null, “”, null, null);
        foreach (XmlNode node in nodes)
        {
            if (node.Name == “rs:data”)
            {
                for (int i = 0; i < node.ChildNodes.Count; i++)
                {
                    if (node.ChildNodes[i].Name == “z:row”)
                    {
                       lblResults.Text =  node.ChildNodes[i].Attributes["ows_Title"].Value;
                    }         }     } }

    9 comments:

    1. Oh my goodness! Impressive article dude! Thank you, However I am going through troubles
      with your RSS. I don't understand why I can't subscribe to it.

      Is there anybody else getting the same RSS issues?
      Anyone that knows the solution will you kindly respond?
      Thanks!!

      my website ... best registry cleaner
      My site :: registry cleaner

      ReplyDelete
    2. Heya i am for the first time here. I found this board and I find It truly useful & it helped me out a lot.

      I hope to give something back and help others like you aided me.



      Feel free to visit my web page: diet plan for women

      ReplyDelete
    3. This post is genuinely a pleasant one it helps new
      the web viewers, who are wishing in favor of blogging.


      My web-site www.grabthebucket.com

      ReplyDelete
    4. Right here is the perfect blog for everyone who would like to understand this topic.
      You know a whole lot its almost hard to
      argue with you (not that I personally would want to…HaHa).
      You certainly put a fresh spin on a subject which has been discussed for a long time.
      Excellent stuff, just great!

      Feel free to surf to my web blog :: forex system review

      ReplyDelete
    5. I know this if off topic but I'm looking into starting my own blog and was curious what all is required to get setup? I'm assuming having a blog like yours would cost a pretty penny?

      I'm not very internet savvy so I'm not 100% certain. Any
      suggestions or advice would be greatly appreciated.
      Thanks

      Feel free to visit my web page :: selling options

      ReplyDelete
    6. Asking questions are really good thing if you are not understanding anything completely, however this
      article offers fastidious understanding yet.

      Also visit my site: laser hair removal *http://www.hiarabs.com/blog/163129/secrets-driving-long-hairstyles*

      ReplyDelete
    7. Great post. I was checking constantly this blog and I'm impressed! Very useful info specially the last part :) I care for such info a lot. I was looking for this certain info for a very long time. Thank you and good luck.

      my site - longer hair

      ReplyDelete
    8. Hi there, There's no doubt that your site may be having browser compatibility problems. Whenever I look at your blog in Safari, it looks fine however, when opening in IE, it has some overlapping issues. I just wanted to give you a quick heads up! Besides that, wonderful website!

      My web-site hair straightener

      ReplyDelete
    9. hey
      one of best link on services
      thanks for share

      ReplyDelete