none
Error creating a SharePoint group using PowerShell - Cannot find an overload for "Add" and the argument count: "4". RRS feed

  • Question

  • Hi All,
    I am trying to create a SharePoint Online group using PowerShell but keep get an error message. The code and error message are show below:

    *************** Code Example 1 ***************
    $loc = "C:\Users\User1\Desktop\dll\"
    $user ="AlanHardy@Outlook.com"
    $site = "https://CompanySite.sharepoint.com/sites/Dev1/"
    Set-Location $loc
    Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.dll")
    Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.Runtime.dll")
    Write-Host "Add Password"
    $SecPass = Read-Host -AsSecureString
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($site);
    $cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($user,$SecPass)
    $context.Credentials = $cred
    $web = $context.Web
    $context.Load($web)
    $context.ExecuteQuery()
    $web.SiteGroups.Add("Test Group", "GaryRed@outlook", "MarkApple@outlook", "Description")
    $ownerGroup.Update()
    $context.ExecuteQuery()

    *************** ERROR MESSAGE ***************
    Cannot find an overload for "Add" and the argument count: "4".
    At line:21 char:1
    + $web.SiteGroups.Add("Test Group", "GaryRed@outlook", "MarkApple@outlook", " ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodException
        + FullyQualifiedErrorId : MethodCountCouldNotFindBest

    You cannot call a method on a null-valued expression.
    At line:22 char:1
    + $ownerGroup.Update()
    + ~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull
    *************************************

    I also tried:

    *************** Code Example 2 ***************
    $loc = "C:\Users\User1\Desktop\dll\"
    $user ="AlanHardy@Outlook.com"
    $site = "https://CompanySite.sharepoint.com/sites/Dev1/"
    Set-Location $loc
    Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.dll")
    Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.Runtime.dll")
    Write-Host "Add Password"
    $SecPass = Read-Host -AsSecureString
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($site);
    $cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($user,$SecPass)
    $context.Credentials = $cred
    $web = $context.Web
    $context.Load($web)
    $context.ExecuteQuery()
    $groupMemberUser = $web.EnsureUser("TinaBrown@Outlook.com")
    $groupOwnerUser = $web.EnsureUser("NickYoung@Outlook.com")
    $context.Load($groupMemberUser)
    $context.Load($groupOwnerUser)
    $context.ExecuteQuery()
    $web.SiteGroups.Add("Test Group", $groupMemberUser, $groupOwnerUser, "Group Description")
    $context.ExecuteQuery()

    *************** ERROR MESSAGE ***************
    Cannot find an overload for "Add" and the argument count: "4".
    At line:27 char:1
    + $web.SiteGroups.Add("Test Group", $groupMemberUser, $groupOwnerUser,  ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodException
        + FullyQualifiedErrorId : MethodCountCouldNotFindBest
    **************************************

    I have even tried :
    $web.SiteGroups.Add("XX", $web.SiteUsers["AnyUser@outlook.com"], $web.SiteUsers["AnyUser@outlook.com"], "test")

    but still get the same error message

    I hope you can help
    Colin










    • Edited by ceBlueSky Tuesday, January 17, 2017 3:33 PM
    Tuesday, January 17, 2017 2:49 PM

Answers

  • Hi, you will need to first load 'SiteGroups' and then use 'Add' function.

    Also use GroupCreationInformation class for creation of Group. Here is the code, 

    $context.Load($web.SiteGroups)
    $context.ExecuteQuery()
    $group = New-Object Microsoft.SharePoint.Client.GroupCreationInformation
    $group.Title = "Test Group"
    $group.Description = "Description"
    $group.Owner = $groupOwnerUser
    $group.Update()
    $context.ExecuteQuery()
    

    Use $group.Users.Add for adding uses in the group.

    Again you will need to use UserCreationInformation class to add uses, same as GroupCreationInformation.


    get2pallav
    Please click "Propose As Answer" if this post solves your problem or "Vote As Helpful" if this post has been useful to you.

    • Marked as answer by ceBlueSky Tuesday, January 17, 2017 7:25 PM
    Tuesday, January 17, 2017 3:52 PM
  • Hi,

    The function add accepts the group object, the idea is you create a new group object and add to the existing group collection.

    Please find the sample below:

    $Groups = $Context.Web.SiteGroups
    $Context.Load($Groups)
    $Context.ExecuteQuery()

    #Create Group
    $NewGroup = New-Object Microsoft.SharePoint.Client.GroupCreationInformation
    $NewGroup.Title = $PermName
    $NewGroup.Description = $PermDescription
    $AddedGroup = $Context.Web.SiteGroups.Add($NewGroup)
    $Context.ExecuteQuery()

    Please refer the below article for more detailed information on group creation and assigning permissions.

    https://blogs.technet.microsoft.com/fromthefield/2015/04/02/office-365-create-permissions-level-and-groups-using-csom/


    Please remember to mark the replies as answers and upvote them if they help. Thanks & Regards, Balu

    • Marked as answer by ceBlueSky Tuesday, January 17, 2017 7:25 PM
    Tuesday, January 17, 2017 4:48 PM

All replies

  • Hi, you will need to first load 'SiteGroups' and then use 'Add' function.

    Also use GroupCreationInformation class for creation of Group. Here is the code, 

    $context.Load($web.SiteGroups)
    $context.ExecuteQuery()
    $group = New-Object Microsoft.SharePoint.Client.GroupCreationInformation
    $group.Title = "Test Group"
    $group.Description = "Description"
    $group.Owner = $groupOwnerUser
    $group.Update()
    $context.ExecuteQuery()
    

    Use $group.Users.Add for adding uses in the group.

    Again you will need to use UserCreationInformation class to add uses, same as GroupCreationInformation.


    get2pallav
    Please click "Propose As Answer" if this post solves your problem or "Vote As Helpful" if this post has been useful to you.

    • Marked as answer by ceBlueSky Tuesday, January 17, 2017 7:25 PM
    Tuesday, January 17, 2017 3:52 PM
  • Hi,

    The function add accepts the group object, the idea is you create a new group object and add to the existing group collection.

    Please find the sample below:

    $Groups = $Context.Web.SiteGroups
    $Context.Load($Groups)
    $Context.ExecuteQuery()

    #Create Group
    $NewGroup = New-Object Microsoft.SharePoint.Client.GroupCreationInformation
    $NewGroup.Title = $PermName
    $NewGroup.Description = $PermDescription
    $AddedGroup = $Context.Web.SiteGroups.Add($NewGroup)
    $Context.ExecuteQuery()

    Please refer the below article for more detailed information on group creation and assigning permissions.

    https://blogs.technet.microsoft.com/fromthefield/2015/04/02/office-365-create-permissions-level-and-groups-using-csom/


    Please remember to mark the replies as answers and upvote them if they help. Thanks & Regards, Balu

    • Marked as answer by ceBlueSky Tuesday, January 17, 2017 7:25 PM
    Tuesday, January 17, 2017 4:48 PM