以编程方式更新 SharePoint 自定义字段类型列值
我在 SharePoint 中创建了一个基于“多行文本”字段类型的自定义字段类型(例如“示例”)。现在我在列表中创建了一个类型为“示例”的新列(例如“测试”)。我创建了一些列表项。
我可以从 SharePoint UI 成功编辑列值(通过编辑表单)。但是,当我尝试以编程方式修改任何列表项的“测试”列的值时,该列表项的“测试”列的值变为空/空。
知道为什么会出现这个问题吗?下面是我正在使用的 filetypes xml
<?xml version="1.0" encoding="utf-8"?>
<FieldTypes>
<FieldType>
<Field Name="TypeName">Sample</Field>
<Field Name="ParentType">Note</Field>
<Field Name="TypeDisplayName">Sample</Field>
<Field Name="TypeShortDescription">Sample</Field>
<Field Name="UserCreatable">TRUE</Field>
<Field Name="ShowInListCreate">TRUE</Field>
<Field Name="ShowInSurveyCreate">TRUE</Field>
<Field Name="ShowInDocumentLibraryCreate">TRUE</Field>
<Field Name="ShowInColumnTemplateCreate">TRUE</Field>
<Field Name="FieldTypeClass">
SharePoint.Sample.FieldType, SharePoint.Sample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c5560aa45b5518dc
</Field>
<Field Name="FieldEditorUserControl">
/_controltemplates/FieldEditor.ascx
</Field>
<PropertySchema>
<Fields>
<Field Name="DisplayedListBoxProperty" DisplayName="DisplayedListBoxProperty"
Type="Text" Hidden="True">
<Default>"abc"</Default>
</Field>
</Fields>
</PropertySchema>
</FieldType>
</FieldTypes>
代码,我正在使用它来编辑:
SPSite site = new SPSite("site url")
SPWeb web = site.OpenWeb();
SPList list = web.Lists["MyList"];
SPListItem item = list.Items[0];
item["test"] = "xyz"; //becomes null after update
item["numCol"] = "34"; //Gets updated to new value 34 after update
web.AllowUnsafeUpdates = true;
item.Update();
一件重要的事情:此代码在另一个列表的 itemupdating eventhadler 中运行。
I have created a custom field type (say 'Sample') in SharePoint which is based on 'multiline text' field type. Now I have created a new column(say 'test') of type 'Sample' in a list. I created some list items.
I can edit the column value successfully from SharePoint UI (through edit form). But when I try to modify the value of 'test' column programatically for any list item, value of 'test' column for that list item becomes null/empty.
Any idea why this probem is occuring?? Below is the filedtypes xml that I am using
<?xml version="1.0" encoding="utf-8"?>
<FieldTypes>
<FieldType>
<Field Name="TypeName">Sample</Field>
<Field Name="ParentType">Note</Field>
<Field Name="TypeDisplayName">Sample</Field>
<Field Name="TypeShortDescription">Sample</Field>
<Field Name="UserCreatable">TRUE</Field>
<Field Name="ShowInListCreate">TRUE</Field>
<Field Name="ShowInSurveyCreate">TRUE</Field>
<Field Name="ShowInDocumentLibraryCreate">TRUE</Field>
<Field Name="ShowInColumnTemplateCreate">TRUE</Field>
<Field Name="FieldTypeClass">
SharePoint.Sample.FieldType, SharePoint.Sample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c5560aa45b5518dc
</Field>
<Field Name="FieldEditorUserControl">
/_controltemplates/FieldEditor.ascx
</Field>
<PropertySchema>
<Fields>
<Field Name="DisplayedListBoxProperty" DisplayName="DisplayedListBoxProperty"
Type="Text" Hidden="True">
<Default>"abc"</Default>
</Field>
</Fields>
</PropertySchema>
</FieldType>
</FieldTypes>
Code that I am using to edit:
SPSite site = new SPSite("site url")
SPWeb web = site.OpenWeb();
SPList list = web.Lists["MyList"];
SPListItem item = list.Items[0];
item["test"] = "xyz"; //becomes null after update
item["numCol"] = "34"; //Gets updated to new value 34 after update
web.AllowUnsafeUpdates = true;
item.Update();
One Important thing: This code runs in itemupdating eventhadler of another list.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的代码中,“FieldTypeClass”的自定义字段类型存在问题。尝试此“SharePoint.Sample.FieldType,SharePoint.Sample,Version=1.0.0.0,Culture=neutral,PublicKeyToken=c5560aa45b5518dc”
希望这会对您有所帮助。
With your code, there is problem with your Custom Field Type with "FieldTypeClass". Try this 'SharePoint.Sample.FieldType, SharePoint.Sample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c5560aa45b5518dc'
hope this will helps you.
好吧,我找到了问题区域。有一个名为 GetValidatedString() 的函数,用于对值进行任何验证。即使您以编程方式更新值,也会调用此函数。
这造成了一个问题。
Well I found the problem area. There is a function called GetValidatedString() which is used for any validation on the value. This function gets called even in case when you update value programmatically.
It was creating a problem.