Exemple #1
0
        private OfficeDevPnP.Core.Framework.Provisioning.Model.File RetrieveFieldValues(Web web,
                                                                                        Microsoft.SharePoint.Client.File file, OfficeDevPnP.Core.Framework.Provisioning.Model.File modelFile)
        {
            try
            {
                Microsoft.SharePoint.Client.ListItem listItem = null;
                try
                {
                    listItem = file.EnsureProperty(f => f.ListItemAllFields);
                }
                catch
                {
                }

                if (listItem != null)
                {
                    var list = listItem.ParentList;

                    var fields = list.Fields;
                    web.Context.Load(fields,
                                     fs => fs.IncludeWithDefaultProperties(f => f.TypeAsString, f => f.InternalName, f => f.Title));
                    web.Context.ExecuteQueryRetry();

                    var fieldValues = listItem.FieldValues;

                    var fieldValuesAsText = listItem.EnsureProperty(li => li.FieldValuesAsText).FieldValues;

                    var fieldstoExclude = new[]
                    {
                        "ID",
                        "GUID",
                        "Author",
                        "Editor",
                        "FileLeafRef",
                        "FileRef",
                        "File_x0020_Type",
                        "Modified_x0020_By",
                        "Created_x0020_By",
                        "Created",
                        "Modified",
                        "FileDirRef",
                        "Last_x0020_Modified",
                        "Created_x0020_Date",
                        "File_x0020_Size",
                        "FSObjType",
                        "IsCheckedoutToLocal",
                        "ScopeId",
                        "UniqueId",
                        "VirusStatus",
                        "_Level",
                        "_IsCurrentVersion",
                        "ItemChildCount",
                        "FolderChildCount",
                        "SMLastModifiedDate",
                        "owshiddenversion",
                        "_UIVersion",
                        "_UIVersionString",
                        "Order",
                        "WorkflowVersion",
                        "DocConcurrencyNumber",
                        "ParentUniqueId",
                        "CheckedOutUserId",
                        "SyncClientId",
                        "CheckedOutTitle",
                        "SMTotalSize",
                        "SMTotalFileStreamSize",
                        "SMTotalFileCount",
                        "ParentVersionString",
                        "ParentLeafName",
                        "SortBehavior",
                        "StreamHash",
                        "TaxCatchAll",
                        "TaxCatchAllLabel",
                        "_ModerationStatus",
                        //"HtmlDesignAssociated",
                        //"HtmlDesignStatusAndPreview",
                        "MetaInfo",
                        "CheckoutUser",
                        "NoExecute",
                        "_HasCopyDestinations",
                        "ContentVersion",
                        "UIVersion",
                    };

                    foreach (var fieldValue in fieldValues.Where(f => !fieldstoExclude.Contains(f.Key)))
                    {
                        if (fieldValue.Value != null && !string.IsNullOrEmpty(fieldValue.Value.ToString()))
                        {
                            var field = fields.FirstOrDefault(fs => fs.InternalName == fieldValue.Key);

                            string value = string.Empty;

                            switch (field.TypeAsString)
                            {
                            case "URL":
                                value = Tokenize(fieldValuesAsText[fieldValue.Key], web.Url, web);
                                break;

                            case "User":
                                var userFieldValue = fieldValue.Value as Microsoft.SharePoint.Client.FieldUserValue;
                                if (userFieldValue != null)
                                {
#if !ONPREMISES
                                    value = userFieldValue.Email;
#else
                                    value = userFieldValue.LookupValue;
#endif
                                }
                                break;

                            case "LookupMulti":
                                var lookupFieldValue =
                                    fieldValue.Value as Microsoft.SharePoint.Client.FieldLookupValue[];
                                if (lookupFieldValue != null)
                                {
                                    value = Tokenize(JsonUtility.Serialize(lookupFieldValue), web.Url);
                                }
                                break;

                            case "TaxonomyFieldType":
                                var taxonomyFieldValue =
                                    fieldValue.Value as Microsoft.SharePoint.Client.Taxonomy.TaxonomyFieldValue;
                                if (taxonomyFieldValue != null)
                                {
                                    value = Tokenize(JsonUtility.Serialize(taxonomyFieldValue), web.Url);
                                }
                                break;

                            case "TaxonomyFieldTypeMulti":
                                var taxonomyMultiFieldValue =
                                    fieldValue.Value as
                                    Microsoft.SharePoint.Client.Taxonomy.TaxonomyFieldValueCollection;
                                if (taxonomyMultiFieldValue != null)
                                {
                                    value = Tokenize(JsonUtility.Serialize(taxonomyMultiFieldValue), web.Url);
                                }
                                break;

                            case "ContentTypeIdFieldType":
                            default:
                                value = Tokenize(fieldValue.Value.ToString(), web.Url, web);
                                break;
                            }

                            if (fieldValue.Key == "ContentTypeId")
                            {
                                // Replace the content typeid with a token
                                var ct = list.GetContentTypeById(value);
                                if (ct != null)
                                {
                                    value = string.Format("{{contenttypeid:{0}}}", ct.Name);
                                }
                            }

                            // We process real values only
                            if (value != null && !String.IsNullOrEmpty(value) && value != "[]")
                            {
                                modelFile.Properties.Add(fieldValue.Key, value);
                            }
                        }
                    }
                }

                return(modelFile);
            }

            catch (Exception ex)
            {
                throw ex;
            }
        }