从RootWeb添加查找字段到列表

时间:2012-07-25 12:14:30

标签: c# sharepoint

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

//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");
        }

1 个答案:

答案 0 :(得分:0)

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

  strInternalFieldName = colFields.AddLookup(strInternalFieldName, targetListID, currentWeb.Site.RootWeb.ID, true);
then it works fine