Questo esempio di codice, per SharePoint 2007 (WSS3 / MOSS), costruisce un oggetto SPQuery in modo che ritorni solo gli elementi di una specifica cartella e tipo (file o folder).

Il filtro per folder si ottiene impostando la proprietà
ViewAttributes dell'oggetto SPQuery a Scope="RecursiveAll" e filtrando il percorso per il campo FileDirRef.
La scelta se visualizzare solo i file, solo i folder oppure entrambi si ottiene aggiungendo un filtro sul campo
FSObjType'''.

C#

using System;
using Microsoft.SharePoint;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      // parameters
      string url = "http://sharepoint2007/Site1/Shared Documents";
      string folderFilter = "/Area1/Sub1";
      DocumentToShow docType = DocumentToShow.FilesOnly;

      // open web
      using (SPSite site = new SPSite(url))
      {
        using (SPWeb web = site.OpenWeb())
        {
          // get list
          SPList list = web.GetList(url);

          // get correct internal field name
          SPField fldDirectory = list.Fields[SPBuiltInFieldId.FileDirRef];
          SPField fldType = list.Fields[SPBuiltInFieldId.FSObjType];

          // buid filter string by folder
          string filterUrl = list.RootFolder.ServerRelativeUrl + folderFilter;
          if (filterUrl.StartsWith("/"))
          {
            filterUrl = filterUrl.Substring(1);
          }
          if (filterUrl.EndsWith("/"))
          {
            filterUrl = filterUrl.Substring(0, filterUrl.Length-1);
          }
          filterUrl = filterUrl.Replace("//", "/");

          // build query
          SPQuery query = new SPQuery();
          query.ViewAttributes = "Scope=\"RecursiveAll\"";

          string where = "";
          if (docType == DocumentToShow.All)
          {
            // all, file AND folder
            where = string.Format(@"
<Where>
    <Eq><FieldRef Name=""{0}"" /><Value Type=""Lookup"">{1}</Value></Eq>
</Where>"
             , fldDirectory.InternalName
             , filterUrl);
          }
          else
          {
            // filter file OR folder
            where = string.Format(@"
<Where>
  <And>
    <Eq><FieldRef Name=""{0}"" /><Value Type=""Lookup"">{1}</Value></Eq>
    <Eq><FieldRef Name=""{2}"" /><Value Type=""Lookup"">{3}</Value></Eq>
  </And>
</Where>"
     , fldDirectory.InternalName
     , filterUrl
     , fldType.InternalName
     , docType == DocumentToShow.FilesOnly ? "0" : "1");
          }
          query.Query = where;

          //execute query
          SPListItemCollection items = list.GetItems(query);

          //show item
          Console.WriteLine("Type Date                Name");
          foreach (SPListItem item in items)
          {
            Console.WriteLine(
                string.Format("{0} {1} {2}"
                , (string)item[fldType.Id] == "1" ? "Dir " : "File"
                  , item[SPBuiltInFieldId.Modified]
                  , item[SPBuiltInFieldId.FileRef]));
          }
        }
      }
    }
  }
  public enum DocumentToShow
  {
    FilesOnly = 0,
    FolderOnly = 1,
    All = 2,
  }
}
che da un output simile al seguente

DOS / Batch file

Type Date                Name
File 6/1/2011 3:35:30 PM /Sito1/Shared Documents/Area1/Sub1/NotaSpese.xsn
File 6/1/2011 3:35:39 PM /Sito1/Shared Documents/Area1/Sub1/mappa_cww_2.xls

A questo link WSS Field Names for Lists & Document Libraries un interessante elenco dei campi di SharePoint con: i nomi dei campi interni, il display name, il Guid ed il tipo.
Tags:
SharePoint497 SharePoint 2007218
Potrebbe interessarti anche: