Domanda

Mi sono imbattuto in un problema quando si utilizza il metodo GetDataTable (). Sto cercando di riportare la colonna predefinita di SharePoint "fileRef" nei miei risultati da utilizzare. Includo nei miei SPQuery.ViewFields

Domanda:

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

ViewFields:

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

posso anche vederlo restituito nel items.xml, ma quando chiamo GetDataTable () non viene messo in datatable.

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

Perché non GetDataTable funziona correttamente? Sto andando ad avere per scrivere il mio metodo di conversione?

Altri suggerimenti

vi consiglio di una soluzione migliore

Come SPListItemCollection ha Xml proeprty che memorizza tutti i dati elemento, è possibile utilizzare questo XSLT per ottenere i dati in formato XML normale e quindi creare DataSet da XML.

Questa idea può essere convertito in funzione di estensione a portata di mano:

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\">&lt;rows&gt;</xsl:text>" +
              "<xsl:apply-templates select=\"//z:row\"/>" +
              "<xsl:text disable-output-escaping=\"yes\">&lt;/rows&gt;</xsl:text>" +
             "</xsl:template>" +

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

             "<xsl:template match=\"@*\">" +
              "<xsl:text disable-output-escaping=\"yes\">&lt;</xsl:text>" +
              "<xsl:value-of select=\"substring-after(name(), 'ows_')\"/>" +
              "<xsl:text disable-output-escaping=\"yes\">&gt;</xsl:text>" +
              "<xsl:value-of select=\".\"/>" +
              "<xsl:text disable-output-escaping=\"yes\">&lt;/</xsl:text>" +
              "<xsl:value-of select=\"substring-after(name(), 'ows_')\"/>" +
              "<xsl:text disable-output-escaping=\"yes\">&gt;</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;
            }
        }
    }   
}

Tra l'altro, con questo metodo, si ottengono, se necessario, di URL di file allegato (SPQuery.IncludeAttachmentUrls = true) non solo i valori TRUE / FALSE come si potrebbe ottenere utilizzando già citato metodo .

Per quanto riguarda la risposta Janis' - mi piacerebbe rimuovere i bit che fanno una sottostringa sul ows_ e si tenta di rimuoverlo, basta usare: -

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

perché SP2010 ora include campi come ETag che non essere con "ows_" e la soluzione viene a mancare. Molto buona soluzione altrimenti.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top