Guest User

Untitled

a guest
Feb 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. function Get-SPOContext([string]$Url, [string]$UserName, [string]$Password) {
  2. $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force;
  3. $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url);
  4. $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword);
  5. return $context;
  6. }
  7.  
  8. function Get-ListItems([Microsoft.SharePoint.Client.ClientContext]$Context, [String]$ListTitle) {
  9. $list = $Context.Web.Lists.GetByTitle($listTitle);
  10. $qry = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery();
  11. $items = $list.GetItems($qry);
  12. $Context.Load($items);
  13. $Context.ExecuteQuery();
  14. return $items;
  15. }
  16.  
  17. try {
  18. $UserName = "XXXXXXXX"
  19. #$Password = Read-Host -Prompt "Enter the password"
  20. $Password = "XXXXXXXX"
  21. $Url = "https://abc.def.com/sites/subsite1/"
  22.  
  23. $context = Get-SPOContext -Url $Url -UserName $UserName -Password $Password
  24. $items = Get-ListItems -Context $context -ListTitle "Pages"
  25.  
  26. Write-Host "Total Pages Count " $items.Count -BackgroundColor Cyan;
  27.  
  28. foreach ($item in $items) {
  29.  
  30. $contentType = $item.ContentType;
  31. $context.Load($contentType);
  32. $Context.ExecuteQuery();
  33.  
  34. if ($contentType.Name -eq "Custom Page Layout") {
  35. $pageFieldValues = $item.FieldValues;
  36.  
  37. #text field updation
  38. $item["Comments"] = "New Comment 1";
  39. $item.Update();
  40. $Context.ExecuteQuery();
  41.  
  42. #print the values
  43. foreach ($h in $pageFieldValues.GetEnumerator()) {
  44. Write-Host "$($h.Key)" -ForegroundColor Red;
  45. Write-Host "$($h.Value)" -ForegroundColor DarkBlue;
  46. }
  47.  
  48. #only for executing the first page
  49. break;
  50. }
  51. }
  52. }
  53. catch {
  54. Write-Host "$($_.Exception.Message)" -ForegroundColor Red;
  55. }
Add Comment
Please, Sign In to add comment