2012-07-25 50 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