[英]How to create Home.aspx page in SharePoint Team Site using CSOM in C#?
有什么方法可以使用 C# 中的 CSOM 在模板 Team Site 下的 Sharepoint 中创建主页 Home.aspx 吗?
以下代码片段供您参考。
string siteUrl = "https://*****.sharepoint.com/sites/team";
string userName = "admin@****.onmicrosoft.com";
string password = "***";
OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
#region O365
using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
{
Web web = ctx.Web;
ctx.Load(web.Lists);
ctx.Load(web, w => w.SupportedUILanguageIds);
ctx.Load(web);
ctx.ExecuteQueryRetry();
//Create the Page
var homePage = ctx.Web.AddClientSidePage("HomePage.aspx", true);
homePage.AddSection(CanvasSectionTemplate.ThreeColumn, 5);
homePage.Save();
}
更多信息在这里: 如何使用模式和实践在 SharePoint Office 365 中以编程方式创建现代页面 (OfficeDevPNP)
我们也可以使用下面的 PnP PowerShell 来实现。
$siteUrl="https://tenant.sharepoint.com/sites/team";
$cred = Get-Credential
Connect-PnPOnline -Url $siteUrl -Credential $cred
Add-PnPClientSidePage -Name "HomePage"
Set-PnPHomePage -RootFolderRelativeUrl SitePages/HomePage.aspx
试试这个方法:
OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
string siteUrl = "https://******.sharepoint.com/sites/CommunitySite";
string userName = "userh@******.onmicrosoft.com";
string password = "******";
using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
{
Web web = ctx.Web;
ctx.Load(web);
ctx.ExecuteQueryRetry();
List sitePagesList = web.Lists.GetByTitle("Site Pages");
ctx.Load(sitePagesList);
ctx.Load(sitePagesList.RootFolder);
ctx.ExecuteQueryRetry();
var pageTitle = "home.aspx";
sitePagesList.RootFolder.Files.AddTemplateFile(sitePagesList.RootFolder.ServerRelativeUrl + "/" + pageTitle, TemplateFileType.StandardPage);
ctx.ExecuteQueryRetry();
}
}
在现代和经典设计之间切换:
#Get the Web Scoped Feature
$web = $context.Web
# Feature Title : EnableDefaultListLibrarExperience, Scope : Web
$featureguid = new-object System.Guid "52E14B6F-B1BB-4969-B89B-C4FAA56745EF"
#To Enable/Disable the Modern UI, comment and uncomment the below two lines, either to Add or Remove
#$web.Features.Add($featureguid, $true, [Microsoft.SharePoint.Client.FeatureDefinitionScope]::None)
$web.Features.Remove($featureguid, $true)
$context.ExecuteQuery()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.