views:

1260

answers:

2

How can i create a sharepoint webpart that accesses a List, and that can have CAML applied to it.

The CAML is needed to display return only list items that have the field with "Position" > 0

i also need the webpart to have xslt applied to that.

+3  A: 

You need to add the Content Query Web Part to your page (note: requires MOSS not free WSS). This allows you to query your data and apply an XSL transform to it.

The web part allows you to query a particular site collection, web or list. You can then set parameters to return data of a certain type and apply filters, sorting and grouping. You can also choose how you wish the data to be displayed which appears to the end user as a dropdown list of options. Each of these options is powered by an XSL transform.

This blog post by Heather Solomon is one of the best resources to help you get started with how to create your own transform and configure the CQWP. It also explains how to ensure all the fields you need are passed through to the XSLT (by default this only happens for a small subset).

Update:

To only return list items where the field "Position" > 0, it is simplest to do this within XSLT as well. You must have added the Position field to CommonViewFields so that it gets passed through to the XSLT. Then in your custom item style (in ItemStyle.xsl if you follow Heather's post), add the following:

<xsl:if test="@Position &gt; 0">
  <!-- Display desired row output -->
</xsl:if>

This implicitly ignores when "Position" <= 0.

Alex Angas
+1  A: 

I agree with Alex, that the Content Query Web Part (CQWP) is the way to go if possible.

However, if you want to get into the code you can do something like the following. The rough part is getting it into XML, although there may be an easy way to get it into some non-custom (albeit ugly) form of XML.

SPList list = web.Lists["My List Name"];
SPView view = list.Views["My View Name"];  // This view would define Postion > 0
SPQuery query = new SPQuery(view);
SPListItemCollection items = list.GetItems(query);

// Iterate through results and generate XML

If you don't want to use an existing view, you will need to set up the SPQuery object by hand; setting its ViewFields, Query, and RowLimit at a minimum. You can use the a CAML Query Tool to help you with this.

Kirk Liemohn