Top Menu

Wednesday, February 22, 2012

Query a List via Lists.asmx

A quick .NET function to Query a MOSS List via Lists.asmx web service. I use this function time to time, so thought to post here.
Note: I already have posted similer code in my previous post but this is the full version.

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Xml;
using System.IO;

namespace ELLP_Content
{
    class Program
    {
        static void Main(string[] args)
        {
            SharePointManagement objSPManagement = new SharePointManagement();
            objSPManagement.GetListContent();
        }
    }

    public class SharePointManagement
    {
        MOSSListsService.Lists objListService = new ELLP_Content.MOSSListsService.Lists();

        public void GetListContent()
        {
            try
            {
                string strSiteURL = ConfigurationSettings.AppSettings["SiteURL"];
                string strUserName = ConfigurationSettings.AppSettings["Login"];
                string strDomain = string.Empty;
                string strPassword = ConfigurationSettings.AppSettings["Password"];
                string strListName = ConfigurationSettings.AppSettings["List"];

                #region Source URL
                //get the lists for the Source URL
                objListService.Url = strSiteURL.Trim('/') + "/_vti_bin/lists.asmx";

                //get the domain
                if (strUserName.IndexOf("\\") > 0)
                {
                    strDomain = strUserName.Split("\\".ToCharArray())[0];
                    strUserName = strUserName.Split("\\".ToCharArray())[1];
                }

                objListService.Credentials = new System.Net.NetworkCredential(strUserName, strPassword, strDomain);

                #endregion


                //get the Data from the List
                System.Xml.XmlDocument xdListData = new System.Xml.XmlDocument();
                System.Xml.XmlNode xnQuery = xdListData.CreateElement("Query");
                System.Xml.XmlNode xnViewFields = xdListData.CreateElement("ViewFields");
                System.Xml.XmlNode xnQueryOptions = xdListData.CreateElement("QueryOptions");

                //*Use CAML query*/ 
                xnQuery.InnerXml = "<Where><Neq><FieldRef Name=\"Exclude_x0020_From_x0020_Country\" /><Value Type=\"Boolean\">0</Value></Neq></Where>";

                xnQueryOptions.InnerXml = "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns><DateInUtc>TRUE</DateInUtc>";

                System.Xml.XmlNode xnListData = objListService.GetListItems(strListName, null, xnQuery, xnViewFields, null, xnQueryOptions, null);


                foreach (System.Xml.XmlNode node in xnListData)
                {
                    if (node.Name == "rs:data")
                    {
                        for (int i = 0; i < node.ChildNodes.Count; i++)
                        {
                            if (node.ChildNodes[i].Name == "z:row")
                            {
                                Console.WriteLine(node.ChildNodes[i].Attributes["ows_Title"].Value);
                            }
                        }
                    }
                }

                Console.ReadKey();
            }
            catch (System.Web.Services.Protocols.SoapException ex)
            {
                this.WriteLog("Message:\n" + ex.Message + "\nDetail:\n" + ex.Detail.InnerText + "\nStackTrace:\n" + ex.StackTrace);
            }
        }

        private void WriteLog(string strLine)
        {

            string FilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigurationSettings.AppSettings["InputLog"]);
            StreamWriter objFile;

            if (!File.Exists(FilePath))
            {
                objFile = new StreamWriter(FilePath);
            }
            else
            {
                objFile = File.AppendText(FilePath);
            }

            objFile.WriteLine(DateTime.Now + "\\t" + strLine);
            objFile.WriteLine();
            objFile.Close();

        }
    }
}

SharePoint Navigation List Limitation

Today, one of the developers from Ireland team faced a problem while adding a link to the MOSS Navigation list. The item was simply not showing up on the left navigation. After doing some research we found out that in SharePoint 2007 only 50 items can be added to the navigation list.

Official SharePoint Documentation

I have recently contributed to the official SharePoint documentation for developement. Check it out here: https://docs.microsoft.com/en-us...