Zhaobab
3/30/2015 - 12:37 PM

Add SharePoint users to community site (SPWebtemplate). Based on XML input file.

Add SharePoint users to community site (SPWebtemplate). Based on XML input file.

<config>
	<community url="http://rbla-sp2010-002/communautes/communaute01">
	    <user id="rbla\rblanchard" />	
    </community>
</config>
# ----------------------------------------------
# Author: Romain Blanchard
# Date: 02.07.2014
# Description: Add SharePoint users to community site (SPWebtemplate). Based on XML input file.
# ----------------------------------------------

# -- Script -- #

# Foreach Community
Select-Xml -Path "users.xml" -Xpath "/config/community" | ForEach-Object { 
	
	$url = $_.Node.url
    write-host "--- Working on $web community ---" -foregroundcolor "Yellow"
    write-host ""
    $web = Get-SPWeb $url
    $memberslist = $web.Lists["Membres de la communauté"]

    ## Foreach users inside this community
    $_.Node | Select-Xml -Xpath "user" | ForEach-Object {
        $userid =  $_.Node.id
        $user = $web.EnsureUser($userid)

        write-host "Adding user $userid ..." -foregroundcolor "Yellow" -NoNewLine
        $userexistbool = $false

        ## Check if user already exist into this community        
        foreach ($item in $memberslist.Items)
        {
            ## If user already present set boolean to true
            if ($item["Title"] -eq $user.Name)
            {
                $userexistbool = $true
            }
        }

        if ($userexistbool -eq $true)
        {
            write-host " this user already exist." -foregroundcolor "Red"
        }

        ## If user not present, add it and leave the loop
        else
        {
            ## Add user to member list
            $memberItem = $memberslist.Items.Add()
            $memberItem["Title"] = $user.Name
            $memberItem["Member"] = $user
            $memberItem["MemberStatusInt"] = 1
            $memberItem.Update()

            ## Add user to Members Security group
            $membersGroup = $web.SiteGroups["Membres"]
            $membersGroup.AddUser($user)

            ## Increment property bag value
            $web.AllowUnsafeUpdates = $true
            $web.AllProperties["Community_MembersCount"] = ($memberslist.ItemCount)
            $web.Update()
            $web.AllowUnsafeUpdates = $false

            write-host " done!" -foregroundcolor "Green"
        }
    }
}