Subscribe:      
 

JQuery Image Slider for Announcement List in SharePoint 2007

Written By: Gayathri Narayanan -- 9/15/2011 -- join -- contribute -- (4091) comments -- printer friendly version

Rating: Rate --

Categories: Design, MOSS 2007, Navigation, Programming and Customizations, Visual Studio, Web Parts, WSS3

< Prev - 1 | 2 | 3 | 4 | 5 | 6 | - Next > | Become a paid author

Step 5:

Develop your HTML rendering web part with a VB file and a .dwp file. This method of developing the web part is pretty old and I donít think I should be including its detail here. It comes in handy for this image slider web part as we get good results when it is rendered in HTML.

You can first try this with some sample images in an html file storing the style sheet and the scripts in the same place as the HTML file before working on your web part. In that way, you can always change, test and change before you get your final HTML piece of code. I have my vb file with the name of AnnouncementSlider.vb. The code is below:

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Xml.Serialization
Imports Microsoft.SharePoint.WebPartPages
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Utilities
Imports System.Web.HttpRequest
Imports System.Security.Principal

<DefaultProperty("Text"), _
ToolboxData("<{0}:AnnouncementImageSlider runat=server></{0}:AnnouncementImageSlider>"), _
XmlRoot(Namespace:="Announcementslider")> _
Public Class AnnouncementImageSlider
    Inherits Microsoft.SharePoint.WebPartPages.WebPart

#Region "WebPart Properties"
    Protected MaxNo As String = "10" ' The maximum number of items to  
display 
    Protected ListName As String = "AnnouncementSlider"
    Private MaxNoDisplayName = "MaxItems"
    Private IDValue As String = "ID"
    Private Title As String
    Private Synopsis As String
    Private ImageURL As String
    Private ArticleURL As String
    Private CSSLink As String = "/_layouts/1033/Styles/Style.css" 

    'Binding Column with List AnnouncementSlider
    Private TitleValue As String = "Title"
    Private SynopsisValue As String = "Notes"
    Private ImageURLValue As String = "ArticleThumbnail"
    Private ArticleURLValue As String = "ArticleLink"
    Private CoverflowDisplay As Boolean = True


# End Region



#Region "ToolBox Settings"
' You can get the list name from edit--webpartproperties--toolbox

       <Bindable(True), _
   Category("Display Settings"), _
   FriendlyName("Name of List"), _
   DefaultValue(""), _
   Localizable(True), _
   Description("Enter the list name containing the items to display")> _
   Property ListName() As String
        Get
            Dim Name As String = CStr(ViewState("ListName"))

            If Name Is Nothing Then
                Return Me.ListName
            Else
                Return Name
            End If
        End Get

        Set(ByVal Value As String)
            Me.strListName = Value
        End Set
    End Property

'Get the maximum number of items to display in the image slider
   <Bindable(True), _
    Category("Display Settings"), _
    FriendlyName("Maximum Number Of Items To Display"), _
    DefaultValue(""), _
    Localizable(True), _
    Description("Enter the number of items to display")> _
    Property MaxItems() As String
        Get
            Dim MaxCount As String = CStr(ViewState("MaxItems"))

            If MaxCount Is Nothing Then
                Return Me.MaxNo
            Else
                Return MaxCount
            End If
        End Get

        Set(ByVal Value As String)
            Try
                ' Check if it's a valid no, if not, default to 10.
                Dim intTmp As Integer = CInt(Value)
                Me.MaxNo = Value

            Catch ex As Exception
                Me.MaxNo = 10     ' Default to 10.
                Err.Clear()
            End Try
        End Set
    End Property

   Protected Overrides Sub RenderWebPart(ByVal output As  
System.Web.UI.HtmlTextWriter)
        Dim myWeb As SPWeb = SPContext.Current.Web
        myWeb.AllowUnsafeUpdates = True

        Dim strSite As String                       ' The relative path to  
the current site
        Dim intLoop As Integer = 0                  ' There must be at list  
1 record to be displayed.
        
        Dim objWeb As SPWeb                         ' Object connecting to  
the SharePoint Site 
        Dim objList As SPList                       ' List object that store  
the records
        Dim objQuery As New SPQuery                 ' Query object to filter  
the records to be displayed
        Dim objArchiveQuery As New SPQuery                 ' Query object to  
filter the records to be displayed
        Dim objItemCol As SPListItemCollection      ' List item collection  
object containning the records
        Dim objItemColArchive As SPListItemCollection      ' List item  
collection object containning the records
        Dim objItem As SPListItem                   ' Each list item
        Dim objItemArchive As SPListItem

        Dim NowString As String  = System.DateTime.Now.ToString("yyyy-MM- 
dd")

        Dim HTMLstring As String = ""                  ' HTML string to be  
written
     

        'Get user name before doing impersonation
        Dim objUser As SPUser = SPContext.Current.Web.AllUsers 
(WindowsIdentity.GetCurrent.Name)
        Try
            Using New Unimpersonator()
                ' Connect to the site collection.
                If SourceURL = "" Or SourceURL = String.Empty Then

                    objWeb = SPContext.Current.Web      ' User did not  
specify the site path, assume current site.
                    While objWeb.IsRootWeb <> True
                        objWeb = objWeb.ParentWeb
                    End While

                    strSite = GetRelativePath(objWeb.Url)

                Else
                    ' User specified a site path, use the path instead.
                    objWeb = New SPSite(SourceURL).OpenWeb
                    strSite = GetRelativePath(CStr(SourceURL))
                End If
      

            ' Using application pool account
            Dim objUser1 As SPUser = SPContext.Current.Web.AllUsers 
(WindowsIdentity.GetCurrent.Name)
            Dim objSPToken As SPUserToken = objUser1.UserToken
            Dim objSite1 As New SPSite(objWeb.Url, objSPToken)
            Dim objWeb1 As SPWeb = objSite1.OpenWeb
                objWeb1.AllowUnsafeUpdates = True

                'Connect to the list object.
                objList = objWeb1.Lists(ListName)

                'Try to get Max Number of Items From "MaxItems" Item in Menu  
List
                For Each objItem In objList.Items
                    If objItem("Title") = TotalItemsToDisplayName Then
                        Me.MaxItems = objItem("ArticleThumbnail").ToString()
                    End If
                Next
                
            
                'Place your CAML Query here and create it using U2U editor
                objQuery.Query = "<Where><And><Leq><FieldRef  
Name='Publish_x0020_Date' /><Value Type='CustomDateTimeField'>" + NowString  
+ "</Value>"
                objQuery.Query += "</Leq><Eq><FieldRef  
Name='CoverFlowDisplay' /><Value  
Type='Boolean'>1</Value></Eq></And></Where><OrderBy>"
                objQuery.Query += "<FieldRef Name='Publish_x0020_Date'  
Ascending='False' /></OrderBy>"
                objItemCol = objList.GetItems(objQuery)
        'Now comes the HTML part
                HTMLString = "<link rel=""stylesheet"" type=""text/css""  
media=""all"" href=""" + CSSLink + """>"

                HTMLString += "<div id=""contentcontainer"">"
                HTMLString += "<div id=""slider-wrapper"">"
                HTMLString += "<div id=""slider"" class=""nivoSlider"">"
              

                'Loop through items in the collection
                'need to write separate loops for each image
                For Each objItem In objItemCol
                    ' Do not show anything/message if there are no active  
records.
                    If objItemCol.Count >= 1 Then
                        ' Exit if the no. of records displayed exceeds the  
max specified as MaxItems.
                        intLoop += 1
                        If intLoop > CInt(Me.MaxItems) Then
                            Exit For
                        End If
                        'Read value from 'Subject' Field 
                        If objItem.Fields.ContainsField(BindingColumnTitle)  
Then
                            If Not (IsNothing(objItem.Item 
(BindingColumnTitle))) Then
                                Dim Link As String

                                If objItem.Fields.ContainsField 
(ArticleURLValue) Then
                                    If Not (IsNothing(objItem.Item 
(ArticleURLValue))) Then
                                        Link = objItem 
(ArticleURLValue).ToString()
                                        Link = objItem 
("ArticleThumbnail").ToString()
                                    Else
                                        Link = "LinkNotFound.aspx"
                                    End If
                                Else
                                    Link = "LinkNotFound.aspx"
                                End If
                                If objItem.Fields.ContainsField 
(ImageURLValue) Then
                                    If Not (IsNothing(objItem.Item 
(ImageURLValue))) Then
                                        ImageURL = objItem 
(ImageURLValue).ToString()
                                    End If
                                End If
                                If objItem.Fields.ContainsField 
(SynopsisValue) Then
                                    If Not (IsNothing(objItem.Item 
(SynopsisValue))) Then
                                        Synopsis = objItem 
(SynopsisValue).ToString()
                                    End If
                                End If
                                If objItem.Fields.ContainsField 
(ThumbnailValue) Then
                                    If Not (IsNothing(objItem.Item 
(ThumbnailValue))) Then
                                        Thumbnail = objItem 
(ThumbnailValue).ToString()
                                    End If
                                End If
                                Dim ID As String
                                Dim HTMLString As String

                                If objItem.Fields.ContainsField(IDValue)  
Then
                                    If Not (IsNothing(objItem.Item 
(IDValue))) Then
                                        ID = objItem(IDValue).ToString()
                                        HTMLString = "#htmlcaption" + ID +  
""
                                    End If
                                End If
                                If objItem.Fields.ContainsField(TitleValue)  
Then
                                    If Not (IsNothing(objItem.Item 
(TitleValue))) Then
                                        Title = objItem 
(TitleValue).ToString()
                                    End If
                                End If
                                ' actual image div
                                HTMLString += "<a href=""" + Link + """  
target = ""_blank"">"
                                HTMLString += "<img src=""" + ImageURL + """  
width=""510px"" height=""210px"" alt="""" title=""#htmlcaption" + ID + """  
/></a>"
                            End If
                        End If
                    End If
                Next
                HTMLString += "</div>"
                intLoop = 0
                For Each objItem In objItemCol
                    ' Do not show anything/message if there are no active  
records.
                    If objItemCol.Count >= 1 Then
                        ' Exit if the no. of records displayed exceeds the  
max allowed.

                        intLoop += 1
                        If intLoop > CInt(Me.MaxItems) Then
                            Exit For
                        End If
                        'Read value from 'Subject' Field 
                        If objItem.Fields.ContainsField(BindingColumnTitle)  
Then
                            If Not (IsNothing(objItem.Item 
(BindingColumnTitle))) Then
                                Dim Link As String

                                If objItem.Fields.ContainsField 
(ArticleURLValue) Then
                                    If Not (IsNothing(objItem.Item 
(ArticleURLValue))) Then
                                       Link = objItem 
(ArticleURLValue).ToString()
                                    Else
                                       Link = "LinkNotFound.aspx"
                                    End If
                                Else
                                    Link = "LinkNotFound.aspx"
                                End If
                                If objItem.Fields.ContainsField 
(ImageURLValue) Then
                                    If Not (IsNothing(objItem.Item 
(ImageURLValue))) Then
                                        ImageURL = objItem 
(ImageURLValue).ToString()
                                    End If
                                End If
                                If objItem.Fields.ContainsField 
(SynopsisValue) Then
                                    If Not (IsNothing(objItem.Item 
(SynopsisValue))) Then
                                        Synopsis = objItem 
(SynopsisValue).ToString()
                                    End If
                                End If
                                If objItem.Fields.ContainsField 
(ThumbnailValue) Then
                                    If Not (IsNothing(objItem.Item 
(ThumbnailValue))) Then
                                        Thumbnail = objItem 
(ThumbnailValue).ToString()
                                    End If
                                End If
                                Dim ID As String
                                Dim HTMLString As String

                                If objItem.Fields.ContainsField(IDValue)  
Then
                                    If Not (IsNothing(objItem.Item 
(IDValue))) Then
                                        ID = objItem(IDValue).ToString()
                                        HTMLString = "#htmlcaption" + ID +  
""
                                    End If
                                End If
                                If objItem.Fields.ContainsField(TitleValue)  
Then
                                    If Not (IsNothing(objItem.Item 
(TitleValue))) Then
                                        Title = objItem 
(TitleValue).ToString()
                                    End If
                                End If
                                'caption div
                                HTMLString += "<div id=""htmlcaption" + ID + """ class=""nivo-html-caption"">"
                                HTMLString += "<h4>" + Title + "</h4>"
                                HTMLString += "<div id=""description"">" +  
Synopsis + "</div>"
                                HTMLString += "</div>"
                            End If
                        End If
                    End If
                Next
              
                HTMLString += "</div>"
                HTMLString += "</div>" 'content container div closing
                
                'Write HTML to WebPart
                If HTMLString <> "" And HTMLString <> String.Empty Then  
output.Write(HTMLString)

            End Using

        Catch ex As Exception
            'Write to Event Log 
			' This error log can be written anywhere you specify  
for your future checks
            WriteLog("ERROR: " & ex.Message & vbCrLf & ex.StackTrace)
        End Try

    End Sub
    

< Prev - 1 | 2 | 3 | 4 | 5 | 6 | - Next >



Learn more about SharePoint



Sponsor Information




Copyright (c) 2010-2017 Edgewood Solutions, LLC All rights reserved
privacy | disclaimer | copyright | advertise | contribute | feedback | about
Some names and products listed are the registered trademarks of their respective owners.


MSSharePointTips.com | MSSQLTips.com