SPField的几种name的释疑

编写SharePoint Object Model代码时, 经常要用到SPField. 这个SPField有不少名字, 让人很容易混淆.

  • Display Name
  • Internal Name
  • Name
  • ColName
  • StaticName

下面就让我们总结一下吧.

 

你可以使用下面的代码把列表的FieldSchema捞出来看一下.

using (SPSite site = new SPSite("http://servername/sites/testsite"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPList splist = web.Lists["issue tracking1"];
        FileStream fs = new FileStream(@"C:\temp\listschema.xml", FileMode.Create);
        StreamWriter sw = new StreamWriter(fs);
        sw.Write(splist.Fields.SchemaXml);

    }
}

 

这个文件比较大, 我就抽选两个Field来作为例子:

<Field ID="{1df5e554-ec7e-46a6-901d-d85a3881cb18}" ColName="tp_Author" 
       RowOrdinal="0" ReadOnly="TRUE" Type="User" List="UserInfo" 
       Name="Author" DisplayName="Created By" 
       SourceID="http://schemas.microsoft.com/sharepoint/v3" 
       StaticName="Author" FromBaseType="TRUE"/>

<Field ID="{3881510a-4e4a-4ee8-b102-8ee8e2d0dd4b}" ColName="tp_CheckoutUserId" 
       RowOrdinal="0" ReadOnly="TRUE" Type="User" List="UserInfo" 
       Name="CheckoutUser" DisplaceOnUpgrade="TRUE" 
       DisplayName="Checked Out To" 
       SourceID="http://schemas.microsoft.com/sharepoint/v3" 
       StaticName="CheckoutUser" FromBaseType="TRUE"/>

 

这里可以看到这个field的全部信息, 如果你对某个列表的field的某个名字不清楚, 可以使用捞xml的大招来释疑.

我写过一篇文章, 叫做SPQuery 在引用field的时候要用internal name, 那么SPQuery用到的Internal Name是哪一个呢?

 

Name = Internal NameDisplay NameColNameStaticName
SPQuery使用的名字界面上显示的名字数据库中对应的列名不知道做什么的, 看了好几个都与Name, Internal Name一样.
CheckoutUserChecked Out Totp_CheckoutUserIdCheckoutUser
AuthorCreated Bytp_Author 
    

 

应用

================

读写Field时

 

5-19-2010 6-29-38 PM

写代码时应用如下:

SPListItemCollection oitems = splist.Items;
                        foreach (SPListItem oitem in oitems)
                        {
                            //Use display name here.
                            string oAutorStr = oitem["Created By"].ToString();
                            Console.WriteLine();
                        }

 

===========================

编写SPQuery时

5-19-2010 6-45-02 PM

 

对应的代码如下:

SPQuery query = new SPQuery();

query.Query = "<Where>" +
                "<Eq>" +
                    "<FieldRef Name='StatusInternal'/>" +
                    "<Value Type='Text'>Good</Value>" +
                "</Eq>" +
              "</Where>";

SPListItemCollection items = splist.GetItems(query);

 

以上代码均通过了测试.

 

注意, 如果是在页面中使用javascript调用web service来查询list item, 那么通常返回结果中XML里, field的名字会在前面加上ows_前缀

 

如果您觉得清楚了, 那我的功夫就没白费. 呵呵.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值