Thursday, March 1, 2012

Get SharePoint List Views using Web Services

Following is an example of displaying Views of a list in a DataGrid Control from SharePoint 2007 using Views.asmx and Lists.asmx Web Services.
public class SharePointManagement
{
    ViewsService.Views objViewsService = new SPViewsService.ViewsService.Views();
    ListsService.Lists objListsService = new SPViewsService.ListsService.Lists();
    string strSiteURL = "http://mySharepointServer:1100";
    string strUserName = "username";
    string strDomain = "us";
    string strPassword = "password";
    string strListName = "Announcements";
    string strQuery = "";
    string strViewFields = "";
    string strQueryOptions = "";

    ArrayList lstViewID = new ArrayList();

    public void GetViewQuery(string strListName)
    {
        try
        {
            #region Source URL
            //get the lists for the Source URL
            this.objViewsService.Url = this.strSiteURL.Trim('/') + "/_vti_bin/views.asmx";

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

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


            #endregion

            System.Xml.XmlNode xnAllView = this.objViewsService.GetViewCollection(strListName);
            foreach (System.Xml.XmlNode node in xnAllView)
            {
                if (node.Name == "View")
                {
                    this.lstViewID.Add(node.Attributes["Name"].Value);
                }
            }

            //Get the First View
            System.Xml.XmlNode xnViewData = this.objViewsService.GetView(this.strListName, lstViewID[0].ToString());

            foreach (System.Xml.XmlNode node in xnViewData)
            {
                if (node.Name == "Query")
                {
                    this.strQuery = node.InnerXml;
                }
                if (node.Name == "ViewFields")
                {
                    this.strViewFields = node.InnerXml;
                }
            }

        }
        catch (System.Exception exp)
        {
            MessageBox.Show(exp.ToString());
        }
    }

    public void GetViewData()
    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();

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

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

            this.objListsService.Credentials = new System.Net.NetworkCredential(this.strUserName, this.strPassword, this.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 = this.strQuery;
            xnViewFields.InnerXml = this.strViewFields;
            xnQueryOptions.InnerXml = this.strQueryOptions;

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


            StringReader sr = new StringReader(xnListData.OuterXml.Replace("ows_", ""));
            ds.ReadXml(sr);

            return dt;
        }
        catch (System.Exception exp)
        {
            MessageBox.Show(exp.ToString());
            return dt;
        }
    }
}

Official SharePoint Documentation

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