Tuesday, April 10, 2012

Changing Toolbars For List Web Parts

The problem:

I need to always show the toolbar, MOSS 2007-style, on some of the list web parts on the site I am automatically provisioning. Unfortunately, there is no straightforward way to do it via the SharePoint API, for example through the SPWebPart or the SPView objects.

The solution:

I am not sure why Microsoft chose to omit this functionality from the object model, as this seems to be a pretty common requirement. But, it is possible to change the toolbar type if you use reflection. The code below does the trick. Note that I am passing "ShowToolbar" as a parameter to the "SetToolbarType" method.


Friday, March 30, 2012

Long Running Operations

The problem:

I have a long-running operation that is supposed to transfer to an error page in case of an error.

This code is perfectly valid. However, the error page (SPUtility.ErrorPage, which is just a synonym for error.aspx) it transfers to is too generic ("An error has occurred on the server") and cannot be customized with a more informative error message.


The solution:

You don't have to call the End() method for the long operation; if the result is erroneous, make a transfer to the error page using SPUtility.TransferToErrorPage, which takes an error message as a parameter. This code does the trick. Note also that it catches ThreadAbortException.

Thursday, March 1, 2012

Adding a Positive Integer Column To a List Via PowerShell

The problem:

I need to add a column to my list via PowerShell; this column must be a positive integer.

The solution:

Here is a simple script to accomplish this:

$site = Get-SPSite "http://my_site_collection"
$web = $site.OpenWeb("my_site")
$list = $web.Lists["my_list"]
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Integer

#The new field is not required, so the value of the third parameter is 0
$spFieldName = "PositiveNo"
$list.Fields.Add($spFieldName, $spFieldType, 0)
$list.Update()

#Now that we created a new field, let's change it so that it has a minimal value of 1 and no decimal places
$spField = $list.Fields.GetField($spFieldName)
$spField.MinimumValue = 1
$spField.DisplayFormat = "0"
$spField.Update()

#Need to add the new column to the list
$views = $list.Views["All Items"]
$views.ViewFields.Add($spFieldName)
$views.Update()

Friday, February 24, 2012

Flickering Progress Box in Outlook 2010 While Opening a Task

The problem:

A workflow assigned me a task. I opened the task email in Outlook 2010 and then pushed the "Open This Task" button on the ribbon. All I got was a flickering progress box. The task was never opened and it took me quite a while to get rid of the flickering progress box.

The solution:

This seems like a security issue (and could have been handled more gracefully by Outlook.) While normally I don't advocate editing system files on the hive, this is the case when it is necessary. I opened the 14\LAYOUTS\FormsServer.aspx and added the following line right after the <body> tag:


<SharePoint:FormDigest runat="server" />

This creates a security validation for the FormsServer.aspx page and thus gets rid of the annoying flickering issue.

Friday, February 17, 2012

Threaded View in Discussion Boards

The problem:

Our customer created a discussion board and a custom threaded view for this list and made this new threaded view a default one. Now he is no longer able to access this list. When he tries to do so, he gets a nasty and cryptic "Attempted to use an object that has ceased to exist. (Exception from HRESULT: 0x80030102 (STG_E_REVERTED))" error message. 

The solution:

The root cause of this is the misconfigured threaded view. Unfortunately, Microsoft made it too easy to make an error here. The threaded view (and the flat view, too, for that matter) do not work on top-level folders, therefore the only proper way to configure folders for the threaded view or the flat view is this:


To fix the problem I mentioned, the view that is not working must be removed and re-created - to get to the list settings, follow the "Content and Structure" link in the Site Administration, and then follow the "Edit Properties" link in the hovering menu.

Wednesday, February 15, 2012

Search Service Application

The problem:

I have a search service application in my farm, My only content source contains several  claims-aware SSL Web applications. Unfortunately, for some reason, the gatherer was not able to crawl them, throwing a 404 status, for no apparent reason. Deleting and re-creating the search service application did not help matters at all. The ULS logs did not show anything unusual, and neither did the IIS logs.

The solution:

Actually, it was more like a workaround that my co-worker and I invented. He has another SharePoint farm on the same domain. We made our farms trust each other, and then I made my farm consume his search service application. See http://technet.microsoft.com/en-us/library/ee704558.aspx on how exactly to do this. Then, I created my own content source and added my own crawl rule and registered my own custom security trimmer. After all these manipulations, the search worked!

Wednesday, January 25, 2012

Cannot Delete Files From Document Library

The problem:

I needed to delete several files from my document library. I tried to do it using the ribbon. Alas, nothing was deleted. Fiddler brought to my attention that the request was nioved; the ULS log informed me that a new request to the AccessDenied.aspx page was created. I never saw the AccessDenied.aspx page, so the results were not clear at all.

The solution:

I looked at the log again and learned that, when you are deleting the files using the ribbon, the Web request goes to the WCF client.svc web service (http://site_root/_vti_bin/client.svc/ProcessQuery).

My next step was checking the authentication for my Web application. It turned out that for this particular web application, Anonymous Authentication was disabled. When I enabled it back, everything started working.

Lesson learned:  Disabling anonymous authentication can bring some unpredictable effects.