Script Repository

Create user folder in SharePoint library

October 14, 2015

This script creates a folder in a SharePoint document library for a user. To use it in Adaxes, you can add the script to a Business Rule, Custom Command or Scheduled Task using the Run a program or PowerShell script action.


  • $sharePointServer - specifies the NetBIOS name of the computer where the SharePoint Sever is homed;
  • $webApplicationURL - specifies the URL of the SharePoint web application;
  • $libraryName - specifies the name of the SharePoint library where the folder will be created;
  • $userFolderName - specifies the name that will be assigned to the user's folder;
    Note: You can use value references (e.g. %username%) to insert properties of the user account in the folder name.
  • $filesToCopy - specifies paths to files hosted on the SharePoint server that will be copied to the user's folder;
  • $stopInheritablePermissions - specifies whether to stop inheriting permissions from the parent and use custom security settings;
  • $customSecurityItems - specifies security settings for objects other than the target user. Specify $NULL if you want to use default permissions.

    You can specify custom security settings for users, Active Directory groups or SharePoint groups. Format:


    Default role types: Administrator, Contributor, Reader, WebDesigner.
Edit Remove
$sharePointServer = "SharePointServer" # TODO: modify me

$webApplicationURL = "http://$sharePointServer/sites/MySite" # TODO: modify me
$libraryName = "MyLibrary" # TODO: modify me
$userFolderName = "%username%" # TODO: modify me

$filesToCopy = @("Shared Documents/Sales/Prices.xls", "Shared Documents/Sales/Managers.xls") # TODO: modify me. Specify files to be copied to the library

$stopInheritablePermissions = $True # TODO: modify me. Specify $False to inherits permissions from the parent or $True to stop inheritance

$customSecurityItems = @{"EXAMPLE\Administrator"="Administrator";"SharePointDesigners"="WebDesigner"} # TODO: modify me. Specify security settings for objects other than the target user

$domainName = $Context.GetObjectDomain("%distinguishedName%")
$flatDomainName = $domainName.SubString(0,$domainName.IndexOf("."))
$targetUserName = "$flatDomainName\%sAMAccountName%"

$session = New-PSSession $sharePointServer -Authentication Kerberos

$result = Invoke-Command -Session $session -ArgumentList $webApplicationURL, $libraryName, $userFolderName, $filesToCopy, $stopInheritablePermissions, $customSecurityItems, $targetUserName -ScriptBlock {
    param($webApplicationURL, $libraryName, $userFolderName, $filesToCopy, $stopInheritablePermissions, $customSecurityItems, $targetUserName)
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | Out-Null
    $site = New-Object Microsoft.SharePoint.SPSite("$webApplicationURL")
    $web = $site.OpenWeb();
    $docLibrary = ($web.GetListsOfType("DocumentLibrary"))[$libraryName]
    $docLibraryFolders = $docLibrary.Folders
    $userFolder = $NULL

    foreach($folder in $docLibraryFolders)
        If($folder.Name -eq $userFolderName)
            $userFolder = $folder

    if($userFolder -eq $NULL)
        $userFolder = $docLibraryFolders.Add($docLibrary.RootFolder.ServerRelativeUrl, 1, $userFolderName)
    $userFolderRoleAssignments = $userFolder.RoleAssignments
    if ($stopInheritablePermissions)
        $userFolderRoleAssignmentsCount = $userFolderRoleAssignments.Count
        for ($i = $userFolderRoleAssignmentsCount-1; $i -ge 0; $i--)
    # Set permissions for the target user
    $ownerRoleAssignment = New-Object Microsoft.SharePoint.SPRoleAssignment("$targetUserName",$null,$null,$null)
    $errorInfo = $NULL
    # Set custom permissions for other users
    if ($customSecurityItems -ne $NULL)
        foreach($objectName in $customSecurityItems.Keys)
            $roleTypeName = $customSecurityItems[$objectName]
                $roleDefinition = $web.RoleDefinitions.GetByType($roleTypeName)
                $errorInfo += $objectName + "; "
            if($web.SiteGroups[$objectName] -ne $NULL)
                $customRoleAssignment = New-Object Microsoft.SharePoint.SPRoleAssignment($web.SiteGroups[$objectName])
                $customRoleAssignment = New-Object Microsoft.SharePoint.SPRoleAssignment($objectName,$null,$null,$null)
    # Copy files
    if($filesToCopy -ne $NULL)
        foreach($fileToCopy in $filesToCopy)
            $fileToCopy = $web.GetFile($web.Url + "/" + $fileToCopy)
            $fileName = $fileToCopy.Name
            $fileToCopy.CopyTo($userFolder.URL + "/" + $fileName.SubString(0,$fileName.IndexOf(".")) + " %initials%" + $fileName.SubString($fileName.IndexOf(".")))
    return $errorInfo

Remove-PSSession -Session $session

if($result -ne $NULL)
    $Context.LogMessage("Permissions for the folder were not set for " + $result + " as the specified SharePoint Role Type was not found on the server.", "Warning")

Comments ( 0 )
No results found.
Leave a comment