January 20, 2012

SharePoint: List does not exist after adding Data Form Web Part to a page

Problem:
After checking in your Page Layout containing a Data Form Web Part and changing the page layout of the publishing page you get error:

List does not exist.
The page you selected contains a list that does not exist.  It may have been deleted by another user.


Your DFWP uses SharePoint List that can be located on another site, but this issue can occur also when the list is on the same site.

Solution:
SharePoint Designer has been helping you during the checkin process and has added three attributes to the Data Form Web Part: ListId, ListName and ListUrl. Remove those and you're good to go.

<WebPartPages:DataFormWebPart runat="server" Description=""  Title="Contacts" ... ListId="SOMEGUID" ListName="{SOMEGUID}" ListUrl=""...>

Note You need to keep removing those every now and then as they keep reappearing.

SharePoint: Filter Data Form Web Part using Metadata value of current Publishing Page

Challenge:

On a Welcome Page of a Publishing Site I had few Managed Metadata fields. I was to implement a functionality that would fetch items from a list located on another site and filter the items using value of a metadata field of the current Welcome Page.

The idea is brilliant: there is a customer site, and there is a common list of contacts. Due to requirements, one common contacts list was preferred, so I decided to map contacts to customers by adding metadata to contact indicating to what customer it belonged to AND have the same metadata defined on the customer page's properties.

Solution:

As you cannot reference page field values in DFWP like it is done with CQWP, you must use Control parameter in Location attribute inside your ParameterBinding node (thank you Tomas Breen).

<ParameterBinding Name="Customer" Location="Control(CustomerName,ItemFieldValue)" DefaultValue=""/>

Customer above is the name of the parameter you will use in XSL. CustomerName is the ID of the Control on the Page Layout that contains the value you want to use as a filter. ItemFieldValue is the magic word I had to look for way too long.

Then, in the xsl:stylesheet of your DFWP, you will have

<xsl:param name="Customer" />

and finally you will do the filtering like this

<xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row[@Customer= substring-before($Customer, '|')]"/>

Note that @Customer is the value of the Customer metadata field of one of the list items we're about to display. $Customer is the xsl:param that contains the value from the Customer metadata field of the current Welcome Page. Substring is done because Welcome Page metadata value comes in the form CompanyX|GUID.

Not yet working? No, because what is still missing is the actual CustomerName control that will contain the Customer value of the current Welcome Page. In my case I used TaxonomyFieldControl and added the ID attribute. You could also use SharePointWebControls:FieldValue, or SharePointWebControls:ListItemProperty, but TaxonomyFieldControl allows editing of the value when in Edit mode and I needed that.

<Taxonomy:TaxonomyFieldControl FieldName="becb57f8-ac0e-45b1-a0b4-e5fa71c0d7fa" ID="CustomerName" runat="server"></Taxonomy:TaxonomyFieldControl>

Note that you will not be able to preview the functionality in SPD. You will have to browse to the page to see your beautiful markup magic.

January 9, 2012

SharePoint 2010: The data source control failed to execute the insert command.

Problem:

When modifying a Form of a list with SharePoint Designer 2010, at some point you might get error:

Error while executing web part: System.ArgumentException: Value does not fall within the expected range

The data source control failed to execute the insert command.

This may only occur with a specific type of List Form (New, Edit,Display).

Solution:

While there can be many causes for this generic error message, in my case the first parameter or ddwrt:DataBind function inside the SharePoint:FormFields element was 'i' and I was working with an Edit Form. Changing it to 'u' as it was with every other FormField fixed the issue.

<SharePoint:FormField runat="server" id="ff1{$Pos}" ControlMode="Edit" FieldName="Esittaja" __designer:bind="{ddwrt:DataBind('u',concat('ff1',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Esittaja')}" />

Explanation:

DataBind operation type parameters (the first parameter) are listed below:

'i' stands for INSERT,
'u' stands for UPDATE,
'd' stands for DELETE.

More about ddwrt:DataBind syntax can be found from Bryan's article.