0

I have Site Admin Custom sharepoint list in a Site1, where users in our oganization need to submit a request to become Cerified Site Collection Administrator. List Consists of 3 columns:

  • Site Name:Single Line of text ( Site Name)
  • Employee: People and group ( required field where users use people picker to select them from all users).
  • Request Status: Choice ( This Field Gets updated according to underlying workflow i have over the list)

In the same site i have 3 Sharepoint groups

  • Site visitors : read permission
  • Site Contributors : Contribute Permisssion
  • Site Owners: Full Control

Now i need to create a powershell code to copy the unique item in Site Admin Custom sharepoint list column employees into Site Owners Group, when the item's request status is set to approved.

Can this be done using Powershell, i am able to set up filter in list and get all available groups in the give n site. Now how do i copy items over using powershell.

Thank You

1 Answer 1

1

Let $web be the SPWeb object where your list is, and let $item be the SPListItem that your script is working on. Here is how to extract the SPUser from $item:

Function Add-SPUserFromListToOwnersGroup
{
    param
    (
        [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
        [Microsoft.SharePoint.SPListItem] $item,
        [Microsoft.SharePoint.SPWeb] $web
    )
    begin {}

    process
    {
        $userfield = New-Object Microsoft.SharePoint.SPFieldUserValue($web, $item["Employee"].ToString();
        $user = $userfield.User;

        $group = $web.SiteGroups["Site Owners"];
        $group.AddUser($user);
    }

    end {}
}

Then, you can use your PowerShell script as follows:

$items = $web.Lists["List Title"].Items | Where { $_["Request Status"] -eq 'Approved' };
$items | Add-SPUserFromListToOwnersGroup -web $web;

Please note, however, that this code will suffer performance-wise for "large" (> 2000 items) lists because of the call to $list.Items. If your list is huge like that, you can filter the list by CAML query to get all the items which you can then pipe into Add-SPUserFromListToOwnersGroup.

PS: I wrote a function in a previous question here which takes a field name (here, Employee) and derives the SPUser from it. It may be helpful for you.

1
  • Thank You Jeff, I was able to use CAML Query to get the result set rather than calling $list.Items. It is performing as expected. Sep 29, 2014 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.