We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.

Script repository

Create user folder in SharePoint library

February 18, 2021 Views: 3482

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.

Parameters:

  • $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:

    "DOMAIN\username"="RoleType";"DOMAIN\groupname"="RoleType";"SharePointGroupName"="RoleType"

    Default role types: Administrator, Contributor, Reader, WebDesigner.
Edit Remove
PowerShell
$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)
        $userFolder.Update()
    }
    
    $userFolderRoleAssignments = $userFolder.RoleAssignments
    $userFolder.BreakRoleInheritance($true)
    
    if ($stopInheritablePermissions)
    {    
        $userFolderRoleAssignmentsCount = $userFolderRoleAssignments.Count
        for ($i = $userFolderRoleAssignmentsCount-1; $i -ge 0; $i--)
        {
             $userFolder.RoleAssignments.Remove($i)
        }
    }
    
    # Set permissions for the target user
    $ownerRoleAssignment = New-Object Microsoft.SharePoint.SPRoleAssignment("$targetUserName",$null,$null,$null)
    $ownerRoleAssignment.RoleDefinitionBindings.Add($web.RoleDefinitions.GetByType("Administrator"))
    $userFolder.RoleAssignments.Add($ownerRoleAssignment)
    $errorInfo = $NULL
    # Set custom permissions for other users
    if ($customSecurityItems -ne $NULL)
    {
        foreach($objectName in $customSecurityItems.Keys)
        {
            $roleTypeName = $customSecurityItems[$objectName]
            try
            {
                $roleDefinition = $web.RoleDefinitions.GetByType($roleTypeName)
            }
            catch
            {
                $errorInfo += $objectName + "; "
                continue
            }
            if($web.SiteGroups[$objectName] -ne $NULL)
            {
                $customRoleAssignment = New-Object Microsoft.SharePoint.SPRoleAssignment($web.SiteGroups[$objectName])
            }
            else
            {
                $customRoleAssignment = New-Object Microsoft.SharePoint.SPRoleAssignment($objectName,$null,$null,$null)
            }
            $customRoleAssignment.RoleDefinitionBindings.Add($roleDefinition)
            $userFolder.RoleAssignments.Add($customRoleAssignment)
        }
    
    }
    $userFolder.Update()
    
    # 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
Leave a comment
Loading...

Got questions?

Support Questions & Answers