使用powershell更新Sharepoint wiki页面的aspx

时间:2018-03-15 22:36:39

标签: powershell sharepoint

我一直试图从我在sharepoint中克隆一个wiki页面的过程中手动跳出一步(通过powershell完成),然后手动转到sharepoint设计器中的aspx并更新textpart的默认值:

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." ...

我正在使用以下函数为我的副本构建上下文,我想知道是否有办法传递最终的ASPX站点URL并操纵内容:

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
}

以前是否有人尝试过此操作或知道我该如何做到这一点?

1 个答案:

答案 0 :(得分:2)

我们可以使用PnP PowerShell或CSOM代码更改Web部件属性。

<强>即插即用

#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

<强>参考

Update/Delete WebParts On SharePoint Pages Using PnP PowerShell

Editing Web Part properties with PowerShell CSOM in SharePoint