Programmatically creating folder in SharePoint Document library

No.of Views1919
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  16 Feb 2010 00:02:56
Tag : SharePoint , Development and Programming
In this article, I am going to create folder inside a document library using the SharePoint object model or in other words using .Net code.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Objective

In this article, I am going to create folder inside a document library using the SharePoint object model or in other words using .Net code.

Assumption

I do have a document library called “My Documents”. I am going to add folder this library. Currently the documents in library are as below.

Image Loading

Working Procedure

1. User will enter name of the folder to be created in the textbox.
2. Window form contains one textbox and one button.
3. Add the reference of Microsoft.SharePoint.dll in the window application.

Image Loading

Design

Image Loading

C# Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
using System.IO;

namespace FileUploadinSharePoint
{public partial class Form1 : Form
    {public Form1()
        {
            InitializeComponent();
        }private void Form1_Load(object sender, EventArgs e)
        {

        }private void btnFolderCreate_Click(object sender, EventArgs e)
        {

            SPSite _MySite = new SPSite("http://adfsaccount:2222/");
            SPWeb _MyWeb = _MySite.OpenWeb();

            SPDocumentLibrary _MyDocLibrary = (SPDocumentLibrary)_MyWeb.Lists["My Documents"];
            SPFolderCollection _MyFolders = _MyWeb.Folders;
            _MyFolders.Add("http://adfsaccount:2222/My%20Documents/" + txtUpload.Text + "/");
            _MyDocLibrary.Update();
            MessageBox.Show("Docuemnt Created");

        }
    }
}

Explanation

1. SPSite is returning the site collection. URL of the site collection is being passed to the constructor.
2. SPWeb is returning the top level site.
3. SPDocumentLibrary is returning document library as a list. A point here to be noted is that document library is basically a list in SharePoint. So I am returning document library as a list and typecasting that in SPDocumentLibrary
4. SPFolderCollection is returning the entire folder in the web.
5. I am adding the folder in the folder list.
6. Updating the document library.

Output

Image Loading

 

Image Loading

 

Image Loading

Conclusion

In this article, I have shown how to create a folder in SharePoint document library. Thanks for reading.Happy Coding

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top