How can we create a site and then create a list in Sharepoint 2013 console app using CSOM. How to set the wait so we know that much time it takes to create a site and then create a list programmatically?
1 Answer
Solution 1
Would you require to create the same list each time a site is created? If yes, you could create the list in the site and save the site as template. Then use that template in your CSOM code to create the site from.
Solution 2
In CSOM, there is no callback that determines the SharePoint has finished creating the site. So the only possible way is to use polling. Create the site in the CSOM and after the method call ExecuteQuery
, call another method that would continuously poll the new web object until it finds it. Example CSOM polling code below.
bool IsWebCreated(string webUrl)
{
ClientContext clientContext = new ClientContext(webUrl);
Web web = clientContext.Web;
clientContext.Load(web);
for(int i=0 ; i<50 ; i++)//not safe to do infinite polling. Can increase from 50 to 100
{
try
{
clientContext.ExecuteQuery();
return true;
}
catch{}
System.Threading.Thread.Sleep(1000);//1 sec delay
}
return false;
}