Office 365 – CSOM – Issue with clientContext.CreateSiteAsync(CommunicationSiteCollectionCreationInformation) – method exited without site collection creation and resolution :)

Hi All,

Today, new issue and solution :). This time issue is with CreateSiteAsync () method of ClientContext class and solution 🙂

Background: In one of our Office 365 / SharePoint Online project we need to create around 300 communication sites. So we have prepared one CSV file with details like Title, URL, Owner and so on. We have written CSOM code as follows

//Code to read .CSV file and then looping for each item / site for creating site collection

//Sample code to create sites using CSOM

CommunicationSiteCollectionCreationInformation cs = new CommunicationSiteCollectionCreationInformation();

 cs.Title = "Title";
 cs.Url = "<URL>";
 
 cs.SiteDesign = CommunicationSiteDesign.Topic;

 ClientContext clientContext = new ClientContext("<SharePoint Tenant URL>");

string Password = "<Password>";
string UserName = "<UserName>";

SecureString passWord = new SecureString();
foreach (char c in Password.ToCharArray()) passWord.AppendChar(c);

clientContext.Credentials = new SharePointOnlineCredentials(UserName, passWord);
 
clientContext.CreateSiteAsync(cs);
clientContext.Load(site);
clientContext.ExecuteQuery();

Issue: But problem with above code is application get exit on the line – clientContext.CreateSiteAsync(cs); without any exception. Site is also not get created.

This code was working some days before and suddenly stop working. After bit googling we notice that ClientContext.CreateAsync() is in beta and can behave differently.

Solution: So no clue and then in one of the MS documentation found ClientContextExtensions class and then we tried CreateSiteAsync() method of this class as

try{
   var commResults =   ClientContextExtensions.CreateSiteAsync(clientContext, new OfficeDevPnP.Core.Sites.CommunicationSiteCollectionCreationInformation(){
   Url = "https://<MySharePointTenant>/sites/AvenueNewsandMedia",
   Title = "d1",
   Lcid = 1033
}).GetAwaiter().GetResult();
}catch(Exception ex){
  //exception handling goes here 
}

The above code worked like a charm 🙂 All our sites created successfully.

References:

ClientContextExtensions

Thanks for reading 🙂

Keep reading, share your thoughts, experiences. Feel free to contact us to discuss more.

If you have any suggestion / feedback / doubt, you are most welcome.

Stay tuned on Knowledge-Junction, will come up with more such articles.

Prasham Sabadra

LIFE IS VERY BEAUTIFUL :) ENJOY THE WHOLE JOURNEY :) Founder of Knowledge Junction and live-beautiful-life.com, Author, Learner, Passionate Techie, avid reader. Certified Professional Workshop Facilitator / Public Speaker. Scrum Foundation Professional certificated. Motivational, Behavioral , Technical speaker. Speaks in various events including SharePoint Saturdays, Boot camps, Collages / Schools, local chapter. Can reach me for Microsoft 365, Azure, DevOps, SharePoint, Teams, Power Platform, JavaScript.

You may also like...

Leave a Reply

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

Discover more from Microsoft 365

Subscribe now to keep reading and get access to the full archive.

Continue reading