1

I am trying to create sub site using CSOM. The parent site is having webtemplate as "GROUP". But I am receiving following error message.

Value does not fall within the expected range.

at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream) at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse() at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb) at Microsoft.SharePoint.Client.ClientRequest.ExecuteQuery()
at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQuery()
at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()

Here is my code for creating subsite

      ClientContext _context = null;
   SPWeb f_Web = null;


  _context = new ClientContext("https://Mysharepoint.sharepoint.com/sites/office365");
  _context.Credentials = new SharePointOnlineCredentials(m_Userid,
                                      m_SecurePassword);
  f_Web = _context.Web;

  _context.Load(f_Web, website => website.Webs);
  _context.ExecuteQuery();

  WebCreationInformation wcf= new WebCreationInformation();
  wcf.Description   = "This is subsite";
  wcf.Language      = 1033;
  wcf.Title         = "MySubSite";  
  wcf.Url           = "newsubsite";
  wcf.WebTemplate   = "STS#0";
  wcf.UseSamePermissionsAsParentSite = true;
   f_Web.Webs.Add(wcf);
   f_Web.Update();
  _context.ExecuteQuery();

Please suggest the solution. Thanks in advance.

  • Can you try GROUP#0 in wcf.WebTemplate = "GROUP#0"; – Nadeem Yousuf Feb 5 '18 at 5:38
  • @NadeemYousuf still same error – Mayuresh Feb 5 '18 at 5:49
  • 1
    how about wcf.WebTemplate = "STS#0"; ? you can't create a group subsite – Gautam Sheth Feb 5 '18 at 5:51
  • @GautamSheth : Its giving error as "Access denied. You do not have permission to perform this action or access this resource.". But I am able to create subsite manually – Mayuresh Feb 5 '18 at 6:07
  • ok, can you share the full code that you are using to create subsite ? Looks like the account you are using to create subsite programatically doesnt have adequate permission or you are not passing the credentials correctly – Gautam Sheth Feb 5 '18 at 6:15
0

Here is combination of CSOM and Powershell.

The First section is used to connect to the SharePoint Online Site that you wish to create the Subsite within, simply update the highlighted $Site variable with the relevant URL.

The second section creates a SubSite within the Site Collection. Update the highlighted variables to match your requirements, I've used the Team Site template (STS#0) in this example.

#Add references to SharePoint client assemblies and authenticate to Office 365 site
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$Username = Read-Host -Prompt "Please enter your username"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$Site = "https://site.sharepoint.com"
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($Site)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username,$Password)
$Context.Credentials = $Creds

#Create SubSite
$WCI = New-Object Microsoft.SharePoint.Client.WebCreationInformation
$WCI.WebTemplate = "STS#0"
$WCI.Description = "SubSite"
$WCI.Title = "SubSite"
$WCI.Url = "SubSite"
$WCI.Language = "1033"
$SubWeb = $Context.Web.Webs.Add($WCI)
$Context.ExecuteQuery()
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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