none
Updating "Person or Group" field of file getting uploaded via Powershell

    Question

  • Greetings,

    I am a complete programming/powershell noob.

    I grabbed parts from various scripts to create a powershell script that does the following after I provide the first and last name of a new employee:

    1. creates a folder in format "lastname, firstname" within a subfolder of a document library and stops inheriting permissions.

    2. Creates a folder called "2017" within.

    3.uploads 3 files to the "2017" folder.

    4. in the upload loop for each of the three files, it sets a metadata column called "Status" to "Not started"  

    Here is what I am missing. 

    1. I have an "employee" and "manager" column which both are of type "Person or Group".  Somehow it fails to update these two fields. Here is the code snippet:

    Foreach ($File in (dir $UploadSourceFolder -File))
    {
        $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
        $FileCreationInfo.Overwrite = $true
        $FileCreationInfo.Content = [System.IO.File]::ReadAllBytes($File.FullName)
        $FileCreationInfo.URL = $List.RootFolder.ServerRelativeUrl + "/" + $UploadTargetFolder + "/" + $File.Name
        $UploadFile = $List.RootFolder.Files.Add($FileCreationInfo)
        $UploadFile.ListItemAllFields["Status"] = "Started";
        $user = UserLookUp;
        $UploadFile.ListItemAllFields["Manager"] = $user;
        $UploadFile.ListItemAllFields.Update();
        $Context.Load($UploadFile)
        $Context.ExecuteQuery()
       
    }

    Here is the UserLookUp Function:

    Function UserLookUp()
    {
    $Ctx = Get-Context -Url $Url -Username $Username -Password $Password
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $Ctx.ExecuteQuery()
    Write-Host $Web.Title
    
    
    $User = $Web.EnsureUser("john@contoso.com)
    $Ctx.Load($User)
    $Ctx.ExecuteQuery()
    
    $userValue = new FieldUserValie
    Write-Host $User.Email
    }

    This runs without error, but the "manager" field is empty when I check it in the browser.

    Any idea what I am doing wrong? 

    2. I was able to stop inheriting permissions for the "Last name, first name" folder, but now I need to add 2 groups and two single users with Full Control to that folder. 

    Any idea how to do that? 

    I know this is a lot and pardon my coding. This is my first attempt at writing a script.

    Any help is appreciated.

    Thursday, April 20, 2017 8:06 PM

All replies

  • Hi Chendrich, 

    Since you are working with SharePoint online, I suggest you creating a new thread in SharePoint online forum:

    http://answers.microsoft.com/en-us

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thanks for your understanding.

    Best Regards, 

    Lisa Chen 



    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Saturday, April 22, 2017 9:27 AM
    Moderator