Advertisement
Guest User

Untitled

a guest
May 6th, 2016
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. format-default : The collection has not been initialized. It has not been requested or the request has not been executed. It may need
  2. to be explicitly requested.
  3. + CategoryInfo : NotSpecified: (:) [format-default], CollectionNotInitializedException
  4. + FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException,Microsoft.PowerShell.Commands.FormatDefau
  5. ltCommand
  6.  
  7. Add-Type -Path 'C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.dll'
  8. Add-Type -Path 'C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Runtime.dll'
  9.  
  10.  
  11. ### Get the user credentials
  12. $credential=Get-Credential
  13. $username=$credential.UserName
  14. $password=$credential.GetNetworkCredential().Password
  15. $securePassword = ConvertTo-SecureString $password -AsPlainText -Force
  16.  
  17. ### Input Parameters
  18. $url = 'https://aareSharePoint.sharepoint.com/sites/muhsite/'
  19.  
  20. ### References
  21. # Specify the path where the dll's are located.
  22. Add-Type -Path 'c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.dll'
  23. Add-Type -Path 'c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Runtime.dll'
  24.  
  25. $usrName = 'Aare Test'
  26. $email = 'aare@aareDomain.com'
  27.  
  28. ### CreateFolder Function
  29. function CreateFolder()
  30. {
  31. # Connect to SharePoint Online and get ClientContext object.
  32. $clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
  33. $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
  34. $clientContext.Credentials = $credentials
  35.  
  36. # Get the SharePoint web
  37. $web=$clientContext.Web;
  38. $clientContext.Load($web)
  39. $clientContext.ExecuteQuery()
  40.  
  41. # Adds the folder that is located at the specified URL to the collection
  42. # Create a new folder in SharePoint Documents
  43. $folder1=$web.Folders.Add('Library1/'+$usrName)
  44. $folder2=$web.Folders.Add('Library2/'+$usrName)
  45. $clientContext.Load($folder1)
  46. $clientContext.Load($folder2)
  47. # Execute the query
  48. $clientContext.ExecuteQuery()
  49.  
  50. $role = $web.RoleDefinitions.GetByName('Edit')
  51. $usrRole = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($clientContext)
  52. $usrRole.Add($role)
  53. $clientContext.Load($role)
  54. $clientContext.Load($usrRole)
  55.  
  56. $usr = $Web.EnsureUser('i:0#.f|membership|'+$email)
  57. Write-Host $usrRole.Description
  58. $clientContext.Load($usr)
  59. $clientContext.ExecuteQuery()
  60.  
  61. # Display the folder name and URL
  62. Write-Host -ForegroundColor Green 'Folder Name: ' $folder1.Name ' URL: '$folder1.ServerRelativeUrl;
  63. Write-Host -ForegroundColor Green 'Folder Name: ' $folder2.Name ' URL: '$folder2.ServerRelativeUrl;
  64.  
  65.  
  66. $folder1.ListItemAllFields.BreakRoleInheritance($false, $true)
  67. $folder2.ListItemAllFields.BreakRoleInheritance($false, $true)
  68.  
  69. $folder1.ListItemAllFields.RoleAssignments.Add($usr,$usrRole)
  70. $folder2.ListItemAllFields.RoleAssignments.Add($usr,$usrRole)
  71. $clientContext.Load($folder1)
  72. $clientContext.Load($folder2)
  73. $clientContext.ExecuteQuery()
  74.  
  75.  
  76. }
  77. CreateFolder
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement