Sunday, November 29, 2015

Creating a SharePoint OnLine Folder using CSOM

Surprisingly I can't find a complete solution to this on the blogosphere. So let me put that right.

First add two references to
Microsoft.SharePoint.Client.dll
Microsoft.SharePoint.Client.RunTime.dll

I found them on my 64-bit server located here
C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI

In your code add a using statement

using Microsoft.SharePoint.Client;

Then in your method add the following

using (ClientContext ctx = new ClientContext(site))
{var securePassword = new SecureString();
foreach (char c in password)
{
    securePassword.AppendChar(c);
}
 
ctx.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(username, securePassword);
var list = ctx.Web.Lists.GetByTitle(doclib);
var folderRoot = list.RootFolder;
ctx.Load(folderRoot);
ctx.ExecuteQuery();
folderRoot = folderRoot.Folders.Add(folder);
ctx.ExecuteQuery();
};
 

No comments: