Skip to content

Commit 59f3f28

Browse files
PedroMordePPedro Montegautamdsheth
authored
The AddDataRowsToSiteTemplate fields requested should also be validated if can be retrieved (#5020)
* The fields requested should also be validated if can be retrieved. Fix issue "The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested". * Shouldn't filter ReadOnlyField fields * Update comment --------- Co-authored-by: Pedro Monte <[email protected]> Co-authored-by: Gautam Sheth <[email protected]>
1 parent 7086fb7 commit 59f3f28

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

src/Commands/Provisioning/Site/AddDataRowsToSiteTemplate.cs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -160,43 +160,42 @@ protected override void ExecuteCmdlet()
160160
}
161161
}
162162
}
163+
164+
List<Microsoft.SharePoint.Client.Field> fieldsToExport;
165+
// Get the fieds to export
163166
if (Fields != null)
164167
{
168+
fieldsToExport = [];
165169
foreach (var fieldName in Fields)
166170
{
167-
Microsoft.SharePoint.Client.Field dataField = fieldCollection.FirstOrDefault(f => f.InternalName == fieldName);
168-
171+
// Discard all fields unsupported
172+
Microsoft.SharePoint.Client.Field dataField = fieldCollection.FirstOrDefault(f => f.InternalName == fieldName && !_unsupportedFieldTypes.Contains(f.FieldTypeKind));
169173
if (dataField != null)
170174
{
171-
var defaultFieldValue = GetFieldValueAsText(ClientContext.Web, listItem, dataField);
172-
if (TokenizeUrls.IsPresent)
173-
{
174-
defaultFieldValue = Tokenize(defaultFieldValue, ClientContext.Web, ClientContext.Site);
175-
}
176-
177-
row.Values.Add(fieldName, defaultFieldValue);
175+
fieldsToExport.Add(dataField);
178176
}
179177
}
180178
}
181179
else
182180
{
183-
// All fields are added except readonly fields and unsupported field type
184-
var fieldsToExport = fieldCollection.AsEnumerable()
185-
.Where(f => !f.ReadOnlyField && !_unsupportedFieldTypes.Contains(f.FieldTypeKind));
186-
foreach (var field in fieldsToExport)
181+
// Discard all fields readonly and unsupported field type
182+
fieldsToExport = [.. fieldCollection.AsEnumerable().Where(f => !f.ReadOnlyField && !_unsupportedFieldTypes.Contains(f.FieldTypeKind))];
183+
}
184+
185+
// Get validate field and get data
186+
foreach (Microsoft.SharePoint.Client.Field field in fieldsToExport)
187+
{
188+
if (listItem.FieldExistsAndUsed(field.InternalName))
187189
{
188-
var fldKey = (from f in listItem.FieldValues.Keys where f == field.InternalName select f).FirstOrDefault();
189-
if (!string.IsNullOrEmpty(fldKey))
190+
var fieldValue = GetFieldValueAsText(ClientContext.Web, listItem, field);
191+
if (TokenizeUrls.IsPresent)
190192
{
191-
var fieldValue = GetFieldValueAsText(ClientContext.Web, listItem, field);
192-
if (TokenizeUrls.IsPresent)
193-
{
194-
fieldValue = Tokenize(fieldValue, ClientContext.Web, ClientContext.Site);
195-
}
196-
row.Values.Add(field.InternalName, fieldValue);
193+
fieldValue = Tokenize(fieldValue, ClientContext.Web, ClientContext.Site);
197194
}
195+
row.Values.Add(field.InternalName, fieldValue);
198196
}
199197
}
198+
200199
rows.Add(row);
201200
}
202201
}

0 commit comments

Comments
 (0)