For a recent project I created a Cover Story User Control and Web Part that allowed administrators to post content in the form of a picture, author information, story and link to the full story.
Not caring much for the standard development framework I use an alternate method that involves creating a web control in Visual Studio and then loading the control in the Web Part.
The following snippet explains how:
Protected Overrides Sub createchildcontrols()
Try
Me.CoverStory = Page.LoadControl(“/controls/coverstory/CoverStory.ascx”)
Controls.Add(CoverStory)
Catch ex As Exception
Dim objStreamWriter As StreamWriter
objStreamWriter = File.CreateText(“c:\CoverStoryWPV1_Error_Log.txt”)
objStreamWriter.WriteLine(Date.Now.ToLongDateString)
objStreamWriter.WriteLine(ex.Source.ToString)
objStreamWriter.WriteLine(ex.Message.ToString)
objStreamWriter.WriteLine(ex.StackTrace.ToString)
objStreamWriter.Close()
End Try
End Sub
To help troubleshoot I use the following code:
Protected Overrides Sub RenderWebPart(ByVal output As System.Web.UI.HtmlTextWriter)
Try
ensurechildcontrols()
output.Write(“<div style=’PADDING: 2px;’ width=’99%’>”)
alertsv1.RenderControl(output)
output.Write(“</div>”)
Catch ex As Exception
Dim objStreamWriter As StreamWriter
objStreamWriter = File.CreateText(“c:\coverStory_Error_Log.txt”)
objStreamWriter.WriteLine(Date.Now.ToLongDateString)
objStreamWriter.WriteLine(ex.Source.ToString)
objStreamWriter.WriteLine(ex.Message.ToString)
objStreamWriter.WriteLine(ex.StackTrace.ToString)
objStreamWriter.Close()
End Try
End Sub
The Web Part allowed person with administrator rights to create, update and select the Cover Stories. The User Control contains two panels; 1) Story viewing panel with picture and text and 2) a story editing form. The Web Part simply hosts the User Control and sets values based on whether the user has administrative rights. The User Control Story information is stored in an SQL Server Table, several text fields as follows:
  • Story Heading
  • Link to image
  • Author name
  • Author Email
  • Date of publishing
  • Story summary
  • Story main body
  • Link to full story

Web Part properties are made use of to set the default story for display, the story heading is used to instruct the User Control (via the Web part) to display the Administrators story of choice.

The Web Part helps to spice up the pages with SharePoint Portal Server or Windows SharePoint Services by adding CMS like functionality (without the complexity and cost).

Advertisements