In our previous post, we saw how to build user controls and reference the same from our Home Page layout. In this post lets see how we can retrieve the Latest News from a SharePoint List rather than static HTML code.
Jet30 publishes their latest news daily and wishes to show them in the Home Page. The Latest News section in our home page reflected this daily news section.
Jet30 wants their content authors to manage the daily news and hence they choose to create a new Announcements List called LatestNews where they can manage (add/edit/delete) the daily news. The Latest News section should only display the news of that day.
As you can see in the above screenshot, we have 2 items which expires today (October 6th) and the other which has expired (October 5th).
Injecting the SharePoint Logic into our user control
We now know our exact job – Query the LatestNews list for the items which expires today
The LatestNews user control now changes to:
We will use a Label to update today’s date and a Repeater to display today’s latest news.
Below is the updated .ascx code:
And below is the code behind:
Pretty basic ASP.NET and SharePoint code 🙂
The AddTodaysDate updates the label with today’s date and AddTodaysNews queries the SharePoint list LatestNews items which expires today.
Build the code and deploy to the site.
You should still see the same home page, the only difference this time being that Latest News section is now content driven 🙂
What about Web Parts?
You might be wondering why I haven’t used Web Parts and chosen to build User Controls. I think this discussion would take us away from our current trending topic, but do hold on, I will come up with a separate post explaining why I choose to build User Controls and not Web Parts in the WCM world.
In our next post, we will see the necessity of building custom field controls and build one!
For now, you can download the source code from here.
If you don’t have SPVisualDev, build a WSP package and deploy it to test.
Remember that, until we have our own site definition, we are not done yet and things will be little messy 🙂