0 Comments

 

If you want to create a folder in a document library, by using WebClient in C#, you can use the following code:

 

namespace Research
{
    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System.Net;

    [TestClass]
    public class RliResearch
    {
        [TestMethod]
        public void CreateSharePointFolderInDocumentLibrary()
        {
            string folderToCreateUri = "https://1.1.1.1/sites/mysite/Shared%20Documents/FolderIWantToCreate";
            using (var client = new WebClient())
            {
                client.Credentials = CredentialCache.DefaultCredentials;
                client.UploadString(folderToCreateUri, "MKCOL", "");
            }
        }
    }
}

Note: The folder you want to create the folder in, must exist. In other words: create subfolder hierarchy one at a time.

To create the folder hierarchy:

Shared Documents

     SubLevel1

             SubLevel2

 

namespace Research
{
    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System.Net;

    [TestClass]
    public class RliResearch
    {
        [TestMethod]
        public void CreateSharePointFolderInDocumentLibrary()
        {
            string sublevel1 = "https://1.1.1.1/sites/mysite/Shared%20Documents/SubLevel1";
            using (var client = new WebClient())
            {
                client.Credentials = CredentialCache.DefaultCredentials;
                client.UploadString(sublevel1, "MKCOL", "");
            }

            string sublevel2 = "https://1.1.1.1/sites/mysite/Shared%20Documents/SubLevel1/SubLevel2";
            using (var client = new WebClient())
            {
                client.Credentials = CredentialCache.DefaultCredentials;
                client.UploadString(sublevel2, "MKCOL", "");
            }
        }
    }
}

One Reply to “How to create a Microsoft SharePoint folder in a document library, by using WebClient in C#”

  1. how to put a validation for this “if the folder is exist then ignore, if not exist then create?”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts