In this SharePoint Online pnp tutorial, We will discuss how to create a Modern SharePoint Team Site in SharePoint Online Office 365 Admin Center. We will see how to create modern team site SharePoint Online programmatically using PnP CSOM.
Also, we will discuss how we can read data from an Excel sheet programmatically using pnp csom.
If you are new to PnP SharePoint, check out an article on SharePoint Online Development using Patterns and Practices (PnP).
SharePoint Online tutorial contents:
I have an Excel spreadsheet which contains four columns as:
- SiteName: This helps to give a name to the new Modern SharePoint Team Site.
- SiteURL: This defines to provide an address URL to the new Modern SharePoint Team Site.
- SiteDescription: This assists to provide a description of the new Modern Team Site.
- SiteStorage: It helps to store the size of a new Modern Team Site.
You can see all the columns and its all values in below Excel spreadsheet screenshot:
By using this above Excel sheet, We need to create SharePoint Modern Team Site in SharePoint Online Office 365 Admin Center using pnp csom.
For this requirement, At first, we have to retrieve the Excel Sheet and then we have to write the code for creating SharePoint Modern Team Site. For that you can follow the below-tested csom code:
public async Task ReadDataFromExcelAsync()
{
try
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
string strSiteName,strURL,strDescription;
double intStorage;
int RowCount;
int TotalRow = 0;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(@"D:\SharePointSiteData", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
range = xlWorkSheet.UsedRange;
TotalRow = range.Rows.Count;
for (RowCount = 2; RowCount <= TotalRow; RowCount++)
{
strSiteName = (string)(range.Cells[RowCount, 1] as Excel.Range).Value2;
strURL = (string)(range.Cells[RowCount, 2] as Excel.Range).Value2;
strDescription = (string)(range.Cells[RowCount, 3] as Excel.Range).Value2;
intStorage = Convert.ToDouble((range.Cells[RowCount, 4] as Excel.Range).Value2);
lblMessage.Text = strSiteName + " " + strDescription + " " + strURL + " " + Convert.ToDouble(intStorage).ToString();
string siteUrl = "https://yourtenantname.sharepoint.com/";
string userName = "preeti@yourtenantname.onmicrosoft.com";
string passWord = "**********";
OfficeDevPnP.Core.AuthenticationManager authManager = new OfficeDevPnP.Core.AuthenticationManager();
ClientContext context = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, passWord);
bool aliasContext = await context.AliasExistsAsync(strURL);
if(aliasContext==false)
{
TeamSiteCollectionCreationInformation modernteamSiteInfo = new TeamSiteCollectionCreationInformation
{
DisplayName = strSiteName,
Description = strDescription,
Alias = strURL,
IsPublic = true,
};
var createModernSite = await context.CreateSiteAsync(modernteamSiteInfo);
lblCreateModernSite.Text = "Modern Team Sites created successfully";
}
lblCreateModernSite.Text = "This Modern Team Site is already exists";
}
xlWorkBook.Close(true, null, null);
xlApp.Quit();
}
catch (Exception ex)
{
lblMessage.Text = ex.StackTrace;
}
}
Here I have written this code in a button click, So the code will run when you will click on the button. Once you will execute this above csom code, you can see the output in the SharePoint Online Office 365 Admin Center.
The below screenshot represents all the new Modern SharePoint Team Site which are presenting in the SharePoint Online Office 365 Admin Center.
I have an Excel spreadsheet which contains three columns as:
- SiteName: This helps to give a name to the new SharePoint Subsite.
- SiteURL: This defines to provide an address URL to the new SharePoint Subsite.
- SiteDescription: This assists to provide a description of the new SharePoint Subsite.
You can see all the columns and its all values in below Excel spreadsheet screenshot:
By using this above Excel sheet, We need to create SharePoint Subsite using pnp csom programmatically.
For this requirement, At first, we have to retrieve the Excel Sheet and then we have to write the code for creating SharePoint Subsite. For that you can follow the below-tested csom code:
public void CreateSubSite()
{
try
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
string strSubsiteName, strSubsiteURL, strSubsiteDescription;
int RowCount;
int TotalRow = 0;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(@"D:\SharePointSubsiteData", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
range = xlWorkSheet.UsedRange;
TotalRow = range.Rows.Count;
for (RowCount = 2; RowCount <= TotalRow; RowCount++)
{
strSubsiteName = (string)(range.Cells[RowCount, 1] as Excel.Range).Value2;
strSubsiteURL = (string)(range.Cells[RowCount, 2] as Excel.Range).Value2;
strSubsiteDescription = (string)(range.Cells[RowCount, 3] as Excel.Range).Value2;
string siteUrl = "https://yourtenantname.sharepoint.com/sites/TSInfoteam";
string userName = "preeti@yourtenantname.onmicrosoft.com";
string passWord = "**********";
OfficeDevPnP.Core.AuthenticationManager authManager = new OfficeDevPnP.Core.AuthenticationManager();
ClientContext context = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, passWord);
Web subSiteWeb = context.Site.RootWeb;
if (subSiteWeb.WebExists(strSubsiteURL))
{
lblMessage.Text = "Subsite Already Exists";
}
else
{
Web web = context.Site.RootWeb.CreateWeb(new OfficeDevPnP.Core.Entities.SiteEntity()
{
Title = strSubsiteName,
Url = strSubsiteURL,
Description = strSubsiteDescription,
Template = "STS#0",
Lcid = 1033
},
true, true);
lblMessageCreateSubsite.Text = "Subsite created successfully";
}
}
}
catch (Exception ex)
{
lblMessageCreateSubsite.Text= ex.StackTrace;
}
}
Hence I have written this code in a button click, So the code will run when you will click on the button. Once you will execute this above csom code, You can see the output in the SiteContent of your existing URL.
Below screenshot represents all the new SharePoint Subsite which are presenting in the Site contents of existing Site URL(Which you are provided in the csom code).
I have an Excel spreadsheet which contains one column as:
- SiteURL: This defines to provide an address URL of SharePoint Subsite which you want to delete.
You can see the column and its values in below Excel spreadsheet screenshot:
By using this above Excel sheet, We need to delete SharePoint Subsite using pnp csom programmatically.
For this requirement, At first, we have to retrieve the Excel Sheet and then we have to write the code for deleting SharePoint Subsite. For that you can follow the below-tested csom code:
public void DeleteSubsite()
{
try
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
string strSubsiteURL;
int RowCount;
int TotalRow = 0;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(@"D:\SharePointSubsiteData", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
range = xlWorkSheet.UsedRange;
TotalRow = range.Rows.Count;
for (RowCount = 2; RowCount <= TotalRow; RowCount++)
{
strSubsiteURL = (string)(range.Cells[RowCount, 2] as Excel.Range).Value2;
string siteUrl = "https://yourtenantname.sharepoint.com/sites/TSInfoteam";
string userName = "preeti@yourtenantname.onmicrosoft.com";
string passWord = "**********";
OfficeDevPnP.Core.AuthenticationManager authManager = new OfficeDevPnP.Core.AuthenticationManager();
ClientContext context = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, passWord);
bool success = context.Web.DeleteWeb(strSubsiteURL);
if (success)
{
lblMessageDeleteSubsite.Text = "Subsite deleted successfully";
}
else
{
lblMessageDeleteSubsite.Text = "Problem in deleting the Subsite";
}
}
}
catch (Exception ex)
{
lblMessageDeleteSubsite.Text = ex.StackTrace;
}
}
Hence I have written this code in a button click, So the code will run when you will click on the button. Once you will execute this above csom code, You can see the output in the Site Content of your existing URL.
In the below screenshot, You can see there is no Subsite present in the Site contents of existing Site URL(Which you are provided in the above csom code).
You may like following PnP SharePoint tutorials:
- Create Choice Field in SharePoint Online Using PnP CSOM Library
- Rename or hide/remove SharePoint List Title Column Programmatically using PnP CSOM
- Delete All items from the SharePoint List using PnP core CSOM Library Programmatically
- Create Folder and Subfolder in SharePoint Document Library using PnP Core CSOM Library
- Create a task scheduler in windows 10 for SharePoint
Here in this SharePoint Online pnp tutorial, We discussed how to create a Modern SharePoint Team Site in SharePoint Online using PnP CSOM SharePoint. We saw how to create and delete the subsite using pnp csom programmatically in SharePoint Online.
We also saw, how we can read data from an Excel sheet programmatically using pnp csom.
Hello Everyone!! I am Bhawana a SharePoint MVP and having about 10+ years of SharePoint experience as well as in .Net technologies. I have worked in all the versions of SharePoint from wss to Office 365. I have good exposure in Customization and Migration using Nintex, Metalogix tools. Now exploring more in SharePoint 2016 🙂 Hope here I can contribute and share my knowledge to the fullest. As I believe “There is no wealth like knowledge and no poverty like ignorance”