Use of filtered lookup field across sites on sitetemplate

Jan 7, 2010 at 12:29 PM

Hey,

I use the filtered lookup field in a list on a site template. The lookup field points to a list on a site other than the site template. When i create a new site based on this site template, the lookupfield loses connection with the list on the other site. This is because upon site creation, the list guids known in the sitetemplate will be replaced by a new one. For the lookup field the list guid will be replaced, but it doesn't point to an existing list (The guid of the list itself is not replaced, because not on the template). So i've manipulated the source code in the following way.

In the FilteredLookupField class i've added ListGuid and SiteGuid (They will not be replace upon use of sitetemplate!):

private string _listGuid;

public string ListGuid

{      get

{       if (_listGuid == null)

{  _listGuid = GetFieldThreadDataValue("ListGuid", false);   }

return (!string.IsNullOrEmpty(_listGuid) ? _listGuid : Guid.Empty.ToString());

}

set  {  SetFieldThreadDataValue("ListGuid", value); }

}

Same for SiteGuid.

In FilteredLookuFiledEditor class in the OnSaveChange method i've added :

_f.SiteGuid = _web.ID.ToString();

_fListGuid = list;

In the Utilities class in the GetAvailableValues manipulated following code

try

{  lookupList = lookupWeb.Lists[new Guid (f.LookupList)]; }    //This will throw exception if list is not found

catch(Exception) { lookupList = lookupWeb.Lists[new Guid (f.ListGuid)]; }  //ListGuid will still contain the write guid.

With this modifications i've managed to get the items for the FilteredLookupFieldControl. So in New and Edit mode it is possible to make the right selection. The only problem is that the selected Value will not appear in the FilteredLookupField in the list.

When i go to my filteredlookup column in my listsettings it appears that the configuration is pointing to a default list. My selected value is not available in this list. I think that is why it is not set in the FilteredLookupField. f.LookupList still points to wrong Guid. With reflection i've managed to set this property to the right guid. In FilteredLookuField i've added following method

public string BaseSetFieldAttributeValue(string key, string value)

{

Type type = typeof(SPField);

MethodInfo method = type.GetMethod("SetFieldAttributeValue",BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(string), typeof(string) }, null);

string result = (string)method.Invoke(this, new object[] { key, value });

return result;

}

In the Utilities class in the GetAvailableValues rewrited following code

try

{  lookupList = lookupWeb.Lists[new Guid (f.LookupList)]; }    //This will throw exception if list is not found

catch(Exception)

{

f.LookupList = f.BaseSetFieldAttributeValue("List", f.ListGuid);

lookupList = lookupWeb.Lists[new Guid (f.LookupList)];

But it does not solve my problem to get the value into my FilteredLookupField, I guess it is because i've manipulated the Field property (f.LookupList) on SPItem level.  I believe it should happen on SPList level. But i don't seem to find the correct place to do this. (f.ParentList seems to be null) Is there anybody who can give me a hand on this. 

Thanks.

 

Jan 8, 2010 at 2:38 PM

Yoh,  Solved it

I've added similar code into the Initialize method of FilteredLookuFieldControl as in the Utilities class in the GetAvailableValues

FiltererdLookupField field = (FiltererdLookupField)this.List.Fields[base.Field.Id];

this.Web.AllowUnsafeUpdates = true;

using (SPSite site = SpControl.GetContextSite(Context))

{     using (SPWeb lookupWeb = site.OpenWeb (field.LookupWebId))

{      try

{  lookupList = lookupWeb.Lists[new Guid (field.LookupList)]; }    //This will throw exception if list is not found

catch(Exception)

{

field.LookupList = field.BaseSetFieldAttributeValue("List", field.ListGuid);

lookupList = lookupWeb.Lists[new Guid (field.LookupList)];

field.Update();

}

}

this.Web.AllowUnsafeUpdates = false;

this code updates the SPList.Field while in the GetAvailableValues method the SPItem.Field is used. It seems that both are needed. I've not been able to clear up the code without breaking the functioniality. Maybe because my looks are to close to the problem.  Any suggestions from anyone with a view from another perspective are welcome. 

In my opinion, the solution here provided has the disadvantage to have used reflection. This indicates that the solution is not provided on the right place. It should be solved in the code that replaces the guids when using the sitetemplate. (Microsoft??).  The advantage of this solution is that the FilteredLookupField still is an isolated feature. No dependent feature is needed to solve this problem.

I hope to be at service of anyone with the same problem.

Greetz,

Walter

Sep 2, 2010 at 5:15 PM

Hi,

I'm having the same problem you had with the filtered lookup, but I didnt understand what you did to solve it. Can you help me?

Thanks.