Question : code check

I wrote some code to return some sharepoint data. Was all easy enough using GetListItems().
However now I am finding myself having to re-run the code several times before the page is served.
I am creating a list of links on a aspx page, the links are made up of text from several lists. As I can't find a decent way to combine list data I'm having to return back to GetListItems() and bring in back in chunks, one list at a time.

Now that I've gone from one method, to 2 and probably on to 6 or 7 in the end(!) I'm worried about where I am declaring variables and if things are being closed/disposed of as they should.

The methods are in my class file (see the code window) and they are called one after the other on page load of my main page...

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ExtractListData etl = new ExtractListData();
        string theZones = "";
        theZones = etl.GetListZonesForTree();
        Response.Write(theZones);
        Response.Write("--------------------------<br />");
        string theIssues = "";
        theIssues = etl.GetListIssuesForTree();
        Response.Write(theIssues);


    }
}

Can someone take a look at let me know the best way to declare/instantiate and destroy please.
I seem to be declaring the list and other stuff repeatedly.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
using System;
using System.Collections.Generic;
using System.Web;


namespace GetListStuff
{
    public class ExtractListData
    {
        public ExtractListData()
        {
            //
            // TODO: Add constructor logic here
            //
        }


        public string GetListZonesForTree()
        {


            DPlist.Lists myListservice = new DPlist.Lists();

            myListservice.Credentials = System.Net.CredentialCache.DefaultCredentials;
            myListservice.Url = "http://xxxx/_vti_bin/Lists.asmx";


            /* Assign values to pass the GetListItems method*/
            string thelistName = "Zones"; // "{B6E894E8-EBB3-4E41-8416-E8150B0EC57B}";

            //Maybe we can create views in sharepoint that are specifically used for our web app?
            //string viewName = "{C5C450CF-CEA0-4081-B4BE-7D7DA06C8677}";
            string rowLimit = "500";
            // Instantiate an XmlDocument object 
            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
            System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
            System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
            System.Xml.XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");
            /*Use CAML query*/

            query.InnerXml = "";//"<Where><Gt><FieldRef Name=\"ID\" /><Value Type=\"Counter\">0</Value></Gt></Where>";

            viewFields.InnerXml = "<FieldRef Name=\"Title\" /><FieldRef Name=\"Zone_x0020_Number\" />";

            queryOptions.InnerXml = "";

            System.Xml.XmlNode nodes = myListservice.GetListItems(thelistName, null, query, viewFields, rowLimit, queryOptions, null);

            string ReturnedZoneItems = "";

            foreach (System.Xml.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")
                        {

                            string theURL = string.Concat("<a href='Zones.aspx?ZoneID=",node.ChildNodes[i].Attributes["ows_Zone_x0020_Number"].Value,"'>");
                            ReturnedZoneItems += string.Concat(theURL,node.ChildNodes[i].Attributes["ows_Title"].Value ?? String.Empty,"</a><br />");
                      

                        }

                    }

                }
                

            }
            return ReturnedZoneItems;
            
        }

        public string GetListIssuesForTree()
        {


            DPlist.Lists myListservice = new DPlist.Lists();

            myListservice.Credentials = System.Net.CredentialCache.DefaultCredentials;
            myListservice.Url = "http://xxxxxxx/_vti_bin/Lists.asmx";


            /* Assign values to pass the GetListItems method*/
            string thelistName = "Issues"; // "{B6E894E8-EBB3-4E41-8416-E8150B0EC57B}";

            //Maybe we can create views in sharepoint that are specifically used for our web app?
            //string viewName = "{C5C450CF-CEA0-4081-B4BE-7D7DA06C8677}";
            string rowLimit = "500";
            // Instantiate an XmlDocument object 
            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
            System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
            System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
            System.Xml.XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");
            /*Use CAML query*/

            query.InnerXml = "";

            viewFields.InnerXml = "<FieldRef Name=\"Title\" /><FieldRef Name=\"Issue_x0020_Number\" />";

            queryOptions.InnerXml = "";

            System.Xml.XmlNode nodes = myListservice.GetListItems(thelistName, null, query, viewFields, rowLimit, queryOptions, null);

            string ReturnedIssueItems = "";

            foreach (System.Xml.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")
                        {

                            string theURL = "<a href='Issues.aspx?IssueID=" + node.ChildNodes[i].Attributes["ows_Issue_x0020_Number"].Value + "'>";
                            ReturnedIssueItems += string.Concat(theURL, node.ChildNodes[i].Attributes["ows_Title"].Value ?? String.Empty, "</a><br />");


                        }

                    }

                }


            }
            return ReturnedIssueItems;

        }



    }


}

Answer : code check


Hi,

I tried writing a generic function - GetData and passing the generic parameters. It may not be exact code, but it works. I hope it helps.

Cheers ...
Rajendra

-------------------------------------------------------------------------------------------------------------------------------------------

protected void Page_Load(object sender, EventArgs e)
    {
        string returnItems = GetData("City", "", "<FieldRef Name='Title' /><FieldRef Name='Country' />", "zones.aspx?ZoneID=", "ows_ID");
        Response.Write(returnItems);
    }

    private string GetData(string thelistName, string strQuery, string strViewFields, string aspx, string idColumn)
    {
        Lists.Lists myListservice = new Lists.Lists();
        myListservice.Credentials = System.Net.CredentialCache.DefaultCredentials;
        myListservice.Url = "http://localhost/subsite1/_vti_bin/Lists.asmx";

        /* Assign values to pass the GetListItems method*/
        //string thelistName = "Zones"; // "{B6E894E8-EBB3-4E41-8416-E8150B0EC57B}";

        //Maybe we can create views in sharepoint that are specifically used for our web app?
        //string viewName = "{C5C450CF-CEA0-4081-B4BE-7D7DA06C8677}";
        string rowLimit = "500";

        // Instantiate an XmlDocument object
        System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
        System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
        System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
        System.Xml.XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");
        /*Use CAML query*/

        query.InnerXml = strQuery; // "";//"<Where><Gt><FieldRef Name=\"ID\" /><Value Type=\"Counter\">0</Value></Gt></Where>";
        viewFields.InnerXml = strViewFields; // "<FieldRef Name=\"Title\" /><FieldRef Name=\"Zone_x0020_Number\" />";
        queryOptions.InnerXml = "";

        System.Xml.XmlNode nodes = myListservice.GetListItems(thelistName, null, query, viewFields, rowLimit, queryOptions, null);
        string returnedItems = "";
        foreach (System.Xml.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")
                    {
                        string theURL = string.Concat("<a href='" + aspx , node.ChildNodes[i].Attributes[idColumn].Value, "'>");
                        returnedItems += string.Concat(theURL, node.ChildNodes[i].Attributes["ows_Title"].Value ?? String.Empty, "</a><br />");
                    }
                }
            }
        }
        return returnedItems;
    }
Random Solutions  
 
programming4us programming4us