0

I've written a powershell script to create a batch of lists. The script does the following:

  1. Creates the library.
  2. Creates SharePoint groups.
  3. Changes the library name to replace spaces with underscores in the name.
  4. Adds a custom content type to the list.
  5. Sets permissions on the list.

After the script is finished, everything seems fine. I can navigate to the lists, and see them in site contents on the front end but I can't see the lists when executing $web.Lists. Why?

Update 08/07 - After some experimenting, I've narrowed it down to this block of script:

# Set Permissions
$list = $web.Lists[$libraryName]
$list.BreakRoleInheritance($true)
$web.AllowUnsafeUpdates=$true
[Microsoft.SharePoint.SPRoleAssignmentCollection] $spRoleAssignments=$list.RoleAssignments
for([int] $a=$spRoleAssignments.Count-1; $a -ge 0;$a--)
{
   if($spRoleAssignments[$a].Member.Name -ne "Group Owners")
   {
      $spRoleAssignments.Remove($a);
   }
}

My Script

# variable description
cls
$webUrl = "http://domain.url.here"
$site = Get-SPSite $webUrl
$web = Get-SPWeb $webUrl
$facilityName = Read-Host 'What is the facility name?'
$cleanURL = $facilityName -replace " ", "_"
$libraryTemplate = [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary;
$groupOwner = $web.SiteGroups["Owners Group"]

## Create Groups
$web.SiteGroups.Add("group1", $groupOwner, $web.Site.Owner, "Use this group to manage permissions to the $facilityName libraries")
$web.SiteGroups.Add("group2", $groupOwner, $web.Site.Owner, "Use this group to manage permissions to the $facilityName libraries")

###########################
## Adding Policy Library ##
###########################

$libraryFunction = "Policies"
$libraryURL = $cleanURL + "_" + $libraryFunction
$libraryName = "$facilityName $libraryFunction"
$libraryDescription = "$libraryFunction library for $facilityName"

# Adding Library
$web.Lists.Add($libraryURL,$libraryDescription,$libraryTemplate)

# Change Library Name and Add Content Type
$list = $web.Lists[$libraryURL]
$list.Title = $libraryName
$list.ContentTypesEnabled = $true
$ctToAdd = $site.RootWeb.ContentTypes["Policy"]
$ct = $list.ContentTypes.Add($ctToAdd)
$list.Update()

# Set Permissions
$list = $web.Lists[$libraryName]
$list.BreakRoleInheritance($true)
$web.AllowUnsafeUpdates=$true
[Microsoft.SharePoint.SPRoleAssignmentCollection] $spRoleAssignments=$list.RoleAssignments
for([int] $a=$spRoleAssignments.Count-1; $a -ge 0;$a--)
{
   if($spRoleAssignments[$a].Member.Name -ne "Owners Group")
   {
      $spRoleAssignments.Remove($a);
   }
}
# Add Facility Visitors Group
$groupName = "$facilityName Policy Visitors"
$permLevel = "Read"
$ListName = "$facilityName $libraryFunction"
$account = $web.SiteGroups[$groupName]
$assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($account)
$role = $web.RoleDefinitions[$permLevel]
$assignment.RoleDefinitionBindings.Add($role);
$list = $web.Lists[$ListName]
$list.RoleAssignments.Add($assignment)

# Add Policies Visitors Group
$groupName = "Policies Visitors"
$permLevel = "Read"
$ListName = "$facilityName $libraryFunction"
$account = $web.SiteGroups[$groupName]
$assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($account)
$role = $web.RoleDefinitions[$permLevel]
$assignment.RoleDefinitionBindings.Add($role);
$list = $web.Lists[$ListName]
$list.RoleAssignments.Add($assignment)

# Add QAM Group
$groupName = "QAM Group"
$permLevel = "Contribute"
$ListName = "$facilityName $libraryFunction"
$account = $web.SiteGroups[$groupName]
$assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($account)
$role = $web.RoleDefinitions[$permLevel]
$assignment.RoleDefinitionBindings.Add($role);
$list = $web.Lists[$ListName]
$list.RoleAssignments.Add($assignment)

############################
## Adding Archive Library ##
############################

$libraryFunction = "Archive"
$libraryURL = $cleanURL + "_" + $libraryFunction
$libraryName = "$facilityName $libraryFunction"
$libraryDescription = "$libraryFunction library for $facilityName"

# Adding Library
$web.Lists.Add($libraryURL,$libraryDescription,$libraryTemplate)

# Change Library Name and Add Content Type
$list = $web.Lists[$libraryURL]
$list.Title = $libraryName
$list.ContentTypesEnabled = $true
$ctToAdd = $site.RootWeb.ContentTypes["Policy"]
$ct = $list.ContentTypes.Add($ctToAdd)
$list.Update()

# Set Permissions
$list = $web.Lists[$libraryName]
$list.BreakRoleInheritance($true)
$web.AllowUnsafeUpdates=$true
[Microsoft.SharePoint.SPRoleAssignmentCollection] $spRoleAssignments=$list.RoleAssignments
for([int] $a=$spRoleAssignments.Count-1; $a -ge 0;$a--)
{
   if($spRoleAssignments[$a].Member.Name -ne "Owners Group")
   {
      $spRoleAssignments.Remove($a);
   }
}

# Add QAM Group
$groupName = "QAM Group"
$permLevel = "Contribute"
$ListName = "$facilityName $libraryFunction"
$account = $web.SiteGroups[$groupName]
$assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($account)
$role = $web.RoleDefinitions[$permLevel]
$assignment.RoleDefinitionBindings.Add($role);
$list = $web.Lists[$ListName]
$list.RoleAssignments.Add($assignment)

############################
## Adding Staging Library ##
############################

$libraryFunction = "Staging"
$libraryURL = $cleanURL + "_" + $libraryFunction
$libraryName = "$facilityName $libraryFunction"
$libraryDescription = "$libraryFunction library for $facilityName"

# Adding Library
$web.Lists.Add($libraryURL,$libraryDescription,$libraryTemplate)

# Change Library Name and Add Content Type
$list = $web.Lists[$libraryURL]
$list.Title = $libraryName
$list.ContentTypesEnabled = $true
$ctToAdd = $site.RootWeb.ContentTypes["Policy"]
$ct = $list.ContentTypes.Add($ctToAdd)
$list.Update()

# Set Permissions
$list = $web.Lists[$libraryName]
$list.BreakRoleInheritance($true)
$web.AllowUnsafeUpdates=$true
[Microsoft.SharePoint.SPRoleAssignmentCollection] $spRoleAssignments=$list.RoleAssignments
for([int] $a=$spRoleAssignments.Count-1; $a -ge 0;$a--)
{
   if($spRoleAssignments[$a].Member.Name -ne "Owners Group")
   {
      $spRoleAssignments.Remove($a);
   }
}

# Add QAM Group
$groupName = "QAM Group"
$permLevel = "Contribute"
$ListName = "$facilityName $libraryFunction"
$account = $web.SiteGroups[$groupName]
$assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($account)
$role = $web.RoleDefinitions[$permLevel]
$assignment.RoleDefinitionBindings.Add($role);
$list = $web.Lists[$ListName]
$list.RoleAssignments.Add($assignment)

##################################
## Adding Dissemination Library ##
##################################

$libraryFunction = "Dissemination"
$libraryURL = $cleanURL + "_" + $libraryFunction
$libraryName = "$facilityName $libraryFunction"
$libraryDescription = "$libraryFunction library for $facilityName"

# Adding Library
$web.Lists.Add($libraryURL,$libraryDescription,$libraryTemplate)

# Change Library Name and Add Content Type
$list = $web.Lists[$libraryURL]
$list.Title = $libraryName
$list.ContentTypesEnabled = $true
$ctToAdd = $site.RootWeb.ContentTypes["Policy"]
$ct = $list.ContentTypes.Add($ctToAdd)
$list.Update()

# Set Permissions
$list = $web.Lists[$libraryName]
$list.BreakRoleInheritance($true)
$web.AllowUnsafeUpdates=$true
[Microsoft.SharePoint.SPRoleAssignmentCollection] $spRoleAssignments=$list.RoleAssignments
for([int] $a=$spRoleAssignments.Count-1; $a -ge 0;$a--)
{
   if($spRoleAssignments[$a].Member.Name -ne "Owners Group")
   {
      $spRoleAssignments.Remove($a);
   }
}

# Add QAM Group
$groupName = "QAM Group"
$permLevel = "Contribute"
$ListName = "$facilityName $libraryFunction"
$account = $web.SiteGroups[$groupName]
$assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($account)
$role = $web.RoleDefinitions[$permLevel]
$assignment.RoleDefinitionBindings.Add($role);
$list = $web.Lists[$ListName]
$list.RoleAssignments.Add($assignment)


# Dispose
$web.AllowUnsafeUpdates=$false
$web.Update()
$web.Dispose()
  • 1
    Can you please share your script? – Phil Greer Aug 6 '14 at 14:22
  • I added my script to the original post. – Michael Harley Aug 6 '14 at 15:18

Your Answer

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

Browse other questions tagged or ask your own question.