c# - 使用 SharePoint Web 服务时如何仅返回列表的某些列?

标签 c# web-services list sharepoint caml

SharePoint Web 服务有一个具有以下签名的方法:

 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”和“标题”列,例如“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);
        }

所以,现在我想编写一个新方法,例如:

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>";
}

ViewFieldsIncludeMandatoryColumns 的组合在过去对我有用。

关于c# - 使用 SharePoint Web 服务时如何仅返回列表的某些列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9889900/

相关文章:

flash - 访问 Flash CS3 AS3 项目中的 Web 服务

c++ - 在复制构造函数中设置嵌套在类中的结构变量

c# - 当我想在我的类/方法中使用不同类型时,是否有比泛型更好的 Ant 设计解决方案

C# 从循环中启动线程抛出 IndexOutOfBoundsException

c# - BindingList<> ListChanged 事件

c# transactionscope 只需要回滚一些更改

抛出 NoEndPointException 的 Java Axis Webservice stub

.net - 如何使WCF服务STA(单线程)

python - 优雅地扩展几个列表

java - 数组中 int 元素的随机整数