using (SPWeb newWeb = web.Site.OpenWeb(web.ID))
{
cT = newWeb.ContentTypes[cT.Id];
SPField myField = cT.Fields[field.Title];
cT.FieldLinks[myField.Id].Hidden = true;
cT.Update(true);
newWeb.Update();
}
Category Archives: SharePoint
This item cannot be updated because it is locked as read-only
Had to call in Microsoft Support to help unlock files that were marked as read-only. Some files appeared to be locked, but using the UI, they couldn’t be unlocked, declared as a record, or edited.
Error: This item cannot be updated because it is locked as read-only
Error: The file “your file string” is checked out for editing by SHAREPOINT\system
This script was setup to run on a SharePoint 2010 instance. If you need to run it on another version, try updating the $sharePointAssembly line.
To unlock a file, input the URL of the file in the line that starts with $fileUrl.
$recordFields =
"_vti_ItemHoldRecordStatus",
"_vti_ItemDeclaredRecord"
$recordProperties =
"ecm_RecordRestrictions",
"ecm_ItemLockHolders",
"ecm_ItemDeleteBlockHolders"
$fileUrl = "http://siteURL/siteCollection/libraryName/folder/subFolder/fileName.pdf"
$sharePointAssembly = [System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
Add-Type -TypeDefinition @"
using Microsoft.SharePoint;
public class EventsDisabler : SPEventReceiverBase
{
public EventsDisabler() {}
public bool EventsDisabled
{
get { return !EventFiringEnabled; }
set { EventFiringEnabled = !value; }
}
}
"@ -ReferencedAssemblies $sharePointAssembly
Write-Host "Getting site collection at $fileUrl..."
[Microsoft.SharePoint.SPSite]$site = New-Object Microsoft.SharePoint.SPSite($fileUrl)
if ($site -eq $null) { exit }
$siteUrl = $site.Url
Write-Host "Found site collection $siteUrl"
Write-Host ""
Write-Host "Getting web at $fileUrl..."
[Microsoft.SharePoint.SPWeb]$web = $site.OpenWeb()
if ($web -eq $null) { exit }
$webTitle = $web.Title
Write-Host "Found web $webTitle"
Write-Host ""
Write-Host "Verifying current user is System Account"
$web.CurrentUser.ID
$site.SystemAccount.ID
if ($web.CurrentUser.ID -ne $site.SystemAccount.ID)
{
Write-Error "Please run this script as System Account" -Category PermissionDenied
#exit
}
Write-Host ""
Write-Host "Getting list at $fileUrl..."
Write-Host $fileUrl
[Microsoft.SharePoint.SPList]$list = $web.GetList($fileUrl)
if ($list -eq $null) { exit }
$listTitle = $list.Title
Write-Host "Found list $listTitle"
Write-Host ""
Write-Host "Getting list item at $fileUrl..."
[Microsoft.SharePoint.SPListItem]$listItem = $web.GetListItem($fileUrl)
if ($listItem -eq $null) { exit }
$listItemName = $listItem.Name
Write-Host "Found list item $listItemName"
Write-Host ""
$eventsDisabler = New-Object EventsDisabler
$eventsOriginallyDisabled = $eventsDisabler.EventsDisabled
if ($eventsOriginallyDisabled -eq $false)
{
Write-Host "Disabling events"
$eventsDisabler.EventsDisabled = $true
Write-Host ""
}
$didWork = $false
$itemNeedsUpdate = $false
#Discard any check-out
if ($listItem.File -ne $null -and $listItem.File.CheckOutType -ne [Microsoft.SharePoint.SPFile+SPCheckOutType]::None)
{
Write-Host "Undoing check-out"
$listItem.File.UndoCheckOut()
$didWork = $true
}
else
{
Write-Host "No file or file is not checked out"
Write-Host ""
}
#Iterate the Record fields and set all values to null
foreach($recordField in $recordFields)
{
if ($listItem.Fields.ContainsField($recordField) -eq $true -and $listItem[$recordField] -ne $null)
{
$recordFieldValue = $listItem[$recordField]
Write-Host "$recordField = $recordFieldValue"
Write-Host "Setting $recordField to null"
$listItem[$recordField] = $null
$didWork = $true
$itemNeedsUpdate = $true
}
}
#Iterate the Record properties and remove any that exist
foreach($recordProperty in $recordProperties)
{
if ($listItem.Properties.ContainsKey($recordProperty) -eq $true)
{
$recordPropertyValue = $listItem.Properties[$recordProperty]
Write-Host "$recordProperty = $recordPropertyValue"
Write-Host "Removing property $recordProperty"
$listItem.Properties.Remove($recordProperty)
$didWork = $true
$itemNeedsUpdate = $true
}
}
#Remove the icon Record lock overlay
if ($listItem.IconOverlay -eq "lockoverlay.png")
{
Write-Host "Removing the icon Record lock overlay"
$listItem.IconOverlay = $null
$didWork = $true
$itemNeedsUpdate = $true
}
if ($didWork -ne $true)
{
Write-Host "No changes were made"
}
Write-Host ""
#Update the item
if ($itemNeedsUpdate -eq $true)
{
Write-Host "Updating item"
$listItem.SystemUpdate()
Write-Host ""
}
if ($eventsOriginallyDisabled -ne $true)
{
Write-Host "Enabling events"
$eventsDisabler.EventsDisabled = $false
Write-Host ""
}
$web.Dispose()
$site.Dispose()
Find all Lists and Libraries Where InfoPath is Used
Recently had the need to find and update most every InfoPath form in a production farm. This, mixed with the future demise of InfoPath, prompted the need to find or write a script. The below script will traverse every list and library in a given web app. The script can be easily modified to include every web app in a farm.
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
$fileName = "C:\xsn-" + $(Get-Date -Format "yyyyMMddHHmmss") + ".csv"
$webApp = Get-SPWebApplication "http://webApp.sharepointed.com"
"Title `t URL `t Type" | out-file $fileName
$(foreach($siteColl in $webApp.Sites)
{
foreach($web in $siteColl.AllWebs)
{
foreach($list in $web.Lists) {
if ($list.BaseType -eq "DocumentLibrary" -and $list.BaseTemplate -eq "XMLForm"){
"$($list.title) `t $($list.parentweb.url)$($list.DefaultViewUrl) `t Library" | out-file $fileName -Append
}elseif ($list.ContentTypes[0].ResourceFolder.Properties["_ipfs_infopathenabled"]){
"$($list.title) `t $($list.ParentWeb.URL)$($list.DefaultViewUrl) `t List" | out-file $fileName -Append
}
else{
$check = 0
foreach($c in $list.ContentTypes) {
if($c.DocumentTemplateUrl.ToString().EndsWith("xsn") -eq $true)
{
$check++
}
}
if($check -gt 0)
{
"$($list.title) `t $($list.ParentWeb.URL)$($list.DefaultViewUrl) `t List" | out-file $fileName -Append
}
}
}}})
PowerShell Unable to index into an object of type Microsoft.SharePoint.SPList.
Made a change to an existing PowerShell script, then ran into this error: Unable to index into an object of type Microsoft.SharePoint.SPList
If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{ Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell }
$web = Get-SPWeb "http://sharepointed.com/sitename"
$listTask = $web.Lists["My List"]
$sItem = $listTask.GetItemById(9)
$title = $listTask["Title"]
In the script, you will notice that I’m trying to capture the title of the item. The above script wasn’t working because I failed work with this list item, instead I was trying to work with the list object.
Correct script:
If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{ Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell }
$web = Get-SPWeb "http://sharepointed.com/sitename"
$listTask = $web.Lists["My List"]
$sItem = $listTask.GetItemById(9)
$title = $sItem["Title"]
copy.asmx This item is a copy of
Using the SharePoint Web Service Copy.asmx, you may notice this message on copied items: This item is a copy of
What I was doing wrong was, I used the destination URL in place of the source URL. If you provide a source URL, you will also receive the This item is a copy of… message in the properties view of the item. To avoid the message all together I replaced the source URL with the file name.
//bad
cWs.CopyIntoItems(destination, destinationUrl, fields.ToArray(), myBinary, out result);
//good
cWs.CopyIntoItems(filename, destinationUrl, fields.ToArray(), myBinary, out result);
Error When Updating Item Using Web Service MoveNext
Using the SharePoint REST service to update items, ran into this error:
Unable to update the sharepoint document – An error occurred while processing this request.System.Data.Services.Client.DataServiceClientException: <?
xml version=”1.0″ …
…..
at System.Data.Services.Client.DataServiceContext.SaveResult.<HandleBatchResponse>d_1e.MoveNext()
The target list was set to require checkout before items could be updated. Once I set require checkout to No, the REST service was able to update items.
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()
}
Client machine making thousands of calls to SharePoint sites
——- Still researching this topic ——
Randomly each day, my computer was making thousands of calls to a couple SharePoint sites. Using Fiddler, I could see that the calls were being made through WebDav.
User-Agent: Microsoft-WebDAV-MiniRedir/6.1.7601
Then I notice that the first call of the batch was always to this web service:
/_vti_bin/publishedlinksservice.asmx
Googled around and found this post. It’s related to SP2007 but it helped to find the answer.
After reading the post, I opened Word, clicked on File –>Save and Send –> Save to SharePoint. There it was, all of the sites that my computer was reaching out to every day. Taking things one more step deeper, I opened a document library, selected the Library tab in the ribbon, then selected Connect to Office. From there select Manage SharePoint Sites. Displayed was listed the libraries that I had created connections to.
http://mysite.yourdomain.com/_layouts/MyQuickLinks.aspx
All of the sites that are displayed in the Save to SharePoint section of Office are stored here:
HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Common\Server Links\Published\My Site\
I deleted the My Site folder, opened Word and all of the sites were cleared out.
Also cleared out the following folder:
HKEY_Current_User\Software\Microsoft\Office\14.0\Common\Portal\Link Providers\
Next I need to make sure the folder doesn’t get repopulated with random sites!
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
Office SharePoint Server Standard Site features must be enabled
In SharePoint Designer 2010, I was attempting to create an approval task for an item. When I tried to added the action to the workflow, I received the following error.
Cannot insert this action. To use task process actions, the Office SharePoint Server Standard Site features must be enabled for this site by an administrator.
At the site and site collection I made sure SharePoint Server Standard Site features was enabled. Taking it one step further, I also enabled SharePoint Server Enterprise Site features. Still the workflow wasn’t working correctly. Navigated back to the site collection features and activated the Workflows feature. Refreshed SharePoint Designer and all of the actions worked.
Credit goes to Giles Hamson.