Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- format-default : The collection has not been initialized. It has not been requested or the request has not been executed. It may need
- to be explicitly requested.
- + CategoryInfo : NotSpecified: (:) [format-default], CollectionNotInitializedException
- + FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException,Microsoft.PowerShell.Commands.FormatDefau
- ltCommand
- Add-Type -Path 'C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.dll'
- Add-Type -Path 'C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Runtime.dll'
- ### Get the user credentials
- $credential=Get-Credential
- $username=$credential.UserName
- $password=$credential.GetNetworkCredential().Password
- $securePassword = ConvertTo-SecureString $password -AsPlainText -Force
- ### Input Parameters
- $url = 'https://aareSharePoint.sharepoint.com/sites/muhsite/'
- ### References
- # Specify the path where the dll's are located.
- Add-Type -Path 'c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.dll'
- Add-Type -Path 'c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Runtime.dll'
- $usrName = 'Aare Test'
- $email = 'aare@aareDomain.com'
- ### CreateFolder Function
- function CreateFolder()
- {
- # Connect to SharePoint Online and get ClientContext object.
- $clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
- $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
- $clientContext.Credentials = $credentials
- # Get the SharePoint web
- $web=$clientContext.Web;
- $clientContext.Load($web)
- $clientContext.ExecuteQuery()
- # Adds the folder that is located at the specified URL to the collection
- # Create a new folder in SharePoint Documents
- $folder1=$web.Folders.Add('Library1/'+$usrName)
- $folder2=$web.Folders.Add('Library2/'+$usrName)
- $clientContext.Load($folder1)
- $clientContext.Load($folder2)
- # Execute the query
- $clientContext.ExecuteQuery()
- $role = $web.RoleDefinitions.GetByName('Edit')
- $usrRole = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($clientContext)
- $usrRole.Add($role)
- $clientContext.Load($role)
- $clientContext.Load($usrRole)
- $usr = $Web.EnsureUser('i:0#.f|membership|'+$email)
- Write-Host $usrRole.Description
- $clientContext.Load($usr)
- $clientContext.ExecuteQuery()
- # Display the folder name and URL
- Write-Host -ForegroundColor Green 'Folder Name: ' $folder1.Name ' URL: '$folder1.ServerRelativeUrl;
- Write-Host -ForegroundColor Green 'Folder Name: ' $folder2.Name ' URL: '$folder2.ServerRelativeUrl;
- $folder1.ListItemAllFields.BreakRoleInheritance($false, $true)
- $folder2.ListItemAllFields.BreakRoleInheritance($false, $true)
- $folder1.ListItemAllFields.RoleAssignments.Add($usr,$usrRole)
- $folder2.ListItemAllFields.RoleAssignments.Add($usr,$usrRole)
- $clientContext.Load($folder1)
- $clientContext.Load($folder2)
- $clientContext.ExecuteQuery()
- }
- CreateFolder
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement