0

我有一个名为 Main Site 的网站集 rootweb。我有很多子网站,按年份叫,2012、2011等。在rootweb中,有一个叫Products的列表。在子站点中有一个名为 Sales 的列表,我需要在根站点中的 Products 列表中添加一个查找字段。我找到了这段代码,但它不起作用,它创建了查找字段,但即使有产品,下拉列表也是空的。

//Request North lookup
                 currentList = currentWeb.GetSafeListByName(SponsoringCommon.Constants.LISTNAMES_SALESNORTH);
                 string sFldReqNr = SponsoringCommon.Constants.FIELDS_REQUESTNUMBERLOOKUPNORTH_NAME + currentWeb.Title;
                 Functions.CreateRequestNumberLookup(currentList, sFldReqNr, false, Functions.NorthSouth.North);

                 ArrayList colPreviousContentTypes = new ArrayList();
                 currentList.AddFieldToContentType(sFldReqNr, SponsoringCommon.Constants.CONTENTTYPES_SALESNUMBER_NAME, 2, colPreviousContentTypes, 1033);




public static void CreateProductNameLookup(SPList currentList, string strInternalFieldName, bool allowMultipleValues)
        {
              Logger.LogDebug("Functions",
                "CreateProductNameLookup(SPList currentList, string strInternalFieldName, bool allowMultipleValues)", "BEGIN");

            SPWeb currentWeb = currentList.ParentWeb;
            SPList targetList = currentWeb.Site.RootWeb.GetSafeListByName(SponsoringCommon.Constants.LISTNAMES_PRODUCT_NAME);
            SPFieldCollection colFields = currentList.Fields;
            Guid targetListID = targetList.ID;

            int L1 = strInternalFieldName.Length;
            int L2 = currentWeb.Title.Length;
            string sFieldNameWithoutYear = (strInternalFieldName.EndsWith(currentWeb.Title) ? 
                strInternalFieldName.Substring(0, L1 - L2) : 
                strInternalFieldName);

            if (colFields.ContainsField(strInternalFieldName))
            {
                Logger.LogDebug("Functions",
                    "CreateProductNameLookup(SPList currentList, string strInternalFieldName, bool allowMultipleValues)",
                    "Field '" + strInternalFieldName + "' already exists. (>> Skipped)");
            }
            else
            {
                strInternalFieldName = colFields.AddLookup(strInternalFieldName, targetListID, false);
                SPField fld = colFields.GetFieldByInternalName(strInternalFieldName);
                fld.ShowInNewForm = true;
                fld.ShowInEditForm = true;
                fld.ShowInDisplayForm = true; 
                SPFieldLookup fldLU = fld as SPFieldLookup;
                fldLU.AllowMultipleValues = allowMultipleValues;
                string strSchemaXml = fldLU.SchemaXmlWithResourceTokens;
                strSchemaXml = strSchemaXml.Replace("DisplayName=\"" + strInternalFieldName + "\"", "DisplayName=\"$Resources:SPNLSponsoring,Field_" + sFieldNameWithoutYear + "_Name;\"");
                strSchemaXml = strSchemaXml.Replace("/>", " ShowField=\"" + targetList.Fields[SponsoringCommon.Constants.FIELDS_PRODUCT_NAMENEW].InternalName + "\" " +
                    "Description=\"$Resources:SPNLSponsoring,Field_" + sFieldNameWithoutYear + "_Description;\" " +
                    "Group=\"$Resources:SPNLSponsoring,Field_NationaleLoterijSponsoringColumns_Group;\" />");
                fldLU.SchemaXml = strSchemaXml;
                fldLU.Update(true);
                currentList.Update();
            }

            Logger.LogDebug("Functions",
                "CreateProductNameLookup(SPList currentList, string strInternalFieldName, bool allowMultipleValues)", "END");
        }
4

1 回答 1

0

有可能,我已经解决了,我不得不使用另一个重载方法 addlookup 来接收 parentweb.id

  strInternalFieldName = colFields.AddLookup(strInternalFieldName, targetListID, currentWeb.Site.RootWeb.ID, true);
then it works fine
于 2012-07-25T12:32:59.140 回答