SharePoint 2013 Make the document icon in a document library work

We have a customer who is using Colligo Engage for Outlook, uploading emails to SharePoint Online and tagging them with some additional metadata for other departments to keep track of what is occurring.

All of that is well and good but the name column was just too long to display along with all the other columns in the library view. This is a pretty common problem with long file names and one you can usually solve with users who are uploading traditional files but with this process they didn’t modify the file name Colligo automatically generated.

Continue reading “SharePoint 2013 Make the document icon in a document library work”

PowerShell crashes when using start-job in a loop

In one of my scripts I needed to implement a kind of watch dog for part of the script which had a tendency to barf and get stuck. I settled on using start-job with a timeout so I just wrapped the buggy method in that and then give it 15 seconds to comply before terminating. When the next loop runs it starts by looking for any dead process threads and kills them so all the memory doesn’t fill up. For those interested – the $workbook.close() sometimes just sat there and did nothing until I killed the excel.exe process.
Continue reading “PowerShell crashes when using start-job in a loop”

Create a SharePoint group with permissions and add an AD group to it

group_add

Requirement

  • Create a SharePoint group in a site collection
  • Assign that group an existing permission for Site Permissions list
  • Add an Active Directory group to that SharePoint group
  • Read the values from a CSV file
  • Repeat for 1000 groups spread across multiple site collections
  • The Site Collection Admin will run the script and be added as owner automatically

 

Solution

function global:New-SPGroup {
#Parameters that the script offers out to use,
# e.g. New-SPGroup -SiteCollection “
http://intranet/sitecollection” -ADGroupName “domain\ADgroup” -SPGroupName “SharePoint Group 1” -SPGroupDescription “Group Description” -SPGroupPermission “Permission”
[CmdletBinding()]
Param(
[Microsoft.SharePoint.PowerShell.SPWebPipeBind]
[string]$SiteCollection,
[string]$ADGroupName,
[string]$SPGroupName,
[string]$SPGroupDescription,
[string]$SPGroupPermission
)

#Required variabales for dev or single item runs, remove # and highlight from here down to above CSV section in Powershell ISE
#$SiteCollection = “
http://site/sitecollection”
#$ADGroupName = “domain\adgroup”
#$SPGroupName = “My Test Group A1”
#$SPGroupDescription = “Test Group A1’s Description” #Note: do not use” – ” in description, i.e. space hyphen space
#$SPGroupPermission = “Read”

#Start of script
$site = Get-SPWeb $SiteCollection

#Check if the group already exists
if ($site.SiteGroups[“SPGroupName”] -eq $null)
{

#Ensure Group/User is part of site collection users beforehand and add them if needed
$site.EnsureUser(“$ADGroupName”)

# Get the AD Group/User in a format that PowerShell can use otherwise there will be a string error
$ADGroupSPFriendly = $site | Get-SPUser $ADGroupName

#Create the SharePoint Group – Group Name, Group Owner, Group Member, Group Description. Can’t add AD group yet…
$NewSPGroup = $site.SiteGroups.Add($SPGroupName, $site.CurrentUser, $site.CurrentUser, $SPGroupDescription)
$site.AssociatedGroups.Add($site.SiteGroups[“$SPGroupName”]);
$NewSPAccount = $site.SiteGroups[“$SPGroupName”]

#Assign the Group permission
$GroupAssignment = New-Object Microsoft.SharePoint.SPRoleAssignment($NewSPAccount)
$GroupRole = $site.RoleDefinitions[“$SPGroupPermission”]
$GroupAssignment.RoleDefinitionBindings.Add($GroupRole)
$site.RoleAssignments.Add($GroupAssignment)

#Add the AD Group/User to the group, can’t be done during group creation when using Powershell otherwise errors so is done now.
Set-SPUser -Identity $ADGroupSPFriendly -Web $SiteCollection -Group $SPGroupName
}
$site.Dispose()
}

#Read from the CSV input file
#CSV file must have header row – SiteColl, AdSecGroup, SPSecGroupName, SPGroupDesc, SPGroupPerm
#No “” around any items in the CSV file are needed
$csv = Import-csv -path D:\DEVGroupNameCSVFile2.csv
if ($csv -ne $null) {
foreach($line in $csv)
{
[string]$SiteCollection = $line.SiteColl;
[string]$ADGroupName = $line.AdSecGroup;
[string]$SPGroupName = $line.SPSecGroupName;
[string]$SPGroupDescription = $line.SPGroupDesc;
[string]$SPGroupPermission = $line.SPGroupPerm

New-SPGroup -Site $SiteCollection -ADGroupName $ADGroupName -SPGroupName $SPGroupName -SPGroupDescription $SPGroupDescription -SPGroupPermission $SPGroupPermission;
}
}

 

I hope someone else finds this script useful since all I found on the web were partial answers at best, and with a few solutions that didn’t match the requirement.

Errors encountered along the way and running the script

“You cannot add a domain group to a group.” – You can however add your AD group to the SharePoint group after is it created, just not while you are creating it. Resolved by the last part of the function.

Exception calling “Add” with “4” argument(s): “The specified name is already in use. Please try again with a new name.” – As it implies, this is a duplicate item in your CSV file.

References

Basis for script –

http://sharepointryan.com/2011/07/20/create-sharepoint-groups-using-powershell/

http://addictedtosharepoint.com/2011/07/23/adding-a-security-group-to-a-sharepoint-site-via-powershell/

http://www.iotap.com/Blog/tabid/673/entryid/154/Powershell-script-for-Adding-Active-Directory-Users-to-Sharepoint-2010-Groups.aspx

http://get-spscripts.com/2011/02/add-sharepoint-or-ad-groupuser-to-all.html

http://stackoverflow.com/questions/4512548/how-to-add-ad-group-to-sharepoint-2010-spgroup-programmatically

http://shpstuff.blogspot.co.uk/2012/05/create-groups-in-site-collection-using.html

http://stackoverflow.com/questions/3713497/how-to-get-spuser-object-using-user-id-domain-name-userid

Additional information that you might find useful –

http://sharepoint.stackexchange.com/questions/20739/add-users-to-sp-group

http://social.msdn.microsoft.com/Forums/eu/sharepointdevelopment/thread/2df1f82b-ba04-4378-9df8-6fb0bf8eeaa3

http://www.learningsharepoint.com/forum/sharepoint-2010-programming/add-user-with-powershell-in-sharepoint-2010-site/

http://social.technet.microsoft.com/forums/en-GB/sharepointadminprevious/thread/c9e11537-6b1a-46f6-b030-395f8ed1fbf7

http://social.technet.microsoft.com/forums/en-ZA/sharepointgeneralprevious/thread/0632b071-617f-4e93-bd06-ed85ab7ef4a6

[aboutme username=”ashley.lawrence”]