C#读取SharePoint的List中的数据及操作SharePoint

一般SharePoint都有提供一个webservice来访问、操作SharePoint的各项数据。

地址一般为http://server/_vti_bin/Lists.asmx


下面是一个简单的读取SharePoint List数据的例子:

        private void button1_Click(object sender, EventArgs e)
        {
            WebListService.Lists webLists = new WebListService.Lists();
            //webLists.UseDefaultCredentials = true;
            NetworkCredential ntc = new NetworkCredential("user", "password");
            webLists.Credentials = ntc;
            webLists.Url = "http://test.com/teams/SiteName/_vti_bin/Lists.asmx";

            XmlDocument xmlDoc = new XmlDocument();
            XmlElement query = xmlDoc.CreateElement("Query");
            //query.InnerXml = "data1";

            XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
            viewFields.InnerXml = "";

            XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");
            queryOptions.InnerXml = "FALSE";

            string result = webLists.GetListItems("{31AE732D-F6FC-4BD5-A51E-90895CF5BB1B}", null, query, viewFields, null, queryOptions, null).OuterXml;

            //XmlTextReader reader = new XmlTextReader(result, XmlNodeType.Document, null);
            //DataSet ds = new DataSet();
            //ds.ReadXml(reader);
            //reader.Close();
            //DataTable dt = ds.Tables[1];
            //MessageBox.Show(dt.Rows.Count.ToString());

            Clipboard.SetText(result);
            XmlDocument xdoc = new XmlDocument();
            xdoc.LoadXml(result);
            XmlNamespaceManager xnm = new XmlNamespaceManager(xdoc.NameTable);
            xnm.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
            xnm.AddNamespace("z", "#RowsetSchema");
            XmlNodeList xl = xdoc.DocumentElement.SelectNodes("//z:row", xnm);
            foreach (XmlNode item in xl)
            {
                MessageBox.Show(item.Attributes["ows_Title"].Value);
            }
        }

转换datatable,解析xml都可以。

其他操作同样调用对应的webservice即可。

至于其中的查询相关的3个参数可以使用一个软件来测试:SPCAMLQueryHelper.exe.zip

你可能感兴趣的:(c#)