Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Get-SPOContext([string]$Url, [string]$UserName, [string]$Password) {
- $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force;
- $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url);
- $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword);
- return $context;
- }
- function Get-ListItems([Microsoft.SharePoint.Client.ClientContext]$Context, [String]$ListTitle) {
- $list = $Context.Web.Lists.GetByTitle($listTitle);
- $qry = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery();
- $items = $list.GetItems($qry);
- $Context.Load($items);
- $Context.ExecuteQuery();
- return $items;
- }
- try {
- $UserName = "XXXXXXXX"
- #$Password = Read-Host -Prompt "Enter the password"
- $Password = "XXXXXXXX"
- $Url = "https://abc.def.com/sites/subsite1/"
- $context = Get-SPOContext -Url $Url -UserName $UserName -Password $Password
- $items = Get-ListItems -Context $context -ListTitle "Pages"
- Write-Host "Total Pages Count " $items.Count -BackgroundColor Cyan;
- foreach ($item in $items) {
- $contentType = $item.ContentType;
- $context.Load($contentType);
- $Context.ExecuteQuery();
- if ($contentType.Name -eq "Custom Page Layout") {
- $pageFieldValues = $item.FieldValues;
- #text field updation
- $item["Comments"] = "New Comment 1";
- $item.Update();
- $Context.ExecuteQuery();
- #print the values
- foreach ($h in $pageFieldValues.GetEnumerator()) {
- Write-Host "$($h.Key)" -ForegroundColor Red;
- Write-Host "$($h.Value)" -ForegroundColor DarkBlue;
- }
- #only for executing the first page
- break;
- }
- }
- }
- catch {
- Write-Host "$($_.Exception.Message)" -ForegroundColor Red;
- }
Add Comment
Please, Sign In to add comment