Wednesday 27 July 2016

Creating Sub Site in SharePoint Online Through CSOM

Friends, here is quick code snippet to create sub site in SharePoint Online in CSOM from a Console Application.

Reference to Microsoft.SharePoint.Client.dll to be added.
Replace user name, password and site url with actuals from the code.
Code:



using Microsoft.SharePoint.Client;
using System;
using System.Security;

namespace TestConsoleApp 

                                                                  

class Program 


static void Main(string[] args)


string userName = "USERNAME";

string password = "PASSWORD";

string siteUrl = "SITEURL"

ClientContext clientContext = new ClientContext(siteUrl);

clientContext.Credentials = new SharePointOnlineCredentials(userName, GetPassword(password));

WebCreationInformation creation = new WebCreationInformation();

creation.Url = "NewWeb";

creation.Title = "NewWeb";

Web newWeb = clientContext.Web.Webs.Add(creation); 

clientContext.Load(newWeb, w => w.Title);

clientContext.ExecuteQuery();


private static SecureString GetPassword(string password)

 


if (password == null)

throw new ArgumentNullException("password");

var securePassword = new SecureString();

foreach (char c in password) 

securePassword.AppendChar(c);

securePassword.MakeReadOnly(); 

return securePassword; 

}

}     

}

 









 
 
 


 

No comments:

Post a Comment