4

Is there a possibility to remove the following two sentences from the SharePoint publishing site home page.

Welcome to Your Publishing Site These links will help you get started.

I'm currently adding new web parts to the page using csom while removing every webpart in the homepage using the below code. But it does not remove the above sentences.

            using (clientContext)
        {
            Microsoft.SharePoint.Client.File page = clientContext.Web.GetFileByServerRelativeUrl(pageUrl);
            clientContext.Load(page);
            clientContext.ExecuteQuery();

            if (page.CheckOutType == CheckOutType.None)
            {
                page.CheckOut();
            }
            var wpm = page.GetLimitedWebPartManager(PersonalizationScope.Shared);
            clientContext.Load(wpm.WebParts);
            clientContext.ExecuteQuery();

            if (wpm.WebParts.Count != 0)
            {
                foreach (WebPartDefinition webpart in wpm.WebParts)
                {
                    webpart.DeleteWebPart();
                    clientContext.ExecuteQuery();
                }
            }

            var importedWebPart = wpm.ImportWebPart(webPartSchemaXml);
            var webPart = wpm.AddWebPart(importedWebPart.WebPart, zoneid, zoneIndex.ToInt32());
            page.CheckIn("", CheckinType.MajorCheckIn);

            clientContext.ExecuteQuery();

        }
1

I would recommend a best practice would be to create your own Home Page Layout rather than customizing the SharePoint's OOB that too with code.

Once you create your own layout, then just apply on to your homepage.

This PowerShell function can help you in replacing the page layout.

function ChangePageLayout()
{
    param(
        [Parameter(Mandatory=$true)][string]$siteurl,
        [Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials,
        [Parameter(Mandatory=$false)][string]$PageName,
        [Parameter(Mandatory=$false)][string]$PageLayoutName,
        [Parameter(Mandatory=$false)][string]$PageLayoutDisplayName,
        [Parameter(Mandatory=$false)][string]$Title,
        [Parameter(Mandatory=$false)][bool]$isCustomPageLayout
    )
    try
    {

        $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteurl)
        $ctx.Credentials = $credentials

        if($isCustomPageLayout -eq $false)
        {
            $PageLayoutName = "/_catalogs/masterpage/" + $PageLayoutName + "," + $PageLayoutDisplayName
        }
        else
        {
            #Here I have assumed that if its custom page layout, then it's placed inside some folder which is child to masterpage
            #If that's not the case with you then you can use below line of code
            #$PageLayoutName = "/_catalogs/masterpage/" + $PageLayoutName + ", " + $PageLayoutDisplayName
            #
            $PageLayoutName = "/_catalogs/masterpage/Custom Page Layouts/" + $PageLayoutName + ", " + $PageLayoutDisplayName
        }

        $Pages = $ctx.Web.Lists.GetByTitle('Pages')
        $camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
        $camlQuery.ViewXml = '<View><Query><Where><Eq><FieldRef Name="FileLeafRef" /><Value Type="Text">'+ $PageName +'</Value></Eq></Where></Query></View>'
        $Page = $Pages.GetItems($camlQuery)
        $ctx.Load($Page)
        $ctx.ExecuteQuery()

        $file = $Page.File

        $ctx.Load($file)
        $ctx.ExecuteQuery()

        if ($file.CheckOutType  -ne [Microsoft.SharePoint.Client.CheckOutType]::None) {
            $file.UndoCheckOut()
            $ctx.Load($file)
            $ctx.ExecuteQuery() 
        }

        $file.CheckOut()
        $ctx.Load($file)
        $ctx.ExecuteQuery()

        $Page.Set_Item("PublishingPageLayout", $PageLayoutName)
        $Page.Set_Item("Title", $Title)
        $Page.Update()
        $Page.File.CheckIn("", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
        $Page.File.Publish("")

        #check for approval
        $ctx.Load($Pages)
        $ctx.ExecuteQuery()

        if ($Pages.EnableModeration -eq $true) {
            $Page.File.Approve("")
        }

        $ctx.Load($Page)
        $ctx.ExecuteQuery()
        Write-Host "Update Page Layout Complete"
        Write-Host ""

    }
    catch
    {
        Write-Host ("Error while changing page layout. Error -->> " + $_.Exception.Message) -ForegroundColor Red
    }
}

Usage:

$credentials = Get-Credential
ChangePageLayout "http:yoursite.com" $credentials "default.aspx" "YourCustomLayout.aspx" "Your Custom Page Layout" "Some Title" $true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.