如何使用C#中的CSOM在SharePoint团队网站中创建Home.aspx页面?
是否有任何方法可以使用C#中的CSOM在Sharepoint模板团队网站下创建Home.aspx主页?尝试以下方法:如何使用C#中的CSOM在SharePoint团队网站中创建Home.aspx页面?,c#,sharepoint,sharepoint-2013,sharepoint-online,csom,C#,Sharepoint,Sharepoint 2013,Sharepoint Online,Csom,是否有任何方法可以使用C#中的CSOM在Sharepoint模板团队网站下创建Home.aspx主页?尝试以下方法: OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager(); string siteUrl = "https://******.sharepoint.com/sites/CommunitySite";
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()
以下代码片段供您参考
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();
}
详情如下:
我们也可以使用下面的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
我只需要创建home.aspx页面,不需要创建站点。您可以使用o365 CLI创建站点,请参见上文。实际上,我不需要创建站点。我只需要使用CSMYA创建一个页面(现代页面),这很酷,但我需要使用CSOM而不是OfficeDevPnPy创建页面。您可以参考OfficeDevPnP的源代码。核心如下: