Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

My ideal situation is that I can create a web part with a dropdown. Each choice in the dropdown will correspond to a view created on a list. Once the user selects an item from the dropdown, my web part will get only the items that match that view's filters returned. I'll then calculate based on that and display results accordingly.

example: User creates views for 2010 new purchases 2009 new purchases. I'd have a dropdown on a web part with 2 choices, then user selects 1 and only the matching items are returned. I'd then calculate based on the subset returned and return my results.

EDIT:

What I am trying to accomplish is a workaround for calculating column totals on 'Calculated Fields'. A solution I saw posted earlier had a top comment of 'this does not work with currency', which is unacceptable.

I have 1 big list and maybe 20 views saved. We have price information calculated based on several columns. One of the columns, "current value", will not total because it is calculated.

Sure, I can write CAML for each specific query, but what I'm wondering is if there's a way that I can use an existing view / the CAML that view uses, so that my list's users can create a view and save it, and then go to this web part and select that view to get the calculated total.

share|improve this question
add comment

2 Answers

(Separate answer, as it's a different solution to my other answer in this page)

You can look at SPList.Views to get a collection of all the views attached to a list.

You can populate your dropdown with the Names of all the items in the SPList.Views collection. Then you can attach an event handler to SelectionChanged (or whatever it's called, something similar, IntelliSense/MSDN will tell you what it's really called) to grab out the SPView item that you need from the view collection.

Once you have that View, I think you can grab the CAML query (SPView.Query) from the view, pass it into an SPListItemCollection (SPList.GetItems()), and bind it to the datasource of an SPGridView.

Think that should do it for you.

share|improve this answer
    
this is what i was hoping to hear. will report back soon. thanks! –  user2207 Jan 18 '11 at 22:20
add comment

Views can be accessed via the object model but they need to be persisted to the database before you can load a List with it.

Essentially, this is a fairly roundabout way of doing it - you could instead use your Web Part to generate an SPQuery object, which has the CAML necessary for your filter, and then you use SPList.GetItems method, passing in the Query object, and using the return collection in an SPGridView for rendering.

Info:

SPQuery: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spquery.aspx

SPList.GetItems: http://msdn.microsoft.com/en-us/library/ms457534.aspx

SPGridView: http://msdn.microsoft.com/en-GB/library/microsoft.sharepoint.webcontrols.spgridview(v=office.12).aspx

SPView: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spview.query.aspx

share|improve this answer
    
hi, thanks for your help. please check out my edit. my main point is, users will be creating views on this list so instead of my creating custom CAML can I just access views that have already been created and use them to filter results? Thanks! –  user2207 Jan 18 '11 at 22:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.