SPFile Class

Represents a file in a SharePoint Web site that can be a Web Part Page, an item in a document library, or a file in a folder.

Use the GetFile or GetFileAsString method of the SPWeb class to return a single file object. Otherwise, use the Files property of either the SPWeb or SPFolder class to return an SPFileCollection object that represents the collection of files for a site or folder.

This example adds a file from the document library of one site to the Shared Documents document library of another site and its subsites.

SPSite oSiteCollection = SPContext.Current.Site;

SPWeb oWebsiteSrc = oSiteCollection.AllWebs[“Source_Site_Name”];

SPWebCollection collWebsites =

oSiteCollection.AllWebs[“Destination_Site_Name”].Webs;

 

SPFile oFile = oWebsiteSrc.GetFile(“Source_Folder_Name/Source_File”);

string strFilename = oFile.Name;

byte[] binFile = oFile.OpenBinary();

 

foreach (SPWeb oWebsite in collWebsites)

{


if (oWebsite.GetFolder(“Shared Documents”).Exists)

{

SPFolder oFolder = oWebsite.GetFolder(“Shared Documents”);

oFolder.Files.Add(strFilename, binFile, true);

}

oWebsite.Dispose();

}

oWebsiteSrc.Dispose();

Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: