Person or Group Field Append or Remove a Group

Using PowerShell I needed to append a group to a Person or Group field in a list.  The same logic should apply for adding or removing a user.

Append Group:


$web = Get-SPweb "http://sharepointed.com/sites/taco/"
$list = $web.lists["Good Tacos"]

$groupName = "Taco Eaters"
$group = $web.SiteGroups[$groupName]
$GroupValue = new-Object Microsoft.SharePoint.SPFieldUserValue($web,$group.id, $group.Name)

foreach ($item in $list.items)
 {
 $groups = $item["GroupsField"]

$groups.Add($GroupValue)

$item["GroupsField"] = $groups
 $item.Update()
 }

Remove Group:


$web = Get-SPweb "http://sharepointed.com/sites/taco/"
$list = $web.lists["Good Tacos"]

$groupName = "Taco Eaters"
$group = $web.SiteGroups[$groupName]
$GroupValue = new-Object Microsoft.SharePoint.SPFieldUserValue($web,$group.id, $group.Name)

foreach ($item in $list.items)
{
$groups = $item["GroupsField"]

<span style="line-height: 1.5em;">$groupRemove = $groups | ? { $_.LookupId -eq $GroupValue.LookupId }</span>

$groups.Remove($groupRemove)

$item["GroupsField"] = $groups
$item.Update()
}

Count the number of columns that contain a value

Say you have three columns that you want to find if a value exists in, then count the columns that contain that string.

In my list, I created three columns.

cOne, cTwo, cThree

All have a field type of single line of text.

Then I created a calculated column named cCalc.

The formula for cCalc is:

=COUNT(FIND(“Taco”,cOne),FIND(“Taco”,cTwo),FIND(“Taco”,cThree))

In my formula, I’m searching each field for the word Taco.  Keep an eye on your case usage.  Taco is not the same as taco.

Find function:

http://office.microsoft.com/en-us/sharepoint-server-help/find-function-HA001160996.aspx

Count function:

http://office.microsoft.com/en-us/windows-sharepoint-services-help/count-function-HA001160977.aspx

One or more field types are not installed properly. Go to the list settings page to delete these fields

Error:
One or more field types are not installed properly. Go to the list settings page to delete these fields.

After much research, I have found that this error can be the result of a couple things.
1. Programmatically create a site column / content type and the process fails. The result is a broken column that you have to hunt down and remove from the content database.
2. Using a combination of Export-SP and Import-SPweb, an imported list or library will display the error.

In my case, #2 needed to be fixed. Basically, I had a lookup column that had become orphaned from its parent list.
When you look at the field settings (Library/List Settings –> click on your lookup column) you will see that the Get information from: property is empty.

So how do you reconnect or refresh the lookup field property?
Using PowerShell, the fields web ID and source list ID schemaXML need to be populated.

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
$web = Get-SPWeb "http://sharepoint.com/site/"
$list = $web.Lists["Broken LoopField List"]
$lookupList = $web.Lists["Loopup Source List"]

$fixColumn = $list.Fields["My Broken Lookup Field"]

#Change schema XML on the lookup column
$fixColumn.SchemaXml = $fixColumn.SchemaXml.Replace($fixColumn.LookupWebId.ToString(), $web.ID.ToString())
$fixColumn.SchemaXml = $fixColumn.SchemaXml.Replace($fixColumn.LookupList.ToString(), $lookupList.ID.ToString())
$fixColumn.Update()

What’s going on here?
Load the SharePoint snapin.
Get site that holds the broken list / library.
Get broken list / library.
Get source list / library that the broken lookup references.
Get broken field.
Update the broken field schemaXML.