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 Comment

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.