articles

Home / DeveloperSection / Articles / Create a Document in SharePoint and add folder in SharePoint Document using C#

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

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

Chris Anderson22652 05-Dec-2011

Today, 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#:

The question is -

How to create a document library into the SharePoint,

You should 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 the 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 created successfully in the 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.


Updated 04-Mar-2020
hi I am software developer at mindstick software pvt. ltd.

Leave Comment

Comments

Liked By