In this article I am going to explain or show you how to create a Document Library in SharePoint Server and how to add a folder in the Document Library by using C#:

To create a document library, follow these steps:

1)    Open a browser and log into Sharepoint.

2)    Click Site Actions and select More Options…

Create a Document in SharePoint and add folder in SharePoint Document using C#

3)    Under library, click Document Library and enter the document name as Personal Documents and click Create to proceed.

Create a Document in SharePoint and add folder in SharePoint Document using C#

You can see that your Personal Document (Document Library) is create successfully in browser.

Create a Document in SharePoint and add folder in SharePoint Document using C#

In the next step, we have to create a folder in a Document Library using C# application.

1)    Open Visual Studio and create a Console Application.

Create a Document in SharePoint and add folder in SharePoint Document using C#

2)    Browse Microsoft.SharePoint.dll and it to the References.

Create a Document in SharePoint and add folder in SharePoint Document using C#

3)    In the next step you have to change the properties of an application.

Create a Document in SharePoint and add folder in SharePoint Document using C#

4)    In the build option change the platform target into 64 bit (x64).

Create a Document in SharePoint and add folder in SharePoint Document using C#

1)     Then write the below code in the Program.cs file.

using System;
using System.Text;
using Microsoft.SharePoint;

namespace CreateFolderSPDoc
{
    class Program
    {
        static void Main(string[] args)
        {
            SPSite _MySite = new SPSite("http://rohit:34143/");
            SPWeb _MyWeb = _MySite.OpenWeb();
            SPDocumentLibrary _MyDocLibrary = (SPDocumentLibrary)_MyWeb.Lists["Personal                                                                            Documents"];
            SPFolderCollection _MyFolders = _MyWeb.Folders;
            string folderName = "rohitdoc";
            _MyFolders.Add("http://rohit:34143/Personal%20Documents/" + folderName +
                _MyDocLibrary.Update();                                                                    "/");                        Console.WriteLine("Docuemnt Created");
        }

    }

}

Output:

Create a Document in SharePoint and add folder in SharePoint Document using C#

After create a document you can see your folder (rohitdoc) is successfully added in the Document library (Personal Documents).

Create a Document in SharePoint and add folder in SharePoint Document using C#

Thanks for reading this article. I think this will help you a lot.

  Modified On Jan-17-2018 05:38:16 AM

Leave Comment