Tuesday, July 26, 2011

Using the asp:ListView control in SharePoint

Below is example code implementing the asp:ListView control in a SharePoint page. Bound data comes from the SPDataSource control.

The SelectCommand is in the CAML query language which is XML based. The query looks for all Calendar events ending today or later, row limit of 4, sorted by start date. You can also use parameters for a dynamic SelectCommand:

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.spdatasource.selectparameters.aspx

<asp:ListView ID="ListView1" runat="server" DataSourceID="spDataSource1">
<LayoutTemplate>
<div style="background-color: #fffddd; padding: 10px; width: 200px">
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</div>
</LayoutTemplate>
<ItemTemplate>
<p style="font-size:small;">
<a href="/examples/Lists/Events//DispForm.aspx?ID=<%# Eval("ID") %>" style="color:#787878"><span style='text-decoration:none;<%# Eval("Category").Equals("Holiday") ? "font-weight:bold" : "font-weight:normal" %>'><%# Eval("DisplayDateTime").ToString().Substring(8) %></span><br />
<span style="font-style:italic"><%# Eval("Title") %></span></a></p>
</ItemTemplate>
</asp:ListView>

<SharePointWebControls:SPDataSource runat="server" ID="spDataSource1" DataSourceMode="List" SelectCommand="<Query><Where><Geq><FieldRef Name='EndDate' /><Value Type='DateTime'><Today /></Value></Geq></Where><OrderBy><FieldRef Name='EventDate' Ascending='True' /></OrderBy><RowLimit>4</RowLimit></Query>">
<SelectParameters> <asp:Parameter Name="WebUrl" DefaultValue="/examples/" /> <asp:Parameter Name="ListName" DefaultValue="Events" /> </SelectParameters>
</SharePointWebControls:SPDataSource>

Thursday, July 21, 2011

Creating an SPDataSource .NET control

To use an SP List as a data source for a .NET web control, declare an SPDataSource as shown:

<SharePointWebControls:SPDataSource runat="server" ID="spDataSource1" DataSourceMode="List" SelectCommand="<Query><Where><Eq><FieldRef Name='Section'/><Value Type='Choice'>Get Started</Value></Eq></Where><OrderBy><FieldRef Name='SortOrder' Ascending='True' /></OrderBy></Query>">
<SelectParameters> <asp:Parameter Name="WebUrl" DefaultValue="/student-affairs/enrollment/" /> <asp:Parameter Name="ListName" DefaultValue="Transfer" /> </SelectParameters>
</SharePointWebControls:SPDataSource>


The SelectCommand property allows for filtering and sorting of results.

The ID for this data source will then go inot the DataSourceID field of the web control that uses the SP List, as shown below:

<asp:GridView ID="DataGrid1" runat="server" DataSourceID="spDataSource1">
...
</asp:GridView>