· 8 years ago · Aug 21, 2018, 01:58 AM
1best way to represent table like data in collections
2''' <summary>
3''' creates a comma seperated string of all data
4''' </summary>
5''' <returns></returns>
6''' <remarks></remarks>
7Public Overrides Function ToString() As String
8 ' some code to create a comma separated string of all data contained in the sorted dictionary
9End Function
10
11''' <summary>
12''' This class respresents a listItemCollection as provided in the constructor as table like data with headers and rows
13''' </summary>
14''' <remarks></remarks>
15Public Class MetadataValuesFactory
16 Private _fieldsNotToStore As List(Of String) = ConfigurationService.FieldValuesNotToStore
17 Private _headers As MetaDataValue = New MetaDataValue()
18 Private _rows As IList(Of MetaDataValue) = New List(Of MetaDataValue)
19
20 ''' <summary>
21 ''' returns a metaDAtvaleu object that contains all header values
22 ''' </summary>
23 ''' <returns></returns>
24 ''' <remarks></remarks>
25 Public Function GetHeaders() As MetaDataValue
26 Dim result As MetaDataValue = New MetaDataValue()
27 Dim displaynames = ConfigurationService.FieldValueDisplayNames
28 For Each item In Me._headers
29 Dim displayname = String.Empty
30 If (displaynames.ContainsKey(item.Value)) Then
31 result.Add(item.Key, displaynames.Item(item.Value))
32 Else
33 result.Add(item.Key, item.Value)
34 End If
35 Next
36 Return result
37 End Function
38
39 ''' <summary>
40 ''' Returns all rows that represent the values
41 ''' </summary>
42 ''' <returns></returns>
43 ''' <remarks></remarks>
44 Public Function GetRows() As IList(Of MetaDataValue)
45 Return _rows
46 End Function
47
48 ''' <summary>
49 ''' Creates a new metadatavaluesstore with the specified values
50 ''' </summary>
51 ''' <param name="listItems"></param>
52 ''' <remarks></remarks>
53 Sub New(listItems As ListItemCollection)
54 'first store all values and make sure keys and values are matched
55 Dim totalListItems = listItems.Count
56
57 For index As Integer = 0 To totalListItems - 1
58 Dim valuesToStore As MetaDataValue = New MetaDataValue()
59 Dim item As SPClient.ListItem = listItems(index)
60 Dim fieldValues = item.FieldValues
61
62 For Each fieldValue In fieldValues
63 If (Not _fieldsNotToStore.Contains(fieldValue.Key)) Then 'If it is not in this collection is must be stored
64 'Get index of field in _headers
65 Dim headerindex As Integer = _headers.Values.ToList().IndexOf(fieldValue.Key.ToString)
66 If (headerindex = -1) Then 'If fieldValue.Key is already in the array it doesn't need to be stored again (-1 = no index found)
67 'If Not exists then store header/key in _headers ánd set previous index to index of new headers value
68 headerindex = _headers.Count '' Add new header
69 _headers.Add(headerindex, fieldValue.Key.ToString)
70 End If
71 'add value to valuesstore
72 Dim valueToStore As String
73 If (fieldValue.Value Is Nothing) Then
74 valueToStore = String.Empty
75 Else
76 valueToStore = fieldValue.Value.ToString
77 End If
78 valuesToStore.Add(headerindex, valueToStore)
79 End If
80 Next
81 _rows.Add(valuesToStore)
82 Next
83 End Sub
84
85End Class
86
87metaDataStreamWriter.WriteLine(metadataFactory.GetHeaders.ToString)
88For Each storeValue In metadataFactory.GetRows
89 metaDataStreamWriter.WriteLine(storeValue.ToString)
90Next