如何在使用SharePoint Web服务时仅返回列表的某些列
本文关键字:列表 返回 服务 Web SharePoint | 更新日期: 2023-09-27 18:29:11
SharePoint Web Service有一个具有以下签名的方法:
public System.Xml.XmlNode GetListItems(string listName, string viewName, System.Xml.XmlNode query, System.Xml.XmlNode viewFields, string rowLimit, System.Xml.XmlNode queryOptions, string webID) {
object[] results = this.Invoke("GetListItems", new object[] {
listName,
viewName,
query,
viewFields,
rowLimit,
queryOptions,
webID});
return ((System.Xml.XmlNode)(results[0]));
}
我只想返回列表中的"ID"answers"Title"列,例如"ProductNames",而不是返回所有列。
如何使用此Web方法实现只返回某些列?
我使用以下方法返回所有列:
public XmlNode GetListItems(string listName, XmlElement camlQuery)
{
sp.Lists listService = this.getSPListService();
XmlDocument xmlDoc = new XmlDocument();
XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");
XmlElement foldersEl = xmlDoc.CreateElement("Folder");
if (camlQuery == null) // override default view filters
{
camlQuery = xmlDoc.CreateElement("Query");
camlQuery.InnerXml = "<Where><Gt><FieldRef Name='ID'/><Value Type='Number'>0</Value></Gt></Where>";
}
queryOptions.AppendChild(foldersEl);
return listService.GetListItems(listName, null, camlQuery, ndViewFields, int.MaxValue.ToString(), queryOptions, null);
}
所以,现在我想写一个新的方法,比如:
public XmlNode GetListItems(string listName, XmlElement camlQueryForFilteringRows, bool includeAllColumns, string[] columnNamesToInclude)
{
}
并用称之为
var listItems = GetListItems("ProductNames", null, new [] {"ID", "Title"});
我尝试将以下结构添加到ViewFields变量中,但它仍然返回所有列:
<ViewFields>
<FieldRef Name="ID" />
<FieldRef Name="Title" />
</ViewFields>
谢谢,
试试这样的东西:
XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");
if (!includeAllColumns)
{
ndViewFields.InnerXml =
string.Join(string.Empty,
columnNamesToInclude
.Select(t1 => string.Format("<FieldRef Name='"{0}'" />", t1))
.ToArray());
queryOptions.InnerXml = "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>";
}
ViewFields
和IncludeMandatoryColumns
的组合在过去对我很有效。
尝试将queryoption ViewFieldsOnly设置为True。