9月12日
Programming SharePoint Library File
In my previous blog, I have mentioned that Windows SharePoint Services 3.0 / Microsoft Office SharePoint Server 2007 is bundled with lists and libraries. A SharePoint library, as I have mentioned, is a location on a SharePoint site where we can create, collect, update, and manage files, including document library, picture library, and form library. On this post, I have tabulated the assemblies and components necessary for SharePoint library files programming.
In this piece, I will attempt to provide sample codes on the following:
1. Creating a subfolder in a SharePoint library (parent folder).
using (SPSite spsite = new SPSite("http://SPS01"))
{
using (SPWeb spweb = spsite.OpenWeb())
{
spweb.AllowUnsafeUpdates = true;
SPList splist = spweb.Lists["Shared Documents"];
SPListItem splistitem = splist.Items.Add(splist.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, "Demonstration Folder");
splistitem.Update();
}
}
2. Uploading file in a SharePoint library.
using (SPSite spsite = new SPSite("http://SPS01"))
{
using (SPWeb spweb = spsite.OpenWeb())
{
spweb.AllowUnsafeUpdates = true;
SPFolder spfolder = spweb.Folders[Site + "/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 spfile = spfolder.Files.Add("Sample.docx", content, true);
//Upload file in subfolder.
//SPFile spfile = spfolder.SubFolders["Demonstration Folder"].Files.Add("Sample.docx", content, true);
}
}
3. Deleting file from a SharePoint library.
using (SPSite spsite = new SPSite("http://SPS01"))
{
using (SPWeb spweb = spsite.OpenWeb())
{
spweb.AllowUnsafeUpdates = true;
SPFolder spfolder = spweb.Folders[Site + "/Shared Documents/"];
//spfolder.Files["Sample.docx"].Delete();
//Recycle file from parent folder.
//spfolder.Files["Sample.docx"].Recycle();
//Delete file from subfolder.
//spfolder.SubFolders["Demonstration Folder"].Files["Sample1.docx"].Delete();
spfolder.Update();
}
}
Thanks...