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 = "<Where><Eq><FieldRef Name=\"Title\"></FieldRef><Value Type=\"Text\">data1</Value></Eq></Where>";
XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
viewFields.InnerXml = "<FieldRef Name=\"Title\" /><FieldRef Name=\"Name\" /><FieldRef Name=\"Age\" />";
XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");
queryOptions.InnerXml = "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>";
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即可。
---------------------
作者:Hauk
來源:CSDN
原文:https://blog.csdn.net/haukwong/article/details/8994044
版權宣告:本文為博主原創文章,轉載請附上博文連結!