DataFormWebPart add data to XML

hi all, finally for my SharePoint bosses - a SP Post!

DataFormWebPart  (DFWP), a nice thing that you usually give it a CAML query to a list and BANG you have it in your XSL file, awesome!

BUT - what happens when you want MORE?

so i'll bring some code examples to stuff I did:
1. totally change the data to something completely else.
2. simple aggregation using SharePoint:AggregateDataSource, in webpart file

1. totally change the data to something completely else :
that's pretty simple, you can override the DataBind() method, there you create an XmlDocument, fill it with whatever you want and put is as the sourceDoc

public override void DataBind()
{
    this.sourceDoc = new _MyXmlCreator().CreateXmlDoc);
    this.BindXmlData();
}

I saw in stackoverflow that its a MUST to also override GetXPathNavigator()
protected override XPathNavigator GetXPathNavigator(string viewPath)
{
 
 
if (sourceDoc != null)
    {
       
return sourceDoc.CreateNavigator();
    }
    
return base.GetXPathNavigator(viewPath);
}


from my exp. its better to have 1 root element and then all you stuff, and eventually your xsl will look something like this:
<xsl:template match="/" >
<xsl:for-each select="/root/MyGroupNode/MyNode">

as simple as that!

2. simple aggregation using SharePoint:AggregateDataSource, in webpart file
sometimes all you need is data from 2 lists, so this control allows you, and it will look like this
<property name="DataSourcesString" type="string">
&lt;%@ Register TagPrefix="cc1" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %&gt;&lt;%@ Register TagPrefix="cc2" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %&gt;&lt;cc1:AggregateDataSource runat="server" RowsName="" SeparateRoot="" RootName=""&gt;&lt;Sources&gt;
&lt;cc1:SPDataSource runat="server" DataSourceMode="List" SelectCommand="&amp;lt;View&amp;gt;&amp;lt;Query&amp;gt;&amp;lt;/Query&amp;gt;&amp;lt;/View&amp;gt;" UpdateCommand="" InsertCommand="" DeleteCommand="" UseInternalName="True" UseServerDataFormat="True"&gt;
&lt;SelectParameters&gt;
&lt;cc2:DataFormParameter ParameterKey="ListName" PropertyName="ParameterValues" Name="ListName"&gt;&lt;/cc2:DataFormParameter&gt;
&lt;cc2:DataFormParameter ParameterKey="WebURL" PropertyName="ParameterValues" DefaultValue="/" Name="WebURL"&gt;&lt;/cc2:DataFormParameter&gt;
&lt;cc2:DataFormParameter Name="Category" ParameterKey="Category" PropertyName="ParameterValues"&gt;&lt;/cc2:DataFormParameter&gt;
&lt;asp:Parameter DefaultValue="0" Name="StartRowIndex"&gt;&lt;/asp:Parameter&gt;
&lt;asp:Parameter DefaultValue="0" Name="nextpagedata"&gt;&lt;/asp:Parameter&gt;
&lt;/SelectParameters&gt;
&lt;/cc1:SPDataSource&gt;
&lt;cc1:SPDataSource runat="server" DataSourceMode="List" SelectCommand="&amp;lt;View&amp;gt;&amp;lt;Query&amp;gt;&amp;lt;/Query&amp;gt;&amp;lt;/View&amp;gt;" UpdateCommand="" InsertCommand="" DeleteCommand="" UseInternalName="True" UseServerDataFormat="True"&gt;
&lt;SelectParameters&gt;
&lt;cc2:DataFormParameter ParameterKey="HebTextListName" PropertyName="ParameterValues" Name="ListName"&gt;&lt;/cc2:DataFormParameter&gt;
&lt;/SelectParameters&gt;
&lt;/cc1:SPDataSource&gt;
&lt;/Sources&gt;
&lt;Aggregate&gt;
&lt;concat name="data source"&gt;&lt;datasource name="List" id="0" Type="SPList" /&gt;&lt;datasource name="HebText" id="1" Type="SPList" /&gt;&lt;/concat&gt;&lt;/Aggregate&gt;
&lt;/cc1:AggregateDataSource&gt;
</property>

ye that looks awful, that's cuz MS love so much xml and they do everything in xml and if they need nested xml they'll just encode it and double encode it if needed again.

so we have here the encoded thing that we would use in an as*x file and the caml is double encoded so here is real words:

<%@ Register TagPrefix="cc1" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %><%@ Register TagPrefix="cc2" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<cc1:AggregateDataSource runat="server" RowsName="" SeparateRoot="" RootName="">
<Sources>
<cc1:SPDataSource runat="server" DataSourceMode="List" SelectCommand="<View><Query></Query></View>" UpdateCommand="" InsertCommand="" DeleteCommand="" UseInternalName="True" UseServerDataFormat="True">
<SelectParameters>
<cc2:DataFormParameter ParameterKey="ListName" PropertyName="ParameterValues" Name="ListName"></cc2:DataFormParameter>
<cc2:DataFormParameter ParameterKey="WebURL" PropertyName="ParameterValues" DefaultValue="/" Name="WebURL"></cc2:DataFormParameter>
<asp:Parameter DefaultValue="0" Name="StartRowIndex"></asp:Parameter>
<asp:Parameter DefaultValue="0" Name="nextpagedata"></asp:Parameter>
</SelectParameters>
</cc1:SPDataSource>
<cc1:SPDataSource runat="server" DataSourceMode="List" SelectCommand="<View><Query></Query></View>" UpdateCommand="" InsertCommand="" DeleteCommand="" UseInternalName="True" UseServerDataFormat="True">
<SelectParameters>
<cc2:DataFormParameter ParameterKey="HebTextListName" PropertyName="ParameterValues" Name="ListName"></cc2:DataFormParameter>
</SelectParameters>
</cc1:SPDataSource>
</Sources>
<Aggregate>
<concat name="data source">
<datasource name="List" id="0" Type="SPList" />
<datasource name="HebText" id="1" Type="SPList" />
</concat>
</Aggregate>
</cc1:AggregateDataSource>

btw this can also be done in code: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.aggregatedatasource(v=office.12).ASPX

now here there are few things to remember:
1 - in the xsl add this ns: xmlns:agg=http://schemas.microsoft.com/sharepoint/aggregatesource
2 - in the xsl you need to choose your rows by list like this
<xsl:for-each select="/dsQueryResponse/Rows[@agg:source='HebText']/Row">


Comments

Popular posts from this blog

OverTheWire[.com] Natas Walkthrough - JUST HINT, NO SPOILERS

Asp.Net Ending Response options, Response.End() vs CompleteRequest()

SOLVED The item could not be indexed successfully because the item failed in the indexing subsystem