Elczar 的个人资料Sharing Points { }照片日志列表 工具 帮助

日志


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...

评论

请稍候...
很抱歉,您输入的评论太长。请缩短您的评论。
您没有输入任何内容,请重试。
很抱歉,我们当前无法添加您的评论。请稍后重试。
若要添加评论,需要您的家长授予您相应权限。请求权限
您的家长禁用了评论功能。
很抱歉,我们当前无法删除您的评论。请稍后重试。
您已超过了一天之内允许提供的评论数上限。请在 24 小时后重试。
因为我们的系统表明您可能在向其他用户提供垃圾评论,您的帐户已禁用了评论功能。如果您认为我们错误地禁用了您的帐户,请联系 Windows Live 支持部门
完成下面的安全检查,您提供评论的过程才能完成。
您在安全检查中键入的字符必须与图片或音频中的字符一致。
AdameElcza​r 在此页禁用了评论功能。

引用通告

此日志的引用通告 URL 是:
http://elczara.spaces.live.com/blog/cns!554EC06D366AC9D5!692.trak
引用此项的网络日志