检索元数据列字段 [英] Retrieve metadata column fields

查看:78
本文介绍了检索元数据列字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以编程方式检索Sharepoint文档库中的列,以便在Sharepoint外部设置文件属性.

I need to programmatically retrieve the columns in a Sharepoint document library, in order to set file properties externally to Sharepoint.

我发现只要您已经知道列名即可设置元数据属性并不困难,我不能指望用户自行输入.

I've found that setting the metadata property is not hard as long as you already know the name of the column, which I cannot expect users to input themselves.

由于似乎不可能通过Sharepoint Web服务执行此操作,因此我创建了自己的自定义Web服务,因此可以访问客户端对象模型.

As it does not seem possible to do this through the Sharepoint Web Services I have created my own custom web service so I have access to the Client Object Model.

使用此代码,我可以检索创建的自定义列,但是,我无法区分在项目属性部分(如上图)中可编辑的列和不能进行编辑的列.

Using this code I am able to retrieve the custom columns I have created, however I am not able to distinguish between the ones editable in the item properties section (picture above) and those which aren't.

SPList list = web.Lists[specificList];
foreach (SPField field in list.Fields)
{
    if (!field.Hidden)
    {
        var title = field.Title;
        var description = field.Description;
        var parentList = field.ParentList;

        var references = field.FieldReferences; // contains names of fields referenced in computed fields

        if (references != null)
        {
            foreach (string reference in references)
            {
                var test = parentList.Fields.GetField(reference);
            }
        }
    }
}

我得到了一些额外的属性,例如:

I get extra properties such as:

  • 复制源
  • 内容类型
  • 签出至
  • 已签入评论
  • 类型
  • 文件
  • 大小
  • 编辑
  • 版本
  • 源版本
  • 源名称
  • Copy Source
  • Content Type
  • Checked Out To
  • Checked In Comment
  • Type
  • File
  • Size
  • Edit
  • Version
  • Source Version
  • Source Name

我也尝试过从SPFolder项中检索列字段,但同样会返回许多额外的属性,并且过滤性甚至更低.

I have also tried retrieving the column fields from the SPFolder item, but again this returns many extra properties and is even less filterable.

foreach (SPListItem folderItem in list.Folders)
{
    SPFolder folder = folderItem.Folder;
    System.Collections.Hashtable oHashtable = folder.Properties;
    System.Collections.ICollection collKeys = oHashtable.Keys;

    foreach (var key in collKeys)
    {
        string keyName = key.ToString();
    }
}

是否有检索我需要的列字段的标准方法?还是我必须手动排除默认值,例如签出到"?

Is there a standard way to retrieve the column fields I need? Or will I have to manually exclude the defaults ones such as "Checked out to"?

推荐答案

首先,您必须知道要查看的表单.是EditForm还是NewForm?

First you have to know which form you are viewing. Is it the EditForm or NewForm?

您可以通过获取ContentType的字段来过滤在特定表单上可见的列,然后检查它们是否在NewForm(或任何表单)上显示:

You can filter the columns visible on a specific form by getting the fields of the ContentType and then check if they are getting displayed on the NewForm (or whatever form):

SPList list = web.Lists[specificList];
var contentType = list.ContentTypes[0]; // Select first contenttype. Change this if you need a different contentType
foreach (SPField field in contentType.Fields)
{
    if (!field.Hidden 
        && (field.ShowInEditForm == null
            || !field.ShowInEditForm.Value)) // Replace ShowInEditForm with the form you need
    {
        var title = field.Title;
        var description = field.Description;
        var parentList = field.ParentList;

        var references = field.FieldReferences; // contains names of fields referenced in computed fields

        if (references != null)
        {
            foreach (string reference in references)
            {
                var test = parentList.Fields.GetField(reference);
            }
        }
    }
}

这篇关于检索元数据列字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆