Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have the site collection rootweb called Main Site. I have many subsites called according to the year, 2012, 2011, etc. In the rootweb, there is a list called Products. In the subsites there is a list called Sales, and I need to add a lookup field to the list Products in the root site. I found this code, but its not working, it created the lookup field, but the dropdown is empty even if there are 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");
        }
share|improve this question
add comment

2 Answers 2

I simply define column in elements file of site-scoped feature

<Field ID="fieldId" Type="Lookup" Name="Department" StaticName="Department" DisplayName="Department;" List="Lists/Departments" ShowField="Title" Overwrite="TRUE" Indexed="TRUE"></Field>

And then use it in my content type

<ContentType ID="id"
               Name="ct"
               Description=""
               Overwrite="TRUE"
               Inherits="TRUE"
               Version="0">
    <FieldRefs>
      <FieldRef ID="fieldId" Name="Department"/>
    </FieldRefs>
  </ContentType>

It works fine.

share|improve this answer
    
Hi alex what is the problem if i add feature at web scope? –  Vishal_Sharepoint Aug 27 '12 at 7:21
    
your field will be referenced to the list that shoud be exist in this web. If your would like to create lookup field to the list that is placed to another web, you should use webId and listId properties. –  Alexander Aug 27 '12 at 7:56
add comment
up vote 0 down vote accepted

if I pasted code its because I wanted the answer to include .net code, not xml. :)

I solved it myself and the problem was on this line:

  strInternalFieldName = colFields.AddLookup(strInternalFieldName, targetListID, false);

there is another overload to this method that receives the Guid of the web where the target list is.

After changing it, it worked perfect.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.