How to create folders based on user display name in Document

Harshavardhan 21 Reputation points
2020-11-30T08:02:10.437+00:00

Dear Team,

I would like to create folder based on user display name(using created by field) when user uploads documents into library.

Thanks in advance.

Microsoft 365 and Office | SharePoint Server | Development
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Emily Du-MSFT 51,941 Reputation points Microsoft External Staff
    2020-12-01T07:08:41.657+00:00

    @Harshavardhan
    Try below PowerShell:

    $Web = Get-SPWeb "site collection URL"  
    $List = $web.Lists["document library name"]  
    $Item = $List.GetItembyID("document ID")  
    $Author = New-Object Microsoft.SharePoint.SPFieldUserValue($web, $Item["Author"])  
    $files=$List.RootFolder.files   
    for($i = 0; $i -lt $files.Count){  
    $file=$files[$i]     
    $f = $web.GetFolder($list.RootFolder.ServerRelativeUrl + "/" + $Author.user.displayname)  
    if ($f.Exists -eq $false)  
      {  
        $folder = $list.Items.Add($list.RootFolder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $Author.user.displayname)  
        $folder.Update();  
      }  
    $file.MoveTo($folder.Url + "/" + $file.name)  
    }  
    

    Results:
    45571-11.png
    45542-12.png


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.