Beruflich Dokumente
Kultur Dokumente
You can use the GetFolder method of the SPWeb class to return a specified folder and then access
individual files in the folder. After instantiating an SPWeb object (for example, as mySite), use
SPFolder myFolder = mySite.GetFolder("Shared Documents") (in Visual Basic 2005,
use Dim myFolder As SPFolder = mySite.GetFolder("Shared Documents")) to
return the Shared Documents folder for the site.
Example
The following example returns the collection of files in the folder and displays information about the files.
Visual Basic
Copy Code
myFiles
myFiles
}
In the example, the CopyTo method uses two parameters, one that specifies the destination URL for the
copied file, and the other a Boolean value that specifies whether to overwrite any file of the same name
that is located at the destination.
The previous example requires using directives (Imports in Visual Basic) for the
Microsoft.SharePoint, Microsoft.SharePoint.Utilities, and Microsoft.SharePoint.WebControls
namespaces.
The following example moves all files from the Shared Documents list of the current site to another
folder named StorageFolder, overwriting any file of the same name that may be located there.
Visual Basic
Copy Code
Dim
Dim
Dim
Dim
Procedures
To upload a file to a folder on a SharePoint site from a local folder
1. Create a Web application in Visual Studio 2005 as described in How to: Create a Web
Application in a SharePoint Web Site, adding a FormDigest control and a page directive for the
Microsoft.SharePoint.WebControls namespace to the .aspx file, as follows:
Copy Code
Add an HtmlInputFile control, a text box, and a button to the form on the .aspx page:
Copy Code
</form>
3.
In the .aspx.cs code-behind file, add using directives for the System.IO and
Microsoft.SharePoint namespaces, as follows:
Visual Basic
Copy Code
Imports System.IO
Imports Microsoft.SharePoint
C#
Copy Code
using System.IO;
using Microsoft.SharePoint;
4.
Add the following code to the Click event for the button:
Visual Basic
Copy Code
fStream.Close()
EnsureParentFolder(site, destUrl)
site.Files.Add(destUrl, contents)
C#
Copy Code
if (File1.PostedFile == null)
return;
string destUrl = TextBox1.Text;
SPWeb site = new SPSite(destUrl).OpenWeb();
Stream fStream = File1.PostedFile.InputStream;
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
EnsureParentFolder(site, destUrl);
site.Files.Add(destUrl, contents);
The value that you type in the text box for the destination must be an absolute URL, including
the file name, which is assigned to the destUrl parameter.
In addition to instantiating an SPWeb object for the parent site, the combined use of the SPSite
constructor and OpenWeb method validates the URL and throws an argument exception if the
URL is not served by the current Windows SharePoint Services deployment. A
System.Web.UI.HtmlControls.HtmlInputFile object is used to read the source file into a
byte array for use with the Add method of the SPFileCollection class.
5.
The EnsureParentFolder method ensures that the parent folder in the destination URL exists
in the specified site, and it returns the site-relative URL of the parent folder. The
EnsureParentFolder method accepts two parameters: an SPWeb object that represents the
parent site, and a string that contains the absolute URL that is passed from the UploadFile
method. If the parent folder does not exist, the EnsureParentFolder method creates it.
Visual Basic
Copy Code
parentFolderUrl.Split("/"c)
currentFolder =
currentFolder.SubFolders.Add(folder)
Next folder
End If
End If
Return parentFolderUrl
End Function 'EnsureParentFolder
C#
Copy Code
The GetFile method of the SPWeb class is used in combination with the Url property of the
SPFile class to convert the URL to a site-relative URL, throwing an exception if the specified URL
is not found within the scope of the site. The URL of the parent folder is calculated by using the
String.LastIndexOf method to determine the last appearance of a forward slash (/) within the
destination URL. If there is no slash (in other words, the index equals -1), then the destination
is the root folder for the site and the parentFolderUrl parameter returns an empty string.
Otherwise, the example uses the GetFolder method of the SPWeb class to return the
destination parent folder. If the folder does not exist, the example constructs the folder.
To upload a file from a local folder on the same server that is running Windows SharePoint Services, you
can use a System.IO.FileStream object instead. In this case, add a using directive for the System.IO
namespace, in addition to directives for System and Microsoft.SharePoint. The following example
uses the Click event handler to call an UploadFile method, which in turn calls the previously described
EnsureParentFolder method.
Visual Basic
Copy Code
EnsureParentFolder(site, destUrl);
site.Files.Add(destUrl, contents);
}
The UploadFile method accepts two parameters. The srcUrl parameter specifies the path of the source
location in the file system of the local computer, and the destUrl parameter specifies the absolute URL of
the destination. A System.IO.FileStream object is used to read the source file into a byte array for use
with the Add method of the SPFileCollection class.