1

I want to create new site at root level (not subsite in existing site). I am using CSOM for creating site. In current scenario I need to provide the Site URL to client context for authentication and perform operations. Here is a pseudo code.

    string url = "https://mysharepoint.com/sites/testsite";

                    SecureString f_SecurePass = new SecureString();
                    foreach (char ch in pass)
                        f_SecurePass.AppendChar(ch);

                    clientcontext = new ClientContext(url);

                    var credentials = new SharePointOnlineCredentials(userid, f_SecurePass);                  

                    clientcontext.Credentials = credentials;
                    Web web = clientcontext.Web;

                    clientcontext.Load(web, website => website.Lists);
                    clientcontext.ExecuteQuery();

                WebCreationInformation wci = new WebCreationInformation();
                wci.Url = "/TestAPISite2";
                wci.Title = "TestAPISite2";
                wci.Language = 1033;

                var newsite =  clientcontext.Site.RootWeb.Webs.Add(wci);
                clientcontext.ExecuteQuery();

Please suggest the solution.

6
  • 1
    You mean you want to create site collection?
    – Asad Refai
    Dec 18, 2017 at 13:14
  • No. I want to create only a site
    – Mayuresh
    Dec 18, 2017 at 13:15
  • How do you differ between site and sub-site? Anything if its not a sub-site then its Site Collection.
    – Asad Refai
    Dec 18, 2017 at 13:19
  • Create the subsite from root site, so it's the "sibling" of the testsite.
    – YogaPanda
    Dec 18, 2017 at 14:22
  • what exactly is the issue you are facing?
    – Vamsi K K
    Dec 18, 2017 at 18:47

1 Answer 1

1

The code below for your reference:

string targetSiteURL = @"https://xxx.sharepoint.com/sites/dennis";

var login = "[email protected]";
var password = "***";

var securePassword = new SecureString();

foreach (char c in password)
{
    securePassword.AppendChar(c);
}
SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(login, securePassword);

ClientContext ctx = new ClientContext(targetSiteURL);
ctx.Credentials = onlineCredentials;

WebCreationInformation wci = new WebCreationInformation();
wci.Url = "TestAPISite2"; // This url is relative to the url provided in the context
wci.Title = "TestAPISite2";  
wci.UseSamePermissionsAsParentSite = true;
wci.WebTemplate = "STS#0";
wci.Language = 1033;

var newsite = ctx.Web.Webs.Add(wci);
ctx.ExecuteQuery();
Console.ReadKey();

If you want to create a site collection, the article below for your reference: http://www.vrdmn.com/2014/03/create-site-collections-with-csom-in.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.