Partilhar via


Building Block: Files and Documents

Applies to: SharePoint Foundation 2010

In the context of Microsoft SharePoint Foundation, "file" may refer to a document that is associated with a list item in a document library, or it may refer to a site or application .aspx page that provides a user interface. For information about site pages and application pages, see Building Block: Pages and User Interface.

Each document in a document library is represented by both an SPListItem object and an SPFile object. When you return a list item from a document library, you can use the File property to retrieve the file that is associated with the item. The Files property of the SPFolder class returns all the files within a document library folder, and the Files property of SPWeb returns the .aspx files that are used within the website. An efficient way to return a specific folder is to use the website's GetFolder() method.

Note

Consider that for document libraries, as for generic lists, an SPFolder object counts as a list item. Code that enumerates all the items in a document library can use the FileSystemObjectType property of a list item to distinguish a folder from a file.

To add a file to a document library, use one of the Add() methods of the SPFileCollection class. To copy a file from one location to another within a website or between websites, use the Copy() method of the associated list item object. To add a site page or application page to a website, use the module Feature to provision the file. For information about Features, see Building Block: Features.

Object Model for Files and Documents

The following are the most important classes to use when you are working with files and documents in the server and client object models:

XML Used for Files and Documents

The File element in Collaborative Application Markup Language (CAML) is used within a Module element to provision websites with a site or application page. The File element can be used within a module in a site definition Configuration element to include the file in websites that are created through the site definition, or the File element can be used in the manifest file of a Feature. For more information about the two ways that you can use this element, see Modules. For an example that shows how to use the File element to provision websites with a file, see How to: Provision a File.

Building Block: Lists and Document Libraries

Building Block: Pages and User Interface

Building Block: Features

More Information about Files and Documents

How to: Provision a File

How to: Add Blocked File Types

How to: Upload a File to a SharePoint Site from a Local Folder

SharePoint Document Management