Document libraries are lists that handle documents. Other than the attachments, which are stored in the containing web’s central attachment library, a document library stores its files directly. The programmatic access to libraries is similar to lists. In addition to list-related functions, you use streams, such as a MemoryStream, when saving or loading files.
Reading a File from the Shared Documents Library
Each site contains a library called Shared Documents. Code shown next explains how to retrieve a file as an SPFile object.
using (SPSite site = new SPSite("http://sharepointserve"))
{
using (SPWeb web = site.OpenWeb())
{
if (web.GetFolder("Shared Documents").Exists)
{
SPFolder folder = web.GetFolder("Shared Documents");
SPFile file = folder.Files["WordTemplate.docx"];
}
}
}
Adding a Folder to a Library
If you work with folders, your users might not be satisfied with the UI support. For greater capability, you can create a function that adds folders programmatically.
using (SPSite site = new SPSite("http://sharepointserve"))
{
using (SPWeb web = site.OpenWeb())
{
SPList lib = web.Lists["Shared Documents"];
SPFolder parent = lib.RootFolder;
SPFolder child = parent.SubFolders.Add("SubFolderName");
parent.Update();
}
}
This code adds a SubFolderName folder to the library’s root.
Saving Data to Libraries
To upload a file to a library, you use a similar approach (see Listing 4-21). The file must be available as a Stream or a byte array.
using (SPSite site = new SPSite("http://sharepointserve"))
{
using (SPWeb web = site.OpenWeb())
{
spweb.AllowUnsafeUpdates = true;
SPFolder folder = web.Folders[site.URL + "/Shared Documents/"];
byte[] content = null;
using (FileStream filestream = new FileStream(@"C:\Sample.docx",
System.IO.FileMode.Open))
{
content = new byte[(int) filestream.Length];
filestream.Read(content, 0, (int) filestream.Length);
filestream.Close();
}
SPFile file = folder.Files.Add("Sample.docx", content, true);
// do something usefil with file
}
}
You can add the file to a specific folder:
SPFile file = folder.SubFolders["SubFolder"].Files.Add("Sample.docx",
content,
true);
The content variable is the same as shown previously.
Subscribe to:
Post Comments (Atom)
Popular Posts
-
To see a site collection's hidden user information list append /_catalogs/users/simple.aspx onto the end of the site collection url. ...
-
Please Check the below complete code to bind SharePoint list items to a DataTable/ Gridview. In this post, we are using the list named ...
-
To bind the drop down list data with a field from the SharePoint list you can use the below code methods:- Method 1 if (!Page.IsP...
-
By Default SharePoint shows 3 events for any date in the calendar view, if we want to show all the events then we have to use any of the fol...
-
This Javascript function checks if the parameter supplied exists in the query string. If the parameter supplied is found in the query strin...
-
You can use CAML to do the following: Modify parameters to transport complex data Define the body of SOAP messages to transport data usin...
-
You can use below powershell command to change sharepoint user display name. Set-SPuser -identity "User name" -DisplayName ...
-
SharePoint solutions are either deployed globally or targeted to a particular web application. The decision of which is made automatically...
-
The sharepoint site is very slow when we access it first time. After that the response time is good. The reason is compilation and cachin...
-
The following Event Handler code will prevent users from creating duplicate value in "Title" field. ItemAdding Event Handler ...
No comments:
Post a Comment