CAML Queries.. Getting CMSServerError in CmsHttpContext Current Searches GetByGuid method..
Oct 14
  • Ok, since we are trying to get Sharepoint list items via Sharepoint Web Services, we should add the web service then, as a reference to the project.

Adding Sharepoint web services as a web reference

The important point is; if you want to get listitems from a subsite’s list, don’t forget to change url like this;
For example;

Change url from;
http://<yoursite> /_vti_bin/Lists.asmx

To this;
http://<yoursite>/<yoursubsite> /_vti_bin/Lists.asmx

Otherwise, you can get a SoapException !

  • Set the credentials of the list.

List.Lists cl = new List.Lists();

System.Net.NetworkCredential cred = new System.Net.NetworkCredential(
                    listUser,
                    listPwd);

cl.Credentials = cred;

cl.Url = ConfigurationManager.AppSettings["List.Lists"];

  • Write a caml query to filter results. (you can look at caml query example from this posting.) (Or don’t write any query if you want to get whole items in the list)

System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
XmlNode whereQuery = xmlDoc.CreateElement(”Query”);

whereQuery.InnerXml = String.Format(”<Where>{0}</Where>”,”write a caml query like the one in the that posting”);

  • You can get list items by calling GetListItems method.

XmlNode listNodes = cl.GetListItems(”LISTNAME”, null, whereQuery, null, null, null, null);

  • We have an xml which contains all items. But in order to get only items you should select nodes under “//rs:data/z:row”.
  • First I had tried to select nodes under “//rs:data”. But the child count is doubled, because between each child there exists a node of Whitespace (\n). So in order to get list items properly, “z:row” nodes should be selected.

XmlNamespaceManager nm = new XmlNamespaceManager(listNodes.OwnerDocument.NameTable);
nm.AddNamespace(”rs”, “urn:schemas-microsoft-com:rowset”);
nm.AddNamespace(”z”, “#RowsetSchema”);
XmlNodeList nodes = listNodes.SelectNodes(”//rs:data/z:row”, nm);

  • To display Sharepoint listItems in a repeater, bind the XmlNodeList to the repeater, and configure the template of repeater in the aspx.

<asp:Repeater ID=”rptList” runat=”server”>

     <ItemTemplate>

            <table>

                <tr>

                    <td colspan=”2″>

                        <%# ((XmlNode)Container.DataItem).Attributes["ows_Title"].Value%>

                    </td>

                </tr>

                <tr>

                    <td align=”left”>

                        <%# ((XmlNode)Container.DataItem).Attributes["ows_Date"].Value%>

                    </td>

                    <td align=”right”>

                        <%#  ((XmlNode)Container.DataItem).Attributes["ows_Place"].Value%>

                    </td>

                </tr>

               </table>

         </ItemTemplate>

</asp:Repeater>

 

3 Responses to “Getting list items from a Sharepoint list by using Sharepoint Web Services”

  1. RYErnest Says:

    Nice post u have here :D Added to my RSS reader

  2. Timur I. Alhimenkov Says:

    Wow! Thank you!
    I always wanted to write in my blog something like that. Can I take part of your post to my site?
    Of course, I will add backlink?

    Sincerely, Reader

  3. mark Says:

    Very interesting site, Hope it will always be alive!

Leave a Reply