Create a Document in SharePoint and add folder in SharePoint Document using C#
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…

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

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

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.

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

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

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

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:

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

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