Retrieve Value for a given Key from a list using CAML

Public Shared Function GetSetting(ByVal Key As string, ByVal SiteURL As String, Byval ListName as String)

    Using oSite As SPSite = New SPSite(SiteURL) 

        Using oWeb As SPWeb = oSite.OpenWeb()

            Dim oQry As SPQuery = New SPQuery
            oQry.Query = "<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" & Key & "</Value></Eq></Where>"
            oQry.RowLimit = 100

            Dim oList As SPList = oWeb.Lists(ListName)
            Dim oItems As SPListItemCollection = oList.GetItems(oQry)

            If oItems IsNot Nothing AndAlso oItems.Count = 1 Then
                Dim sValue As String = System.Web.HttpUtility.HtmlDecode(oItems(0).GetFormattedValue("Value"))
                Return sValue
            Else
                Throw New Exception(String.Format("A value for key:{0} was not found in the {1} List on site:{2}", Key,ListName, SiteURL))
            End If

        End Using

    End Using

End Function
This entry was posted in SharePoint and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>