C# 如何在使用SharePoint Web服务时仅返回列表的某些列?
SharePoint Web Service具有具有以下签名的方法:C# 如何在使用SharePoint Web服务时仅返回列表的某些列?,c#,web-services,list,sharepoint,caml,C#,Web Services,List,Sharepoint,Caml,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
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”和“Title”列,例如“ProductNames”,而不是返回所有列
如何使用此webmethod实现只返回某些列
我使用以下方法返回所有列:
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);
}
<ViewFields>
<FieldRef Name="ID" />
<FieldRef Name="Title" />
</ViewFields>
并称之为
var listItems = GetListItems("ProductNames", null, new [] {"ID", "Title"});
我尝试将以下结构添加到ViewFields变量,但它仍然返回所有列:
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);
}
<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>";
}
XmlNode ndViewFields=xmlDoc.CreateNode(XmlNodeType.Element,“ViewFields”和“”);
xmlement queryOptions=xmlDoc.CreateElement(“queryOptions”);
如果(!includealcolumns)
{
ndViewFields.InnerXml=
Join(string.Empty,
列名称包括
.Select(t1=>string.Format(“,t1))
.ToArray());
queryOptions.InnerXml=“FALSE”;
}
ViewFields
和IncludeMandatoryColumns
的组合在过去对我很有效。尝试将queryoption ViewFieldsOnly设置为True 也许一年前是这样,但现在肯定不行了。将IncludeMandatoryColumns设置为FALSE不会删除系统字段,也不会将ViewFieldsOnly设置为TRUE。网络上有很多论坛将此报告为bug。我注意到它首先引入了您在ViewFields中指定的字段。因此,您可能会做一个只返回前X列的变通方法,其中X是指定的视场数。我相信这是一个bug。但是,它会首先引入您在ViewFields中指定的字段。因此,您可以采取一种可能的解决方法,只返回前X列,其中X是指定的视场数。