This chapter shows the basic CRUD (Create, Read, Update, Delete) recipes to work with Site Collections and Webs, using the SharePoint Client Object Model (CSOM), PnP, and PowerShell. Extra information about security, configuration, etc., is also included.
All recipes use the login methods presented in Chapter 06, and the routine's code is not repeated in this chapter. Please review Chapter 06 for login code and configuration instruction.
|
All the recipes have been developed for, and tested with, Modern SharePoint Site Collections. Because almost all the APIs were developed originally to work with the Classic SharePoint user experience, the recipes will work generally without problems as well for the old experience of Site Collections and Webs.
The SharePoint Client Side Object Model (CSOM) is designed to work with SharePoint elements from the Site Collection level to the lowest architecture elements (Items and Documents). For this reason, the CSOM is not able to work at the tenant level, and it has no methods to, for example, create or enumerate Site Collections. To work with the highest rank of elements in the SharePoint hierarchy, it is necessary to use the Microsoft.Online.SharePoint namespace. The necessary assemblies to do that are also installed, together with the Microsoft.SharePoint.Client assemblies, when the NuGet Microsoft.SharePointOnline.CSOM is added to the Visual Studio Solution.
The following recipes will use both namespaces indiscriminately. When operations at tenant level are used, it is necessary to reference the administration Site of SharePoint Online, and use a SharePoint administrator account. The login routines are the same (as indicated in Chapter 06) for employing the administration site (http://domain-admin.sharepoint.com) or a normal Site Collection (http://domain.sharepoint.com/sites/sitecoll); the only difference is the URL to use.
Only SharePoint administrator accounts can create Site Collections in SharePoint Online. There are two types of Site Collections: based on the modern SharePoint user experience and based on the classic experience. How to create modern team sites programmatically depends on whether it needs to be connected to an Exchange Group or not.
For non-group connected sites, a call to a CSOM method for creating sites, and passing in the template identifier STS#3 (for a Team Site) or SITEPAGEPUBLISHING#0 (for a Communication Site) will suffice. For classic Site Collections, use any of the other template identifiers.
08.001 | ID | File | ||
Routines | LoginCsom (see Ch06-s6.1) | |||
NuGets | Microsoft.SharePointOnline.CSOM | |||
Ref. DLLs | ||||
Using | Microsoft.SharePoint.Client, Microsoft.Online.SharePoint.TenantAdministration, System.IO |
static void SpCsCsomCreateOneSiteCollection(ClientContext spAdminCtx) { Tenant myTenant = new Tenant(spAdminCtx); string myUser = ConfigurationManager.AppSettings["spUserName"]; SiteCreationProperties mySiteCreationProps = new SiteCreationProperties { Url = ConfigurationManager.AppSettings["spBaseUrl"] + "/sites/NewSiteCollectionModernCsCsom01", Title = "NewSiteCollectionModernCsCsom01", Owner = ConfigurationManager.AppSettings["spUserName"], Template = "STS#3", StorageMaximumLevel = 100, UserCodeMaximumLevel = 50 }; SpoOperation myOps = myTenant.CreateSite(mySiteCreationProps); spAdminCtx.Load(myOps, ic => ic.IsComplete); spAdminCtx.ExecuteQuery(); while (myOps.IsComplete == false) { System.Threading.Thread.Sleep(5000); myOps.RefreshLoad(); spAdminCtx.ExecuteQuery(); } }
For a Group connected modern site, create an Office 365 group first, and determine the name of the team site to connect to, as shown in the next routine.
08.002 | ID | File | ||
Routines | LoginCsom (see Ch06-s6.1) | |||
NuGets | Microsoft.SharePointOnline.CSOM | |||
Ref. DLLs | ||||
Using | Microsoft.SharePoint.Client, Microsoft.Online.SharePoint.TenantAdministration, System.IO |
static void SpCsCsomCreateGroupForSite(ClientContext spAdminCtx) { string[] myOwners = new string[] { "user@domain.onmicrosoft.com" }; GroupCreationParams myGroupParams = new GroupCreationParams(spAdminCtx); myGroupParams.Owners = myOwners; //GroupCreationParams Tenant myTenant = new Tenant(spAdminCtx); myTenant.CreateGroupForSite( ConfigurationManager.AppSettings["spBaseUrl"] + "/sites/NewSiteCollectionModernCsCsom01", "GroupForNewSiteCollectionModernCsCsom01", "GroupForNewSiteCollAlias", true, myGroupParams); spAdminCtx.ExecuteQuery(); }
To find the identifiers for the different types of Site Collections, use the GetSPOTenantWebTemplates method, indicating the language location identifier.
08.003 | ID | File | ||
Routines | LoginCsom (see Ch06-s6.1) | |||
NuGets | Microsoft.SharePointOnline.CSOM | |||
Ref. DLLs | ||||
Using | Microsoft.SharePoint.Client, Microsoft.Online.SharePoint.TenantAdministration, System.IO |
static void SpCsCsomFindWebTemplates(ClientContext spAdminCtx) { Tenant myTenant = new Tenant(spAdminCtx); SPOTenantWebTemplateCollection myTemplates = myTenant.GetSPOTenantWebTemplates(1033, 0); spAdminCtx.Load(myTemplates); spAdminCtx.ExecuteQuery(); foreach (SPOTenantWebTemplate oneTemplate in myTemplates) { Console.WriteLine(oneTemplate.Name + " - " + oneTemplate.Title); } }
There are no methods at the moment to enumerate modern Site Collections in SharePoint Online. To get the classic Site Collections in the tenant, use the GetSiteProperties method.
08.004 | ID | File | ||
Routines | LoginCsom (see Ch06-s6.1) | |||
NuGets | Microsoft.SharePointOnline.CSOM | |||
Ref. DLLs | ||||
Using | Microsoft.SharePoint.Client, Microsoft.Online.SharePoint.TenantAdministration, System.IO |
static void SpCsCsomReadAllSiteCollections(ClientContext spAdminCtx) { Tenant myTenant = new Tenant(spAdminCtx); myTenant.GetSiteProperties(0, true); SPOSitePropertiesEnumerable myProps = myTenant.GetSiteProperties(0, true); spAdminCtx.Load(myProps); spAdminCtx.ExecuteQuery(); foreach (var oneSiteColl in myProps) { Console.WriteLine(oneSiteColl.Title + " - " + oneSiteColl.Url); } }
The RemoveSite method deletes a Site Collection from the tenant if it has no connection to an Exchange group.
08.005 | ID | File | ||
Routines | LoginCsom (see Ch06-s6.1) | |||
NuGets | Microsoft.SharePointOnline.CSOM | |||
Ref. DLLs | ||||
Using | Microsoft.SharePoint.Client, Microsoft.Online.SharePoint.TenantAdministration, System.IO |
static void SpCsCsomRemoveSiteCollection(ClientContext spAdminCtx) { Tenant myTenant = new Tenant(spAdminCtx); myTenant.RemoveSite( ConfigurationManager.AppSettings["spBaseUrl"] + "/sites/NewSiteCollectionModernCsCsom01"); spAdminCtx.ExecuteQuery(); }
To recover a Site Collection that has been deleted to the Recycle Bin, use the RestoreDeletedSite method. Take into consideration that the Recycle Bin removes its information automatically after some time.
08.006 | ID | File | ||
Routines | LoginCsom (see Ch06-s6.1) | |||
NuGets | Microsoft.SharePointOnline.CSOM | |||
Ref. DLLs | ||||
Using | Microsoft.SharePoint.Client, Microsoft.Online.SharePoint.TenantAdministration, System.IO |
static void SpCsCsomRestoreSiteCollection(ClientContext spAdminCtx) { Tenant myTenant = new Tenant(spAdminCtx); myTenant.RestoreDeletedSite( ConfigurationManager.AppSettings["spBaseUrl"] + "/sites/NewSiteCollectionModernCsCsom01"); spAdminCtx.ExecuteQuery(); }
A Site Collection can be also deleted from the Recycle Bin using the RemoveDeletedSite method. That could be necessary to create a new Site Collection with the same name as an already deleted Site Collection.
08.007 | ID | File | ||
Routines | LoginCsom (see Ch06-s6.1) | |||
NuGets | Microsoft.SharePointOnline.CSOM | |||
Ref. DLLs | ||||
Using | Microsoft.SharePoint.Client, Microsoft.Online.SharePoint.TenantAdministration, System.IO |
static void SpCsCsomRemoveDeletedSiteCollection(ClientContext spAdminCtx) { Tenant myTenant = new Tenant(spAdminCtx); myTenant.RemoveDeletedSite( ConfigurationManager.AppSettings["spBaseUrl"] + "/sites/NewSiteCollectionModernCsCsom01"); spAdminCtx.ExecuteQuery(); }
The isSiteAdministrator parameter (last parameter) in the SetSiteAdmin method indicates if a newly added account to the security settings of the Site Collection is an administrator.
08.008 | ID | File | ||
Routines | LoginCsom (see Ch06-s6.1) | |||
NuGets | Microsoft.SharePointOnline.CSOM | |||
Ref. DLLs | ||||
Using | Microsoft.SharePoint.Client, Microsoft.Online.SharePoint.TenantAdministration, System.IO |
static void SpCsCsomSetAdministratorSiteCollection(ClientContext spAdminCtx) { Tenant myTenant = new Tenant(spAdminCtx); myTenant.SetSiteAdmin( ConfigurationManager.AppSettings["spBaseUrl"] + "/sites/NewSiteCollectionModernCsCsom01", "user@domain.onmicrosoft.com", true); spAdminCtx.ExecuteQuery(); }
A modern Hub Site Collection is a logical aggregator of Site Collections. Any modern Teams Site Collection can be elevated to Hub Site Collection.
08.009 | ID | File | ||
Routines | LoginCsom (see Ch06-s6.1) | |||
NuGets | Microsoft.SharePointOnline.CSOM | |||
Ref. DLLs | ||||
Using | Microsoft.SharePoint.Client, Microsoft.Online.SharePoint.TenantAdministration, System.IO |
static void SpCsCsomRegisterAsHubSiteCollection(ClientContext spAdminCtx) { Tenant myTenant = new Tenant(spAdminCtx); myTenant.RegisterHubSite( ConfigurationManager.AppSettings["spBaseUrl"] + "/sites/NewHubSiteCollCsCsom"); spAdminCtx.ExecuteQuery(); }
In a similar way, a Site Collection can be demoted from Hub Site Collection back to normal modern Site Collection.
08.010 | ID | File | ||
Routines | LoginCsom (see Ch06-s6.1) | |||
NuGets | Microsoft.SharePointOnline.CSOM | |||
Ref. DLLs | ||||
Using | Microsoft.SharePoint.Client, Microsoft.Online.SharePoint.TenantAdministration, System.IO |
static void SpCsCsomUnregisterAsHubSiteCollection(ClientContext spAdminCtx) { Tenant myTenant = new Tenant(spAdminCtx); myTenant.UnregisterHubSite( ConfigurationManager.AppSettings["spBaseUrl"] + "/sites/NewHubSiteCollCsCsom"); spAdminCtx.ExecuteQuery(); }
The GetHubSitePropertiesByUrl method gets the current information configured for a Hub Site Collection. There is also a GetHubSitePropertiesById to recover the Hub information given its identifier.
08.011 | ID | File | ||
Routines | LoginCsom (see Ch06-s6.1) | |||
NuGets | Microsoft.SharePointOnline.CSOM | |||
Ref. DLLs | ||||
Using | Microsoft.SharePoint.Client, Microsoft.Online.SharePoint.TenantAdministration, System.IO |
static void SpCsCsomGetHubSiteCollectionProperties(ClientContext spAdminCtx) { Tenant myTenant = new Tenant(spAdminCtx); HubSiteProperties myProps = myTenant.GetHubSitePropertiesByUrl( ConfigurationManager.AppSettings["spBaseUrl"] + "/sites/NewHubSiteCollCsCsom"); spAdminCtx.Load(myProps); spAdminCtx.ExecuteQuery(); Console.WriteLine(myProps.Title); }
And the same method can be used to update the metadata of the Hub.
08.012 | ID | File | ||
Routines | LoginCsom (see Ch06-s6.1) | |||
NuGets | Microsoft.SharePointOnline.CSOM | |||
Ref. DLLs | ||||
Using | Microsoft.SharePoint.Client, Microsoft.Online.SharePoint.TenantAdministration, System.IO |
static void SpCsCsomUpdateHubSiteCollectionProperties(ClientContext spAdminCtx) { Tenant myTenant = new Tenant(spAdminCtx); HubSiteProperties myProps = myTenant.GetHubSitePropertiesByUrl( ConfigurationManager.AppSettings["spBaseUrl"] + "/sites/NewHubSiteCollCsCsom"); spAdminCtx.Load(myProps); spAdminCtx.ExecuteQuery(); myProps.Title = myProps.Title + "_Updated"; myProps.Update(); spAdminCtx.Load(myProps); spAdminCtx.ExecuteQuery(); Console.WriteLine(myProps.Title); }
A normal classic Teams Site Collection can be added to the collection of Site Collections managed by a Hub Site Collection.
08.013 | ID | File | ||
Routines | LoginCsom (see Ch06-s6.1) | |||
NuGets | Microsoft.SharePointOnline.CSOM | |||
Ref. DLLs | ||||
Using | Microsoft.SharePoint.Client, Microsoft.Online.SharePoint.TenantAdministration, System.IO |
static void SpCsCsomAddSiteToHubSiteCollection(ClientContext spAdminCtx) { Tenant myTenant = new Tenant(spAdminCtx); myTenant.ConnectSiteToHubSite( ConfigurationManager.AppSettings["spBaseUrl"] + "/sites/NewSiteForHub", ConfigurationManager.AppSettings["spBaseUrl"] + "/sites/NewHubSiteCollCsCsom"); spAdminCtx.ExecuteQuery(); }
Any Site Collection that is in the collection of sites managed by a Hub can also be removed from the Hub.
08.014 | ID | File | ||
Routines | LoginCsom (see Ch06-s6.1) | |||
NuGets | Microsoft.SharePointOnline.CSOM | |||
Ref. DLLs | ||||
Using | Microsoft.SharePoint.Client, Microsoft.Online.SharePoint.TenantAdministration, System.IO |
static void SpCsCsomremoveSiteFromHubSiteCollection(ClientContext spAdminCtx) { Tenant myTenant = new Tenant(spAdminCtx); myTenant.DisconnectSiteFromHubSite( ConfigurationManager.AppSettings["spBaseUrl"] + "/sites/NewSiteForHub"); spAdminCtx.ExecuteQuery(); }
Use the WebCreationInformation method to configure the parameters of a new Web Site and add it to the Webs collection of the Site Collection. This recipe can create modern and classic experience Webs.
08.015 | ID | File | ||
Routines | LoginCsom (see Ch06-s6.1) | |||
NuGets | Microsoft.SharePointOnline.CSOM | |||
Ref. DLLs | ||||
Using | Microsoft.SharePoint.Client, Microsoft.Online.SharePoint.TenantAdministration, System.IO |
static void SpCsCsomCreateOneWebInSiteCollection(ClientContext spCtx) { Site mySite = spCtx.Site; WebCreationInformation myWebCreationInfo = new WebCreationInformation { Url = "NewWebSiteModernCsCsom", Title = "NewWebSiteModernCsCsom", Description = "NewWebSiteModernCsCsom Description", UseSamePermissionsAsParentSite = true, WebTemplate = "STS#3", Language = 1033 }; Web myWeb = mySite.RootWeb.Webs.Add(myWebCreationInfo); spCtx.ExecuteQuery(); }
To enumerate all the Webs in a Site Collection, recall first the Site object, and then loop through each Web in the Webs collection.
08.016 | ID | File | ||
Routines | LoginCsom (see Ch06-s6.1) | |||
NuGets | Microsoft.SharePointOnline.CSOM | |||
Ref. DLLs | ||||
Using | Microsoft.SharePoint.Client, Microsoft.Online.SharePoint.TenantAdministration, System.IO |
static void SpCsCsomGetWebsInSiteCollection(ClientContext spCtx) { Site mySite = spCtx.Site; WebCollection myWebs = mySite.RootWeb.Webs; spCtx.Load(myWebs); spCtx.ExecuteQuery(); foreach (Web oneWeb in myWebs) { Console.WriteLine(oneWeb.Title + " - " + oneWeb.Url + " - " + oneWeb.Id); } }
To find only one of the Webs in the Site Collection, create a context using the URL of the Web directly. Then, all its properties can be read.
08.017 | ID | File | ||
Routines | LoginCsom (see Ch06-s6.1) | |||
NuGets | Microsoft.SharePointOnline.CSOM | |||
Ref. DLLs | ||||
Using | Microsoft.SharePoint.Client, Microsoft.Online.SharePoint.TenantAdministration, System.IO |
static void SpCsCsomGetOneWebInSiteCollection() { string myWebFullUrl = ConfigurationManager.AppSettings["spUrl"] + "/NewWebSiteModernCsCsom"; ClientContext spCtx = LoginCsom(myWebFullUrl); Web myWeb = spCtx.Web; spCtx.Load(myWeb); spCtx.ExecuteQuery(); Console.WriteLine(myWeb.Title + " - " + myWeb.Url + " - " + myWeb.Id); }