gpt4 book ai didi

c# - 创建新项目时,以编程方式生成的查找字段导致 "System.ArgumentException: Value does not fall within the expected range"

转载 作者:太空狗 更新时间:2023-10-29 23:26:35 25 4
gpt4 key购买 nike

我创建了一个内容类型 TypeA,它有一个指向基本 ListB 的查找字段。然后我创建一个使用 TypeA 的 ListA。一切都是以编程方式创建的。

当应该加载 ListA 的“添加新项目”模态时抛出异常。只有当 ListB 中有项目时才会发生。如果 ListB 为空,则加载 listA 的“添加新项目”模式,并且 ListB 的查找字段正确显示 (None)

堆栈跟踪:

System.ArgumentException: Value does not fall within the expected range.

at Microsoft.SharePoint.SPFieldMap.GetColumnNumber(String strFieldName, Boolean bThrow)
at Microsoft.SharePoint.SPListItemCollection.GetColumnNumber(String groupName, Boolean bThrowException)
at Microsoft.SharePoint.SPListItemCollection.GetRawValue(String fieldname, Int32 iIndex, Boolean bThrow)
at Microsoft.SharePoint.SPListItem.GetValue(SPField fld, Int32 columnNumber, Boolean bRaw, Boolean bThrowException)
at Microsoft.SharePoint.SPListItem.GetValue(String strName, Boolean bThrowException)
at Microsoft.SharePoint.SPListItem.get_Item(String fieldName)
at Microsoft.SharePoint.WebControls.LookupField.get_DataSource()
at Microsoft.SharePoint.WebControls.LookupField.CreateChildControls()
at System.Web.UI.Control.EnsureChildControls()
at Microsoft.SharePoint.WebControls.BaseFieldControl.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

我用来创建内容类型、列表和查找字段的方法:

    private SPContentType createContentType(SPSite site, string typeName, string groupName, string parentTypeName, string[] fields) {
try {
SPContentType testExist = site.RootWeb.ContentTypes[typeName];
if (testExist != null)
return testExist;
}
catch { }

SPContentType parentType = site.RootWeb.ContentTypes[parentTypeName];

SPContentType contentType = new SPContentType(parentType, site.RootWeb.ContentTypes, typeName);
contentType.Group = groupName;

foreach (string field in fields) {
contentType.FieldLinks.Add(new SPFieldLink(site.RootWeb.GetField(field)));
}
contentType.FieldLinks["Title"].Required = false;
contentType.FieldLinks["Title"].Hidden = true;

site.RootWeb.ContentTypes.Add(contentType);
contentType.Update();
return contentType;
}

public SPFieldLookup createLookupField(string fieldName, string groupName, Guid listGuid, string lookupField, bool allowMultipleValues, bool isRequired) {
if (site.RootWeb.Fields.ContainsField(fieldName))
return null;
string internalName = site.RootWeb.Fields.AddLookup(fieldName, listGuid, isRequired);
SPFieldLookup lookup = site.RootWeb.Fields.GetFieldByInternalName(internalName) as SPFieldLookup;
lookup.AllowMultipleValues = allowMultipleValues;
lookup.LookupField = lookupField;
lookup.Group = groupName;
lookup.Update();
return lookup;
}

public SPList createList(string listName, string description, SPContentType contentType) {

if (web.Lists.TryGetList(listName) != null)
web.Lists[listName].Delete();
Guid newListGuid = web.Lists.Add(listName, description, SPListTemplateType.GenericList);
SPList newList = web.Lists[newListGuid];
newList.OnQuickLaunch = true;
newList.Update();

newList.ContentTypesEnabled = true;
SPContentType newType = newList.ContentTypes.Add(contentType);
newList.Update();
newList.ContentTypes["Item"].Delete();
newList.Update();
newList.Fields["Title"].Required = false;
newList.Fields["Title"].Hidden = true;
newList.Fields["Title"].Update();

SPView view = newList.DefaultView;
foreach (SPField field in newType.Fields) {
view.ViewFields.Add(field);
}
view.ViewFields.Delete("Title");
view.ViewFields.Delete("LinkTitle");
view.ViewFields.Delete("ContentType");
view.Update();

return newList;
}

示例

SPContentType typeB = createContentType(site, "Type B", "My Group", "Item", new string[] {"Salary"});
SPList listB = createList("List B", "my list b", typeB);
SPFieldLookup lookupB = createLookupField("B", "My Group", listB.ID, "Salary", false, false);
SPContentType typeA = createContentType(site, "Type A", "My Group", "Item", new string[] {"Name", "B"});
SPList listA = createList("List A", "my list a", typeA);

最佳答案

SPFieldLookup.LookupField 属性需要字段的内部名称。

解决方案是进行以下更改(在示例中):

SPFieldLookup lookupB = createLookupField("B", "My Group", listB.ID, listB.Fields["Salary"].InternalName, false, false);

关于c# - 创建新项目时,以编程方式生成的查找字段导致 "System.ArgumentException: Value does not fall within the expected range",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4660632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com