Home > SharePoint > Pages with data in SharePoint 2007 and ID as querystring

Pages with data in SharePoint 2007 and ID as querystring

2012/05/16

For complex tasks with data coming from various sources sometimes is required to create a custom page.

Tipically this is done from Site Actions->Create->Web Pages

Here we can create a Web Part Page , choosing the layout (with/without Header, Footer, 2 or more columns and so on) that will be saved in the Shared Documents document library of the current Site .

The same for the Basic Page, in practice a Web Part Page without a layout for 2 or more columns and no web parts zones.

For displaying data in our page we can read the produced code when we create a List / Document Library custom form for add or edit : basically it is used the SharePoint:SPDataSource so we can write

<SharePoint:SPDataSource runat="server" DataSourceMode="List" UseInternalName="true" 
style="margin-left:27pt;">selectcommand="<View></Query><ViewFields><FieldRef Name='Title'/><FieldRef Name='Document_x0020_Type'/></ViewFields></View>" id="spdatasource3">
style="margin-left:27pt;"><SelectParameters>
style="margin-left:54pt;"><asp:Parameter Name="ListID" DefaultValue="{FBCD7E0D-0697-4937-A42E-106DFA056E85}"/>
           </SelectParameters>              
</SharePoint:SPDataSource>

In order to obtain a “skeleton” for the SPDataSource we can create a Basic Page , place the cursor in the code view in SharePoint Designer and in the Data Source Library select “Insert Data Source Control”


Note that in SharePoint Designer 2007 could be necessary a switch to Design view and back to Code in order to have available the menu as in the figure.

By default is created this code

<SharePoint:SPDataSource runat="server" DataSourceMode="List" UseInternalName="true" selectcommand="&lt;View&gt;&lt;/View&gt;" id="spdatasource1">
    <SelectParameters>
        <asp:Parameter Name="ListID" DefaultValue="{FBCD7E0D-0697-4937-A42E-106DFA056E85}"/>
    </SelectParameters>
    <DeleteParameters>
        <asp:Parameter Name="ListID" DefaultValue="{FBCD7E0D-0697-4937-A42E-106DFA056E85}"/>
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Name="ListID" DefaultValue="{FBCD7E0D-0697-4937-A42E-106DFA056E85}"/>
    </UpdateParameters>
    <InsertParameters>
        <asp:Parameter Name="ListID" DefaultValue="{FBCD7E0D-0697-4937-A42E-106DFA056E85}"/>
    </InsertParameters>
</SharePoint:SPDataSource>

If we need to only display data the Delete Update Insert parameter sections can be deleted as in the previous code.

In the selectcommand we can specify a field list , filter conditions and so on; the better approach is to place in a page a ListViewWebPart, convert in XSLT and in the Designer make all changes: after we can read generated commands.

We can display data using the XSLT syntax or by the old mode (note the DataSourceID, the same of our SPDataSource ) :

<asp:FormView ID="ProductsFormView" DataSourceID="spdatasource3" AllowPaging="false" runat="server">
    <ItemTemplate>
      <table>
        <tr>
          <td align="right"><b>Title</b></td>
          <td><asp:Label id="lblTitle" runat="server" Text='<%# Eval("Title") %>' /></td>
        </tr>
      </table>                 
    </ItemTemplate>
</asp:FormView>  

If we need to display a single item is obvious to use the ID field which we have by default in every List / Document library, so we can write

<SharePoint:SPDataSource runat="server" DataSourceMode="List" UseInternalName="true" 
style="margin-left:27pt;">selectcommand="<View><Query><Where><Eq><FieldRef Name='ID'/><Value Type='Counter'>{id}</Value></Eq></Where></Query><ViewFields><FieldRef Name='Title'/><FieldRef Name='Document_x0020_Type'/><FieldRef Name='ID'/></ViewFields></View>" id="spdatasource3">
style="margin-left:27pt;"><SelectParameters>
style="margin-left:54pt;"><asp:Parameter Name="ListID" DefaultValue="{FBCD7E0D-0697-4937-A42E-106DFA056E85}"/>
style="margin-left:54pt;"><asp:QueryStringParameter Name="id" QueryStringField="id"/> 
           </SelectParameters>              
</SharePoint:SPDataSource>

The field used for the filter must be present in the FieldRef list.

The ID field is a Counter type, and enclosed in {} we specify a parameter name that must be defined in the Select parameters.

Here we can do the mistake: we can be tempted to use for the QueryStringParameter a QueryStringField named “id” (or “ID”, but the casing is not important) as in the sample code but in this manner our code simply not work: we always get the Yellow Error Page that tell to us “No item exists…..may have been deleted or renamed by another user.” also we are sure that this “id” exists.

If we use another name for the parameter, as

<SharePoint:SPDataSource runat="server" DataSourceMode="List" UseInternalName="true" 
style="margin-left:27pt;">selectcommand="<View><Query><Where><Eq><FieldRef Name='ID'/><Value Type='Counter'>{baid}</Value></Eq></Where></Query><ViewFields><FieldRef Name='Title'/><FieldRef Name='Document_x0020_Type'/><FieldRef Name='ID'/></ViewFields></View>" id="spdatasource3">
style="margin-left:27pt;"><SelectParameters>
style="margin-left:54pt;"><asp:Parameter Name="ListID" DefaultValue="{FBCD7E0D-0697-4937-A42E-106DFA056E85}"/>
style="margin-left:54pt;"><asp:QueryStringParameter Name="baid" QueryStringField="baid"/> 
           </SelectParameters>              
</SharePoint:SPDataSource>

Our code works.

I have tried to insert a numeric field named “baid” (the same name of the querystring parameter) and still my code works, so it seems a specific problem related to the Counter field (“ID”).

From the other hand if we sort a ListViewWebPart we can notice that in the querystring are used the fieldnames as parameters, so i think that the better approach for a custom querystring is to avoid ID as name for sure (and in every case existing field names ) developing a naming scheme (with a particular prefix, for example) for custom querystring parameters.

Advertisements
Categories: SharePoint
%d bloggers like this: