From 3da15f2549a0a791043908f96c0887ebd8b47735 Mon Sep 17 00:00:00 2001 From: Pedro Monte Date: Tue, 15 Jul 2025 13:01:17 +0100 Subject: [PATCH 1/3] 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". --- .../Site/AddDataRowsToSiteTemplate.cs | 41 +++++++++---------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/src/Commands/Provisioning/Site/AddDataRowsToSiteTemplate.cs b/src/Commands/Provisioning/Site/AddDataRowsToSiteTemplate.cs index 47efdf2f0..e03581bfa 100644 --- a/src/Commands/Provisioning/Site/AddDataRowsToSiteTemplate.cs +++ b/src/Commands/Provisioning/Site/AddDataRowsToSiteTemplate.cs @@ -160,43 +160,42 @@ protected override void ExecuteCmdlet() } } } + + List fieldsToExport; + // Get the fieds to export if (Fields != null) { + fieldsToExport = []; foreach (var fieldName in Fields) { - Microsoft.SharePoint.Client.Field dataField = fieldCollection.FirstOrDefault(f => f.InternalName == fieldName); - + // Discard all fields readonly and unsupported field type + Microsoft.SharePoint.Client.Field dataField = fieldCollection.FirstOrDefault(f => f.InternalName == fieldName && !f.ReadOnlyField && !_unsupportedFieldTypes.Contains(f.FieldTypeKind)); if (dataField != null) { - var defaultFieldValue = GetFieldValueAsText(ClientContext.Web, listItem, dataField); - if (TokenizeUrls.IsPresent) - { - defaultFieldValue = Tokenize(defaultFieldValue, ClientContext.Web, ClientContext.Site); - } - - row.Values.Add(fieldName, defaultFieldValue); + fieldsToExport.Add(dataField); } } } else { - // All fields are added except readonly fields and unsupported field type - var fieldsToExport = fieldCollection.AsEnumerable() - .Where(f => !f.ReadOnlyField && !_unsupportedFieldTypes.Contains(f.FieldTypeKind)); - foreach (var field in fieldsToExport) + // Discard all fields readonly and unsupported field type + fieldsToExport = [.. fieldCollection.AsEnumerable().Where(f => !f.ReadOnlyField && !_unsupportedFieldTypes.Contains(f.FieldTypeKind))]; + } + + // Get validate field and get data + foreach (Microsoft.SharePoint.Client.Field field in fieldsToExport) + { + if (listItem.FieldExistsAndUsed(field.InternalName)) { - var fldKey = (from f in listItem.FieldValues.Keys where f == field.InternalName select f).FirstOrDefault(); - if (!string.IsNullOrEmpty(fldKey)) + var fieldValue = GetFieldValueAsText(ClientContext.Web, listItem, field); + if (TokenizeUrls.IsPresent) { - var fieldValue = GetFieldValueAsText(ClientContext.Web, listItem, field); - if (TokenizeUrls.IsPresent) - { - fieldValue = Tokenize(fieldValue, ClientContext.Web, ClientContext.Site); - } - row.Values.Add(field.InternalName, fieldValue); + fieldValue = Tokenize(fieldValue, ClientContext.Web, ClientContext.Site); } + row.Values.Add(field.InternalName, fieldValue); } } + rows.Add(row); } } From fdc35511b25118b9ffbfe383c80061f5e0a029e4 Mon Sep 17 00:00:00 2001 From: Pedro Monte Date: Mon, 21 Jul 2025 15:17:06 +0100 Subject: [PATCH 2/3] Shouldn't filter ReadOnlyField fields --- src/Commands/Provisioning/Site/AddDataRowsToSiteTemplate.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/Provisioning/Site/AddDataRowsToSiteTemplate.cs b/src/Commands/Provisioning/Site/AddDataRowsToSiteTemplate.cs index e03581bfa..b02a33807 100644 --- a/src/Commands/Provisioning/Site/AddDataRowsToSiteTemplate.cs +++ b/src/Commands/Provisioning/Site/AddDataRowsToSiteTemplate.cs @@ -169,7 +169,7 @@ protected override void ExecuteCmdlet() foreach (var fieldName in Fields) { // Discard all fields readonly and unsupported field type - Microsoft.SharePoint.Client.Field dataField = fieldCollection.FirstOrDefault(f => f.InternalName == fieldName && !f.ReadOnlyField && !_unsupportedFieldTypes.Contains(f.FieldTypeKind)); + Microsoft.SharePoint.Client.Field dataField = fieldCollection.FirstOrDefault(f => f.InternalName == fieldName && !_unsupportedFieldTypes.Contains(f.FieldTypeKind)); if (dataField != null) { fieldsToExport.Add(dataField); From 1309248f0933ccc795cc4702cafaa9b68174f2f1 Mon Sep 17 00:00:00 2001 From: Pedro Monte Date: Mon, 21 Jul 2025 17:11:46 +0100 Subject: [PATCH 3/3] Update comment --- src/Commands/Provisioning/Site/AddDataRowsToSiteTemplate.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/Provisioning/Site/AddDataRowsToSiteTemplate.cs b/src/Commands/Provisioning/Site/AddDataRowsToSiteTemplate.cs index b02a33807..6933b006a 100644 --- a/src/Commands/Provisioning/Site/AddDataRowsToSiteTemplate.cs +++ b/src/Commands/Provisioning/Site/AddDataRowsToSiteTemplate.cs @@ -168,7 +168,7 @@ protected override void ExecuteCmdlet() fieldsToExport = []; foreach (var fieldName in Fields) { - // Discard all fields readonly and unsupported field type + // Discard all fields unsupported Microsoft.SharePoint.Client.Field dataField = fieldCollection.FirstOrDefault(f => f.InternalName == fieldName && !_unsupportedFieldTypes.Contains(f.FieldTypeKind)); if (dataField != null) {