0

I am looking to programatically create sites based on items in a SharePoint list. The list has 4 columns, Name, Email, ManagerName, ManagerEmail.

The aim is to use PowerShell to create a foreach loop, that creates a site (using the "Name" column as the site title/URL) based on the #STS01 teamsite template, breaks permission inheritance, and then gives the Manager ("ManagerName") contribute access to the site.

Not sure where to start with this, any advice would be appreciated!

Cheers!

0

First you need to read the list thorugh powershell. I assume you will run the script as a scheduled job/manually which will run with Farm admin privilages. Here is the code that will help you read the list items. Then in the For loop, you can create site using powershell with all the required parameters.

Also, once a site is created, you can update the list item with a FLAG value which marks that the site is created. So, when next time the script reads the list to create new sites, it will only get the details of list items where the FLAG is not TRUE. This means that while fetching the list items, you need to use CAML query to fetch only the items with FLAG value not true. Refer this. you query will look like

<Where>
 <Neq>
   <FieldRef Name='SiteCreatedFlag' />
   <Value Type='Boolean'>TRUE</Value>
 </Neq>

0
  • Get-SPWeb to aquire Sharepoint site
  • .List['name_of_list'] on retrieved site to get list
  • .Items on retrieved list to get elements
  • | ForEach-Object to get single item data and create site with it.
0
$sourceWebURL = "http://sharepointsite"
$sourceListName = "mylist"

     function AddUserToSite ($web, $loginName, $permLevel)
     {
       $account = $web.SiteUsers[$loginName]
       $assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($account)
       $role = $web.RoleDefinitions[$permLevel]
       $assignment.RoleDefinitionBindings.Add($role);
       $web.RoleAssignments.Add($assignment)
     }

$spSourceWeb = Get-SPWeb $sourceWebURL
$spSourceList = $spSourceWeb.Lists[$sourceListName]

$spSourceItems = $spSourceList.Items

$spSourceItems | ForEach-Object {
     $nameListItem = $_['Name']  #this line is 'http://somesite/subweb'
     $emailListItem = $_['Email']
     $managerNameListItem = $_['ManagerName'] #this line is 'Domain\username'
     $managerEmailListItem = $_['ManagerEmail']
     #create sub site
     New-SPWeb $nameListItem -Template "STS#1" -UniquePermissions –UseParentTopNav           
     # get instance of new sub site
     $web = Get-SPWeb $nameListItem

     #Break permissions inheritance and assign the current user as the only member of this site
     $web.BreakRoleInheritance($false)
     AddUserToSite -web $web -loginName $managerNameListItem -permLevel "Contribute"
}

Search details:

Add groups or user

Get Items from list

Creating new web

Hope this help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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