Exemplo n.º 1
0
        private Model.File RetrieveFieldValues(Web web, File file, Model.File homeFile)
        {
            var listItem = file.EnsureProperty(f => f.ListItemAllFields);

            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",
                "_ModerationStatus"
            };

            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;
                    if (field.TypeAsString == "URL")
                    {
                        value = Tokenize(fieldValuesAsText[fieldValue.Key], web.Url);
                    }
                    else {
                        value = Tokenize(fieldValue.Value.ToString(), web.Url);
                    }

                    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);
                        }
                    }
                    homeFile.Properties.Add(fieldValue.Key, value);
                }
            }

            return homeFile;
        }