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 Name Display Name ColName StaticName
SPQuery使用的名字 界面上显示的名字 数据库中对应的列名 不知道做什么的, 看了好几个都与Name, Internal Name一样.
CheckoutUser Checked Out To tp_CheckoutUserId CheckoutUser
Author Created By tp_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_前缀

 

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

posted on 2010-05-19 18:48  中道学友  阅读(713)  评论(0编辑  收藏  举报

导航

技术追求准确,态度积极向上