简体   繁体   中英

Update Sharepoint wiki page's aspx using powershell

I have been trying to take a manual step out of a process where I clone a wiki page in sharepoint (done via powershell) and then manually go to the aspx in sharepoint designer and update the textpart's default value:

EX:

        <WpNs0:SPSlicerTextWebPart runat="server" MaximumCharacters="255" DefaultValue="REPLACETHISVALUE" RequireSelection="False" FilterMainControlWidthPixels="0" FilterName="Text Filter" Title="Text Filter" FrameType="BorderOnly" SuppressWebPartChrome="False" Description="Filters the contents of Web Parts by allowing users to enter a text value." ...

I'm using the following function for building the context for my copy, and I'm wondering if there is a way to pass the final ASPX site URL and manipulate the content:

function Get-SharepointContext
{ 
    Param(
        [Parameter(Mandatory=$true)]
        $siteUrl,
        [Parameter(Mandatory=$false)]
        $cred)

    If(!$cred){$cred = get-credential -UserName "$ENV:Username@$env:USERDNSDOMAIN" -Message "Login"}

    [string]$username = $cred.UserName
    $securePassword = $cred.Password

    [Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)    
    $ctx.RequestTimeOut = 1000 * 60 * 10;
    $ctx.AuthenticationMode =[Microsoft.SharePoint.Client.ClientAuthenticationMode]::Default
    $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
    $ctx.Credentials = $credentials
    $ctx.Load($ctx.Web)
    $ctx.Load($ctx.Site)
    $ctx.ExecuteQuery()
    Return $ctx
}

Has anyone attempted this before or know of how I can actually do this?

We can change the web part properties using PnP PowerShell or CSOM code.

PnP :

#Get Current Context Site (Root)  
$siteurl = "https://abc.sharepoint.com"  
Connect-SPOnline -Url $siteurl  
$ctx = Get-SPOContext 

#Get Web Part ID  
webpart = Get-SPOWebPart -ServerRelativePageUrl "/Pages/PnPPage.aspx" -Identity "Text Filter"  
$webpartId = $webpart.Id  
# Update WebPart  
Set-SPOWebPartProperty -ServerRelativePageUrl "/Pages/PnPPage.aspx" -Identity $webpartId -Key Height -Value 500 

CSOM :

function Change-WebPart {
    #variables that needs to be set before starting the script
    $siteURL = "https://spfire.sharepoint.com"
    $userName = "mpadmin@spfire.onmicrosoft.com"
    $webURL = "https://spfire.sharepoint.com"
    $relativePageUrl = "/SitePages/Home.aspx"

    # Let the user fill in their password in the PowerShell window
    $password = Read-Host "Please enter the password for $($userName)" -AsSecureString
    # set SharePoint Online credentials
    $SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $password)

    # Creating client context object
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($webURL)
    $context.credentials = $SPOCredentials

    #get Page file
    $page = $context.web.getFileByServerRelativeUrl($relativePageUrl)
    $context.load($page)

    #send the request containing all operations to the server
    try{
        $context.executeQuery()
    }
    catch{
        write-host "Error: $($_.Exception.Message)" -foregroundcolor red
    }

    #use the WebPartManger to load the webparts on a certain page
    $webPartManager = $page.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
    $context.load($webPartManager.webparts)

    #send the request containing all operations to the server
    try{
        $context.executeQuery()
    }
    catch{
        write-host "Error: $($_.Exception.Message)" -foregroundcolor red
    }

    #loop through all WebParts to get the correct one and change its property
    foreach($webPartDefinition in $webpartmanager.webparts){
        $context.Load($webPartDefinition.WebPart.Properties)

        #send the request containing all operations to the server
        try{
            $context.executeQuery()
        }
        catch{
            write-host "Error: $($_.Exception.Message)" -foregroundcolor red
        }

        #Only change the webpart with a certain title
        if ($webPartDefinition.WebPart.Properties.FieldValues.Title -eq "Documents")
        {
            $webPartDefinition.webpart.properties["Title"] = "My Documents"
            $webPartDefinition.SaveWebPartChanges()
        }
    }
}
Change-WebPart

Reference :

Update/Delete WebParts On SharePoint Pages Using PnP PowerShell

Editing Web Part properties with PowerShell CSOM in SharePoint

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM