簡體   English   中英

如何使用 C# 中的 CSOM 在 SharePoint Team Site 中創建 Home.aspx 頁面?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM