将字段添加到 SharePoint 中的自定义列表类型时出现问题

发布于 2024-07-26 19:20:31 字数 1629 浏览 5 评论 0原文

我已经为 SharePoint 定义了自定义列表模板类型。 我使用 VSeWSS 1.3 安装它,一切似乎都运行正常。

我添加了一个自定义操作,它将向列表添加其他列 (SPFields)。 但是,每次执行该代码时,它都会抛出 ArgumentException 并带有 “值不在预期范围内。” 错误消息。 此行为似乎特定于自定义类型,如 这篇博文

以下是相关代码(thisList 是使用浏览器界面创建的自定义列表模板类型的实例),该代码尝试将字段添加到 thisList 的 SPFieldCollection:

                SPFieldType fieldType = Format2SPFieldType(format);
                SPField field = new SPField(thisList.Fields, fieldType.ToString(), fieldName);
                thisList.Fields.Add(field);

最后一条语句 (thisList.Fields.Add(field)) 抛出以下内容例外:

   Message  "Value does not fall within the expected range."

   at Microsoft.SharePoint.SPFieldCollection.GetFieldByInternalName(String strName, Boolean bThrowException)
   at Microsoft.SharePoint.SPFieldCollection.GetFieldByInternalName(String strName)
   at Microsoft.SharePoint.SPFieldCollection.AddFieldAsXmlInternal(String schemaXml, Boolean addToDefaultView, SPAddFieldOptions op)
   at Microsoft.SharePoint.SPFieldCollection.AddFieldAsXml(String schemaXml, Boolean addToDefaultView, SPAddFieldOptions op)
   at Microsoft.SharePoint.SPFieldCollection.Add(SPField field)

如果 SPList 项是基本列表类型(内置自定义列表),则相同的代码执行得很好。

是否有任何字段需要在 CAML 中显式设置并直接使用 AddFieldAsXml() 方法以使此代码适用于自定义列表模板类型?

更新:我还应该提到,即使调用抛出异常,在某些情况下实际上也会创建这些字段!

I have defined a custom list template type for SharePoint. I install it using VSeWSS 1.3 and everything seems to behave correctly.

I have added a custom action which will add additional columns (SPFields) to the list. However, every time that code executes it throws and ArgumentException with a 'Value not in the expected range.' error message. This behavior seems to be specific to custom types as suggested in this blog post.

Here is the relevant code (thisList is an instance of my custom list template type created using the browser interace) that tries to add a field to the SPFieldCollection of thisList:

                SPFieldType fieldType = Format2SPFieldType(format);
                SPField field = new SPField(thisList.Fields, fieldType.ToString(), fieldName);
                thisList.Fields.Add(field);

The last statement (thisList.Fields.Add(field)) throws the following exception:

   Message  "Value does not fall within the expected range."

   at Microsoft.SharePoint.SPFieldCollection.GetFieldByInternalName(String strName, Boolean bThrowException)
   at Microsoft.SharePoint.SPFieldCollection.GetFieldByInternalName(String strName)
   at Microsoft.SharePoint.SPFieldCollection.AddFieldAsXmlInternal(String schemaXml, Boolean addToDefaultView, SPAddFieldOptions op)
   at Microsoft.SharePoint.SPFieldCollection.AddFieldAsXml(String schemaXml, Boolean addToDefaultView, SPAddFieldOptions op)
   at Microsoft.SharePoint.SPFieldCollection.Add(SPField field)

This same code executes just fine if the SPList item is the base list type (built-in custom list).

Are there any fields that need to be set explicitly in the CAML and using the AddFieldAsXml() method directly to make this code work with custom list template types?

Update: I should also mention that the fields are actually created in some instances even though the call throws an exception!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

又爬满兰若 2024-08-02 19:20:31

事实证明,这是由于调用 thisList.SchemaXML 将 SPList 对象置于我无法恢复的状态! 获取对同一 SharePoint 列表的新引用,例如 SPList newList= thisList.ParentWeb.Lists[thisList.ID] 解决了问题!

Turns out that this was caused because calling thisList.SchemaXML put the SPList object into a state that I wasn't able to recover from! Getting a new reference to the same SharePoint List, e.g., SPList newList=thisList.ParentWeb.Lists[thisList.ID] solved the issue!

爱情眠于流年 2024-08-02 19:20:31

您尝试添加的字段类型是什么? 是内部字段还是自定义字段类型,该函数 Format2SPFieldType 返回什么?
如果它是内置字段,您可以尝试添加

thisList.Fields.Add("DisplayName", SPFieldType.Integer, false);

What is the Type of Field you are trying to add? is that an Internal field or a Custom Field type, what does this Function Format2SPFieldType return?
If it is a Inbuilt field can you try adding with the

thisList.Fields.Add("DisplayName", SPFieldType.Integer, false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文