Skip to content

Instantly share code, notes, and snippets.

@Na0mir
Last active August 29, 2015 14:18
Show Gist options
  • Save Na0mir/08a13a08e6aec847c683 to your computer and use it in GitHub Desktop.
Save Na0mir/08a13a08e6aec847c683 to your computer and use it in GitHub Desktop.
Add SharePoint users to community site (SPWebtemplate). Based on XML input file.
# ----------------------------------------------
# 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"
}
}
}
<config>
<community url="http://rbla-sp2010-002/communautes/communaute01">
<user id="rbla\rblanchard" />
</community>
</config>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment