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   中道学友  阅读(722)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律

导航

< 2010年5月 >
25 26 27 28 29 30 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

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

点击右上角即可分享
微信分享提示