Elczar Adame's Shared Points on SharePoint


 

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…

Advertisements

Comments on: "Programming SharePoint Library File" (2)

  1. Andres M Herrera said:

    Hi,
    very useful your code sniped, but I need add a file whit specific content type and assign content type properties. Do you have some example about this?.

  2. This’s helpful for me. Thanks for sharing!

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

Tag Cloud

%d bloggers like this: