次の方法で共有


SharePoint 2007 (MOSS/WSS) using Lists.asmx - GetListItems

Requirement: I have a custom desktop application and I have created an installer for it. During installation it needs to connect trough Internet to receive the key. Now I have stored these keys in a SharePoint List as given below:

image

The user will provide the Activation Key and Registration Number and will receive the Unlock Key to proceed with the software installation. We can use the OOB Lists.asmx web service to achieve this. The web method we are going to use is GetListItems.

I have created a Windows Application to test. Here I'll add Activation Key and Registration Number and will receive the Unlock Key.

image

In the application I add web reference of https://<site_url>/_vti_bin/lists.asmx with a name ListProxy.

Here is the code of my application:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Xml;

namespace TestKeyApp

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)

        {

        }

        private void button1_Click(object sender, EventArgs e)

        {

            ListProxy.Lists newListProxy = new ListProxy.Lists();

            //change the username, password and domain with the value of a use who has permission to the list

            newListProxy.Credentials = new System.Net.NetworkCredential("<username>", "<password>", "<domain>");

            XmlDocument xmlDoc = new XmlDocument();

            XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");

            XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");

            ndViewFields.InnerXml = "<FieldRef Name='Unlock_x0020_Key'/>";

            ndQuery.InnerXml = "<Where><And><Eq><FieldRef Name='Activation_x0020_Key' /><Value Type='Text'>" + textBox1.Text + "</Value></Eq><Eq><FieldRef Name='Registration_x0020_Number' /><Value Type='Text'>" + textBox2.Text + "</Value></Eq></And></Where>";

            try

            {

                XmlNode ndListItems = newListProxy.GetListItems("KeyList", null, ndQuery, ndViewFields, null, null, null);

                //MessageBox.Show(ndListItems.OuterXml);

                XmlNamespaceManager nsManager = new XmlNamespaceManager(ndListItems.OwnerDocument.NameTable);

                nsManager.AddNamespace("z", "#RowsetSchema");

                nsManager.AddNamespace("s", "uuid:BDC6E3F0-6DA3-11d1-A2A3-0AA00C14882");

                nsManager.AddNamespace("dt", "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882");

                nsManager.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");

                XmlNodeList nodes = ndListItems.SelectNodes("rs:data/z:row", nsManager);

                foreach (XmlNode node in nodes)

      {

//label4 is where I am showing the unlock key

label4.Text += node.Attributes["ows_Unlock_x0020_Key"].Value;

      }

               

            }

            catch (Exception ex)

            {

                MessageBox.Show("Please Enter correct activation key and registration number, message: " + ex.Message);

            }

        }

    }

}

Comments

  • Anonymous
    November 24, 2008
    PingBack from http://blog.a-foton.ru/index.php/2008/11/24/sharepoint-2007-mosswss-using-listsasmx-getlistitems/

  • Anonymous
    November 25, 2008
    That's a strange way to build XML. Why create some nodes as XmlNode, and other as plain text all the way? Also, when you use this kind of stuff:  "<Value Type='Text'>" + textBox1.Text + "</Value>" what happens if user input contains reserved XML characters (such as & or <)?

  • Anonymous
    November 28, 2008
    The comment has been removed

  • Anonymous
    May 29, 2009
    I find your article interesting. Currently, I am developing a SharePoint web services on Visual Studio 2008, VB.NET window form project.  I successfully manage to call the GetListitem and it return xml data.   The only thing I have great difficulties is <ViewFields>. Here is my situation: On a SharePoint site, there is a list call ABC. ABC has ID, Package, Version and TargetLevel.  On the ABC list, ID is a counter, automaticall generate by SharePoint; Package is a combo drop list containg all package names.  The user select one package or leave it blank.  Package is not mandatory; Version is a text field; TargetLevel is a combo drop list contaings all Target names. This is a mandatory field and user must select a TargetLevel from the combo drop list; Lets assume that ABC has only two records which I have shown above.   On the ABC list, the there are columns all showing, ID, Package, Version and TargetLevel. The two records exists on ABC lists clearly like this: Id          Package    Version      TargetLevel 27809       aaa        12.12.12     target1 27808                  13.13.13     target2 The package for 27808 is blank.  the package for 27809 is aaa. I use GetListItems to retrieve all records from ABC.  That worked fine and the xml results will be: Id          Package    Version      TargetLevel 27809       aaa        12.12.12     target1 27808                  13.13.13     target2 Scenario #1 If I filter the query to get only 27809, the xml result will be: Id          Package    Version      TargetLevel 27809       aaa        12.12.12     target1 Scenario #2 If I filter the query to get 27808, the xml result will be: Id          Version      TargetLevel 27808       13.13.13     target2 In scenario #2.  I dont know why Package column is missing.  I want the xml result to return as an empty "package" column like Id          Package    Version      TargetLevel 27808                  13.13.13     target2 I use the internal name when using with ViewFields.  I have retest it over and over. I am pretty sure that any records that have blank value for package will not display Package column with blank values.   I dont understand why Package column disappeared when there is no data for package!   i follow the syntax at http://msdn.microsoft.com/en-us/library/lists.lists.getlistitems.aspx and http://msdn.microsoft.com/en-us/library/ms467521.aspx and http://msdn.microsoft.com/en-us/library/dd588595.aspx and  http://sharepointmagazine.net/technical/development/writing-caml-queries-for-retrieving-list-items-from-a-sharepoint-list I dont know what else im missing and do you have any ideas why <ViewFields> ommit the package column????? I would be eternity grateful if you could assist me. Thanks. Regards, Mak