我有一个名为Main site的网站集rootweb。我有很多子网站是根据年份、2012年、2011年等来命名的。在rootweb中,有一个名为"产品"的列表。在子网站中有一个名为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");
}
这是可能的,我已经解决了它,我不得不使用另一个重载的方法addlookup来接收parentweb.id
strInternalFieldName = colFields.AddLookup(strInternalFieldName, targetListID, currentWeb.Site.RootWeb.ID, true);
then it works fine