Cannot Create a Site using CSOM in the Sharepoint /sites folder

Richard Scannell 341 Reputation points
2022-10-26T14:01:33.14+00:00

I am trying to create sharepoint site using CSOM in the sites folder, so that I end up with https://XXXXXXXXXX.sharepoint.com/sites/MyShineyNewSite

I am using the following code

ClientContext ctx = getSharepointConnection("https://XXXXXXXXXX.sharepoint.com")
WebCreationInformation wi = new WebCreationInformation();
wi.Url = "MyShineyNewSite";
wi.Title ="Test Shiney Site";
Web newWeb = ctx.Web.Webs.Add(wi);
ctx.Load(newWeb);
ctx.ExecuteQuery();

When I use the values above, the code runs & I get a valid looking site at https://XXXXXXXXXX.sharepoint.com/MyShineyNewSite, but I don't want it there. I want it under the 'sites' folder

When I try to include 'sites' as part of the ctx definition(IE "https://XXXXXXXXXX.sharepoint.com/sites"), the getSharePointConnection** function errors with a 404 not found
When I try to include 'sites' as part of the wi.Url parameter, (IE "/sites/MyShineyNewSite") , the execute query fails with 'The 'parameters.Url' argument is invalid'

How do I get this to create the site in the correct location ?

**getSharePointConnection is peudocode function to make this code more readable on screen. It covers authenticating to SharePoint in CSOM and returning a loaded and executed Microsoft.SharePoint.Client.ClientContextobject

Thanks in advance

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,837 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,046 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rob Windsor 1,961 Reputation points
    2022-10-26T14:38:51.257+00:00

    The code you have creates a new SharePoint site as a subsite of the current website. The URL for the subsite is {parent site URL}/{subsite URL}. So, using the code you have above, the URL for the subsite will be https://xxxxxxxxxx.sharepoint.com/MyShineyNewSite. This URL structure is inherent to SharePoint and cannot be changed.

    You can use different code to create a new SharePoint site as the root of a new site collection. By default, the URL of the root site of a site collection is {tenant root site URL}/sites/{site collection root site URL}. In your case that could be https://xxxxxxxxxx.sharepoint.com/sites/MyShineyNewSite. Again, this URL structure is inherent to SharePoint and cannot be changed.

    This blog post covers how to create a site collection in SharePoint Online using the Client Object Model:

    How To Create/Provision Site Collection Programmatically Using Client Side Object Model (CSOM) PNP In SharePoint Office 365

    1 person found this answer helpful.
    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Richard Scannell 341 Reputation points
    2022-10-26T15:02:49.34+00:00

    Hi Rob , Many thanks for the quick response. The blog post shows the use of a tenant and sitecreationproperties classes. The sample suggests that its in Microsoft.Online.SharePoint.TenantAdministration , but I cant see that in Nuget . The documentation still refers to that namespace. Do you know if these have been moved ?

    Thanks again, Regards Richard


  2. Richard Scannell 341 Reputation points
    2022-10-26T15:34:40.867+00:00

    Thank you,

    0 comments No comments

  3. Richard Scannell 341 Reputation points
    2022-10-27T10:06:03.207+00:00

    Hi @Rob Windsor

    I have managed to use the logic in the blog post to create the site in the right location, but there are a couple of problems with it. It creates the site more slowly than the WebCreationInformation method, and it initially denies me permission , even though I am siteCreationProperties.Owner.

    The first method I tried ran through more quickly , and granted me access from the get-go. Is there any way to make that site be created under the /sites folder or to move it there from the root?

    Thanks again

    Regards Richard


  4. Richard Scannell 341 Reputation points
    2022-10-27T11:15:18.893+00:00

    Hi @Rob Windsor ,
    Thanks again. So does this mean that https://XXXXXXXXXX.sharepoint.com/sites/ is already a site collection, as that is where our sites are ?

    In which case , is there a way to navigate to it in CSOM to that so that I can use the WebCreationInformation approach to create the sub site there?

    Thanks again

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.