简体   繁体   English

以编程方式生成的查找字段导致“System.ArgumentException:Value不在预期范围内”创建新项目时

[英]Programmatically generated Lookup Field causes “System.ArgumentException: Value does not fall within the expected range” when creating new item

I created a content type TypeA that has a lookup field to basic ListB. 我创建了一个内容类型TypeA,它具有基本ListB的查找字段。 Then I create a ListA that uses TypeA. 然后我创建一个使用TypeA的ListA。 Everything is created programmatically. 一切都是以编程方式创建的。

An exception is thrown when ListA's 'add new item' modal is supposed to load. 当ListA的“添加新项目”模式应该加载时抛出异常。 It only happens when ListB has items in it. 它只发生在ListB中有项目的情况下。 If ListB is empty, listA's 'add new item' modal loads and the lookup field to ListB correctly displays (None) 如果ListB为空,则listA的'添加新项'模态加载,并且ListB的查找字段正确显示(None)

stack trace: 堆栈跟踪:

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)

Methods I use to create the content type, lists, and lookup field: 我用来创建内容类型,列表和查找字段的方法:

    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;
    }

Example

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);

The SPFieldLookup.LookupField property requires the field's internal name. SPFieldLookup.LookupField属性需要字段的内部名称。

Solution is to make the following change (in the example): 解决方案是进行以下更改(在示例中):

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.ArgumentException:'值不在预期范围内。 - System.ArgumentException: 'Value does not fall within the expected range.' System.ArgumentException:值不在预期范围内 - System.ArgumentException: Value does not fall within the expected range Metro 8 - 绑定ListView导致System.ArgumentException“值不在预期范围内” - Metro 8 - Binding ListView causes System.ArgumentException “Value does not fall within the expected range” Application.Run(new Form1()) 抛出 System.ArgumentException: '值不在预期范围内。' - Application.Run(new Form1()) throws System.ArgumentException: 'Value does not fall within the expected range.' Windows Phone 8映射:{System.ArgumentException:值不在预期范围内。} - Windows Phone 8 Map : {System.ArgumentException: Value does not fall within the expected range.} System.ArgumentException值不在SQL问题的预期范围内 - System.ArgumentException Value does not fall within the expected range, SQL issue WebView2: System.ArgumentException: '值不在预期范围内。' - WebView2: System.ArgumentException: 'Value does not fall within the expected range.' System.ArgumentException:“值不在预期范围内。” 在内容对话框 - System.ArgumentException: 'Value does not fall within the expected range.' on Content Dialog (System.ArgumentException)值不在 uwp 中的另一个弹出窗口中打开弹出窗口时抛出的预期范围异常 - (System.ArgumentException) Value does not fall within the expected range exception thrown while open the popup in another one popup in uwp ArgumentException:值不在预期范围内 - ArgumentException: Value does not fall within the expected range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM