Request a Quote

Update Lookup Column Web and List Programmatically

Update Lookup Column Web and List Programmatically

I had requirement of creating custom list programmatically for our varience sites en-us(English) and de-de(German). For that, I have created site-columns, content-type and add site-columns to content-type at root site level. Now I am using same content-type in varience site’s list. But the lookup column of varience site’s list refer to root web ( and list) instead of current web.So to fix this issue I have to manually change the lookup column webid and list of current site not root site.

 

///
/// Set lookup site-columns webid and listid for all variance site
///
public static void SetLookupFieldWebAndListID()
{
  try
  {
    using (SPSite site = new SPSite(RootSiteUrl))
    {
      SPWebCollection allWebs = site.AllWebs;
      foreach (SPWeb currentWeb in allWebs)
      {
        if (PublishingWeb.IsPublishingWeb(currentWeb))
        {
          using (currentWeb)
          {
            //fetch lookup column list and lookup column
            SPList list = currentWeb.Lists["ListName"];
            SPFieldLookup lookupColumn = (SPFieldLookup)list.Fields.GetField("LookupColumnName");

            //update lookup column webid
            lookupColumn.LookupWebId = currentWeb.ID;

            //fetch lookup list
            SPList lookupList = currentWeb.Lists["lookupListName"];

            //update lookup column listid
            if (string.IsNullOrEmpty(lookupColumn.LookupList))
            {
              lookupColumn.LookupList = lookupList.ID.ToString();
            }
            else
            {
              lookupColumn.SchemaXml = lookupColumn.SchemaXml.ReplaceXmlAttributeValue("List", lookupList.ID.ToString()).ReplaceXmlAttributeValue("WebId", currentWeb.ID.ToString());
            }
            lookupColumn.Update(true);
          }
        }

      }
    }

  }
  catch (Exception ex)
  {
    Logger.LogError(ex.Message, "Setup lookup field error", ex.StackTrace);
  }
}

///
/// Relpace the WebID and ListID of lookup field xml-schema.
///
public static string ReplaceXmlAttributeValue(this string xml, string attributeName, string value)
{
            if (string.IsNullOrEmpty(xml))
            {
                throw new ArgumentNullException("xml");
            }
            if (string.IsNullOrEmpty(value))
            {
                throw new ArgumentNullException("value");
            }

            int indexOfAttributeName = xml.IndexOf(attributeName, StringComparison.CurrentCultureIgnoreCase);
            if (indexOfAttributeName == -1)
            {
                throw new ArgumentOutOfRangeException("attributeName", string.Format("Attribute {0} not found in source xml", attributeName));
            }

            int indexOfAttibuteValueBegin = xml.IndexOf('"', indexOfAttributeName);
            int indexOfAttributeValueEnd = xml.IndexOf('"', indexOfAttibuteValueBegin + 1);

            return xml.Substring(0, indexOfAttibuteValueBegin + 1) + value +    xml.Substring(indexOfAttributeValueEnd);
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2015 Trimantra Software Solution LLP- Software Outsourcing Company India. All rights reserved.