启用内容类型列表中的SPFIELS不会按预期更新
-
09-12-2019 - |
题
我缺少拼图我缺少。
我有一个包含内容类型的列表。我正在编程地尝试在FeatureUpgrading方法中添加一个选项到某些选择字段。字段集合中的字段正在更新,但列表内容类型中的字段不是。
代码下面:
eventReciever.cs
public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
{
SPWeb web = (SPWeb)properties.Feature.Parent;
switch ( upgradeActionName ) {
case "AddChoice":
AddChoice(web.Fields[new Guid(parameters["FieldID"])] as SPFieldMultiChoice, parameters["Choice"]);
break;
}
}
protected static void AddChoice(SPFieldMultiChoice field, string choice) {
if ( field == null ) {
throw new ArgumentNullException("field");
}
field.Choices.Add(choice);
field.Update(true);
}
.
template.xml
<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/">
<UpgradeActions>
<VersionRange BeginVersion="0.0.0.0" EndVersion="1.0.1.0">
<CustomUpgradeAction Name="AddChoice">
<Parameters>
<Parameter Name="FieldID">{82E08EC3-0C5C-4565-B0D1-740BDBE4FA93}</Parameter>
<Parameter Name="Choice">METT</Parameter>
</Parameters>
</CustomUpgradeAction>
<CustomUpgradeAction Name="AddChoice">
<Parameters>
<Parameter Name="FieldID">{1C2947AF-86EB-4DD0-95AF-D27FBA8EE3BC}</Parameter>
<Parameter Name="Choice">METT</Parameter>
</Parameters>
</CustomUpgradeAction>
<CustomUpgradeAction Name="AddChoice">
<Parameters>
<Parameter Name="FieldID">{EBA64EAA-000E-46EE-B1DE-C9761899A31B}</Parameter>
<Parameter Name="Choice">METT</Parameter>
</Parameters>
</CustomUpgradeAction>
</VersionRange>
</UpgradeActions>
</Feature>
.
谢谢你的时间。
更新:
根据Derek GuSoff的评论,我已将addchoice方法更改为下面的代码。还是行不通。我仍然非常迷失在你应该如何做到这一点。
.protected static void AddChoice(SPWeb web, SPFieldMultiChoice field, string choice) {
if ( field == null ) {
throw new ArgumentNullException("field");
}
field.Choices.Add(choice);
field.Update(true);
SPFieldLink fieldLink = new SPFieldLink(field);
foreach ( SPList list in web.Lists ) {
if ( !list.ContentTypesEnabled ) continue;
bool update = false;
foreach ( SPContentType contentType in list.ContentTypes ) {
if ( contentType.FieldLinks[field.Id] == null ) continue;
update = true;
string[] order = ( from SPFieldLink f in contentType.FieldLinks select f.Name ).ToArray();
contentType.FieldLinks.Delete(field.Id);
contentType.FieldLinks.Add(fieldLink);
contentType.FieldLinks.Reorder(order);
contentType.Update(true);
}
if ( update ) {
list.Update(true);
}
}
}
解决方案 2
一旦我想出来,我需要做的就是将我的更改推向列表是我内容类型上的Call SPContentType.Update(true)
。我的代码如下:
eventReciever.cs
public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
{
SPWeb web = (SPWeb)properties.Feature.Parent;
switch ( upgradeActionName ) {
case "PushFieldUpdatesToList":
PushFieldUpdatesToList(web.Fields[new Guid(parameters["FieldID"])]);
break;
}
}
protected static void PushFieldUpdatesToList(SPField field) {
if ( field == null ) {
throw new ArgumentNullException("field");
}
field.Update(true);
}
.
template.xml
<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/">
<UpgradeActions>
<VersionRange BeginVersion="0.0.0.0" EndVersion="1.0.1.0">
<CustomUpgradeAction Name="PushFieldUpdatesToList">
<Parameters>
<Parameter Name="FieldID">{82E08EC3-0C5C-4565-B0D1-740BDBE4FA93}</Parameter>
</Parameters>
</CustomUpgradeAction>
</VersionRange>
</UpgradeActions>
</Feature>
.
希望这有助于别人。这不是最直观的过程。再次感谢您的时间。
其他提示
编辑:上次我试图从内存给你一个答案而不验证它,这是错误的。对于那个很抱歉。我不会再次射击一个糟糕的答案。这次我创建了一个包含两个内容类型的列表,两者都有一个名为“region”的选择字段。然后在控制台应用程序中,我运行了这个代码:
SPList list = web.Lists.TryGetList("blah");
if (list != null)
{
SPFieldChoice fld = list.Fields["region"] as SPFieldChoice;
fld.Choices.Add("southwest");
fld.Update();
}
.
这在列表中的两个内容类型中更新了选择字段。我希望这就是你以后的。它应该在功能接收器中没有修改。
还请注意,这在沙箱中不起作用。逐步通过代码时似乎工作,但该字段不会更新。通过所有帐户,它应该工作,但它没有。