2012-07-25 21 views
0

我拥有称为主站点的站点集合rootweb。 我有许多子网站根据年份,2012,2011等调用。 在rootweb中,有一个名为Products的列表。 在子网站中有一个名为Sales的列表,我需要将查找字段添加到根站点中的列表Products。 我发现这个代码,但它不工作,它创建了查找字段,但即使有产品,下拉列表也是空的。将查找字段添加到RootWeb列表中

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

回答

0

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

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