简体   繁体   中英

Adding multiple people to SharePoint peoplepicker from string - convert to object

I need to convert a string into an object for the command "Add-PnPListItem".

I have an excel list with multiple mail addresses which I want to add to a SharePoint Online PeoplePicker Field. ( https://learn.microsoft.com/en-gb/powershell/module/sharepoint-pnp/add-pnplistitem?view=sharepoint-ps )

Regarding the documentation, the format needs to be in quotation marks and comma-separated: Person/Group (id of user/group in Site User Info List or email of the user, separate multiple values with a comma): -Values @{"Person" = "user1@domain.com","21"}

My problem is that if I read the data from my list, it is a string. (In the format as needed.) As Example "mail@test.com","mail2@test.com" .

The command returns an error "Add-PnPListItem: The specified user could not be found."

Maybe to make it more clear:

        $Members = $WorkSheet.Range("G$i").Value2  #= "mail@test.com","mail2@test.com"
        $test = "mail@test.com","mail2@test.com" 
       Add-PnPListItem -List List -Values @{"Members" = "$Members"} #does not work
Add-PnPListItem : The specified user "mail@test.com","mail2@test.com" could not be found.
       Add-PnpListItem -List List -Values @{"Members" = "$test"} #works

If I type the string above directly in my Powershell in the desired format, it works as aspected.

I tested creating a custom object via Add-Member and also an ArrayList with the Split function and have no idea how to solve my problem.

So far my code:

 for ($i=2; $i -le $LastRowInExcel; $i++)  {
  
$ProjectNo = $WorkSheet.Range("A$i").Value2        #SingleLineOfText
$ProjectName = $WorkSheet.Range("B$i").Value2      #URL Field combined with ProjectSiteUrl
$ProjectSiteUrl = $WorkSheet.Range("C$i").Value2   #URL Field combined with ProjectName
$Client = $WorkSheet.Range("D$i").Value2           #Choice
$ProjectManager = $WorkSheet.Range("E$i").Value2   #PeoplePicker - Input via Email
$Director = $WorkSheet.Range("F$i").Value2         #PeoplePicker - Input via Email
$Members = $WorkSheet.Range("G$i").Value2          #PeoplePicker - Input via Email


    #Example that works: 
    $test = "mail@test.com","mail2@test.com"

    #$Object = New-Object PSObject 
    #$Object | Add-Member NoteProperty "Member" $Members


            
    #[System.Collections.ArrayList]$MyAL = @()                
    #$Members.Split(",") | ForEach {
    #$MyAL.Add($_)}
 

Add-PnPListItem -List $listName -Values @{"Title"=$ProjectNo; `
                                          "ProjectNameWithLink"="$ProjectSiteUrl, $ProjectName"; `
                                          "ProjectName"="$ProjectName"; `
                                          "ClientLookup"="$Client"; `
                                          "Manager"="$ProjectManager"; `
                                          "Director"="$Director"; `
                                          "Members" = "$Members"; `
                                          "Start"="$StartDate"; `
                                          "End"="$EndDate"; `
                                          "OfficeLookup"="$Office"; `
                                          "TypeLookup"="$ProjectType"; `
                                          "StatusLookup"="$ProjectStatus"; `
                                          "subsiteCreated"="$SubsiteCreated"; `
                                          "ProjectStructureType"="$ProjectStructureType"; `
                                          "AlertSend"="$AlertSend"; `
                                          "TSISite"=$false; `
                                          "Planner"=$false; `
                                          "Team"=$false; `
                                          "PMChecklist"=$false; `
                                          "PermGroupIdContr"="$PermGroupID"; `
                                          "PermGroupIdRead"="$PermGroupRead"}
    
                             
}

$test = "mail@test.com","mail2@test.com" creates an array of string items, not one string. If your Excel sheet outputs a string like ""mail@test.com", "mail2@test.com"" , you can create an array of that by splitting at the comma and trimming it.

$Members = $WorkSheet.Range("G$i").Value2.Split(',').Trim()

That'll create an array of strings like $test that the PnP command should accept.

If you need to remove the quotes from the Excel string:

$Members.Replace('"', '')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM