Sunday, June 14, 2009

SPDataSource another powerful sharepoint control

Scenario:
I think I mentioned about this one in my previous blogs but I binged and couldn't find anything. So thought of writing about it.

We have seen many powerful controls of sharepoint and one of them is SPDataSource

I am sure you heard about SqlDataSource data source control which represents data in an SQL relational database to data-bound controls. You can use the SqlDataSource control in conjunction with a data-bound control to retrieve data from a relational database and to display, edit, and sort data on a Web page with little or no code.

On the same lines , SPDataSource data source control represents Windows SharePoint Services data to data-bound controls. You can use the control in conjunction with a data-bound control to retrieve data from lists, list items, and Web sites and to display, edit, and modify data with little or no code.

-- Like other data source controls,SPDataSource can also be used in UI and Code behind.
-- Unlike other data source controls, the SPDataSource control is associated with only one view. The GetViewNames method always returns a collection that contains the name of just one data view.

Example:
Here's a quick view to get datasource instance from Announcement List

Code:

  var datasource = new SPDataSource
{
DataSourceMode = SPDataSourceMode.List,
List = SPContext.Current.Web.Lists["Announcements"]
};
Article:
MSDN SPDataSource , SPDataSource every sharepoint developer friend 1 , SPDataSource every sharepoint developer friend 2 , SPdatasource-every-developer-friend , spdatasource-mystery-modes-webs-listoflists , Another very good example


Follow me on Twitter

5 comments:

Anonymous,  September 25, 2009 at 10:14 AM  

Do you know if there is a way to filter a SPDATASource on a column that is set to people/groups by [ME]

I can't seem to get it to work.

Sandeep September 25, 2009 at 10:29 AM  

I think thats possible, u need to get the SPQuery with filtered CAML to return SPListItemCollection items and tie it to ur DataSource

Anonymous,  September 25, 2009 at 11:29 AM  

thanks for responding,

I'm a little unsure tho how to do this tho.

Is this something I can do within Designer?

Sandeep September 25, 2009 at 11:42 AM  

yes u can do it using SPD also.. SPDataSource support SelectParameters ... check this article to get an idea

http://snahta.blogspot.com/2008/09/listid-fix-for-listview-webpart.html

Anonymous,  September 25, 2009 at 12:28 PM  

I read the other article, and if If understanding it right.

I can change the listid to list name?

So in my spdatasource I've done this anywhere it had listID. My list name is Category

Name="ListName" DefaultValue="Category"

but when I click on configure datasource, all the options are shaded out.

am I on the right track?