SPListItemCollection.GetDataTable() 不返回所有列?

发布于 2024-08-06 14:49:08 字数 665 浏览 5 评论 0原文

我在使用 GetDataTable() 方法时遇到了问题。我正在尝试返回结果中的默认 SharePoint 列“FileRef”以供使用。我将它包含在我的 SPQuery.ViewFields

查询中:

<Where><IsNotNull><FieldRef Name='FileRef'/></IsNotNull></Where>

ViewFields:

<FieldRef Name='Title' /><FieldRef Name='Category' /><FieldRef Name='FileRef' /><FieldRef Name='ID' /><FieldRef Name='Created' />

我什至可以看到它在 items.XML 中返回,但是当我调用 GetDataTable() 时,它没有放入数据表中。

SPListItemCollection items = list.GetItems(spq);
dtItems = items.GetDataTable();

为什么 GetDataTable 无法正常工作?我必须编写自己的转换方法吗?

I ran into an issue when using the GetDataTable() method. I'm trying to return the default SharePoint column "FileRef" in my results to use. I include it in my SPQuery.ViewFields

Query:

<Where><IsNotNull><FieldRef Name='FileRef'/></IsNotNull></Where>

ViewFields:

<FieldRef Name='Title' /><FieldRef Name='Category' /><FieldRef Name='FileRef' /><FieldRef Name='ID' /><FieldRef Name='Created' />

I can even see it returned in the items.XML but when I call GetDataTable() it is not put in the datatable.

SPListItemCollection items = list.GetItems(spq);
dtItems = items.GetDataTable();

Why isn't GetDataTable working correctly? Am I going to have to write my own conversion method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

誰ツ都不明白 2024-08-13 14:49:08

我推荐您一个更好的解决方案

由于 SPListItemCollection 具有存储所有项目数据的 Xml 属性,您可以使用 此 XSLT 以普通 XML 格式获取数据,然后从 XML 创建 DataSet。

这个想法可以转换为方便的扩展功能:

using System.Data;
using System.Xml;
using System.Xml.Xsl;
using Microsoft.SharePoint;

namespace Balticovo.SharePoint
{
    public static partial class Extensions
    {
        static string sFromRowsetToRegularXmlXslt =
                "<xsl:stylesheet version=\"1.0\" " +
                 "xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" " +
                 "xmlns:s=\"uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882\" " +
                 "xmlns:z=\"#RowsetSchema\">" +

             "<s:Schema id=\"RowsetSchema\"/>" +

             "<xsl:output method=\"xml\" omit-xml-declaration=\"yes\" />" +

             "<xsl:template match=\"/\">" +
              "<xsl:text disable-output-escaping=\"yes\"><rows></xsl:text>" +
              "<xsl:apply-templates select=\"//z:row\"/>" +
              "<xsl:text disable-output-escaping=\"yes\"></rows></xsl:text>" +
             "</xsl:template>" +

             "<xsl:template match=\"z:row\">" +
              "<xsl:text disable-output-escaping=\"yes\"><row></xsl:text>" +
              "<xsl:apply-templates select=\"@*\"/>" +
              "<xsl:text disable-output-escaping=\"yes\"></row></xsl:text>" +
             "</xsl:template>" +

             "<xsl:template match=\"@*\">" +
              "<xsl:text disable-output-escaping=\"yes\"><</xsl:text>" +
              "<xsl:value-of select=\"substring-after(name(), 'ows_')\"/>" +
              "<xsl:text disable-output-escaping=\"yes\">></xsl:text>" +
              "<xsl:value-of select=\".\"/>" +
              "<xsl:text disable-output-escaping=\"yes\"></</xsl:text>" +
              "<xsl:value-of select=\"substring-after(name(), 'ows_')\"/>" +
              "<xsl:text disable-output-escaping=\"yes\">></xsl:text>" +
             "</xsl:template>" +
            "</xsl:stylesheet>";

        public static DataTable GetFullDataTable(this SPListItemCollection itemCollection)
        {
            DataSet ds = new DataSet();

            string xmlData = ConvertZRowToRegularXml(itemCollection.Xml);
            if (string.IsNullOrEmpty(xmlData))
                return null;

            using (System.IO.StringReader sr = new System.IO.StringReader(xmlData))
            {
                ds.ReadXml(sr, XmlReadMode.Auto);

                if (ds.Tables.Count == 0)
                    return null;

                return ds.Tables[0];
            }
        }

        static string ConvertZRowToRegularXml(string zRowData)
        {
            XslCompiledTransform transform = new XslCompiledTransform();
            XmlDocument tidyXsl = new XmlDocument();

            try
            {
                //Transformer
                tidyXsl.LoadXml(Extensions.sFromRowsetToRegularXmlXslt);
                transform.Load(tidyXsl);

                //output (result) writers
                using (System.IO.StringWriter sw = new System.IO.StringWriter())
                {
                    using (XmlTextWriter tw = new XmlTextWriter(sw))
                    {
                        //Source (input) readers
                        using (System.IO.StringReader srZRow = new System.IO.StringReader(zRowData))
                        {
                            using (XmlTextReader xtrZRow = new XmlTextReader(srZRow))
                            {
                                //Transform
                                transform.Transform(xtrZRow, null, tw);
                                return sw.ToString();
                            }
                        }
                    }
                }
            }
            catch
            {
                return null;
            }
        }
    }   
}

顺便说一句,使用此方法,如果需要,您将获得文件附件 URL (SPQuery.IncludeAttachmentUrls = true),而不仅仅是 TRUE/FALSE 值,因为您将通过使用 前面提到的方法

I'd recommend you a better solution

As SPListItemCollection has Xml proeprty that stores all item data, you can use this XSLT to get data in normal XML format and then create DataSet from XML.

This Idea can be converted to handy extension function:

using System.Data;
using System.Xml;
using System.Xml.Xsl;
using Microsoft.SharePoint;

namespace Balticovo.SharePoint
{
    public static partial class Extensions
    {
        static string sFromRowsetToRegularXmlXslt =
                "<xsl:stylesheet version=\"1.0\" " +
                 "xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" " +
                 "xmlns:s=\"uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882\" " +
                 "xmlns:z=\"#RowsetSchema\">" +

             "<s:Schema id=\"RowsetSchema\"/>" +

             "<xsl:output method=\"xml\" omit-xml-declaration=\"yes\" />" +

             "<xsl:template match=\"/\">" +
              "<xsl:text disable-output-escaping=\"yes\"><rows></xsl:text>" +
              "<xsl:apply-templates select=\"//z:row\"/>" +
              "<xsl:text disable-output-escaping=\"yes\"></rows></xsl:text>" +
             "</xsl:template>" +

             "<xsl:template match=\"z:row\">" +
              "<xsl:text disable-output-escaping=\"yes\"><row></xsl:text>" +
              "<xsl:apply-templates select=\"@*\"/>" +
              "<xsl:text disable-output-escaping=\"yes\"></row></xsl:text>" +
             "</xsl:template>" +

             "<xsl:template match=\"@*\">" +
              "<xsl:text disable-output-escaping=\"yes\"><</xsl:text>" +
              "<xsl:value-of select=\"substring-after(name(), 'ows_')\"/>" +
              "<xsl:text disable-output-escaping=\"yes\">></xsl:text>" +
              "<xsl:value-of select=\".\"/>" +
              "<xsl:text disable-output-escaping=\"yes\"></</xsl:text>" +
              "<xsl:value-of select=\"substring-after(name(), 'ows_')\"/>" +
              "<xsl:text disable-output-escaping=\"yes\">></xsl:text>" +
             "</xsl:template>" +
            "</xsl:stylesheet>";

        public static DataTable GetFullDataTable(this SPListItemCollection itemCollection)
        {
            DataSet ds = new DataSet();

            string xmlData = ConvertZRowToRegularXml(itemCollection.Xml);
            if (string.IsNullOrEmpty(xmlData))
                return null;

            using (System.IO.StringReader sr = new System.IO.StringReader(xmlData))
            {
                ds.ReadXml(sr, XmlReadMode.Auto);

                if (ds.Tables.Count == 0)
                    return null;

                return ds.Tables[0];
            }
        }

        static string ConvertZRowToRegularXml(string zRowData)
        {
            XslCompiledTransform transform = new XslCompiledTransform();
            XmlDocument tidyXsl = new XmlDocument();

            try
            {
                //Transformer
                tidyXsl.LoadXml(Extensions.sFromRowsetToRegularXmlXslt);
                transform.Load(tidyXsl);

                //output (result) writers
                using (System.IO.StringWriter sw = new System.IO.StringWriter())
                {
                    using (XmlTextWriter tw = new XmlTextWriter(sw))
                    {
                        //Source (input) readers
                        using (System.IO.StringReader srZRow = new System.IO.StringReader(zRowData))
                        {
                            using (XmlTextReader xtrZRow = new XmlTextReader(srZRow))
                            {
                                //Transform
                                transform.Transform(xtrZRow, null, tw);
                                return sw.ToString();
                            }
                        }
                    }
                }
            }
            catch
            {
                return null;
            }
        }
    }   
}

By the way, using this method, you will get, if needed, file attachment URL's (SPQuery.IncludeAttachmentUrls = true) not just TRUE/FALSE values as you would get it by using previously mentioned method.

月棠 2024-08-13 14:49:08

关于 Janis 的回答 - 我会删除在 ows_ 上执行子字符串的位并尝试删除它,只需使用:-

"<xsl:value-of select=\"name()\"/>" +

因为 SP2010 现在包含 ETag 等不包含“ows_”的字段,并且解决方案失败。否则非常好的解决方案。

Regarding Janis' answer - I'd remove the bits that do a substring on the ows_ and attempt to remove it, just use:-

"<xsl:value-of select=\"name()\"/>" +

because SP2010 now includes fields such as ETag which don't being with "ows_" and the solution fails. Very good solution otherwise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文