Tuesday, February 19, 2008

MOSS Cross Site Lookup

Need a free MOSS 2007 cross site look up?

This one comes up on most searches and seems to have tons of issues.

However after Googling a little harder I found this cross site look up which has worked pretty well and I have seen three different projects (including one of mine) get some real mileage out of this one. The author gives a fantastic tutorial how it was composed too. The only things we found were:

  • The cross site look-up is only a dropdown, so it will not support multi-selects. Well, you have the code.
  • Another person at RDA saw it does not return result in alphabetical order; however that was easily fixed by adding the following to CrossSiteLookupFieldControl.FillDropDownList().
public static void SortByValue(ListControl combo) {
SortCombo(combo, new ComboValueComparer());
}

public static void SortByText (ListControl combo) {
SortCombo(combo, new ComboTextComparer());
}

private static void SortCombo(ListControl combo, IComparer comparer) {
int i;

if (combo.Items.Count <= 1)
return;

ArrayList arrItems=new ArrayList();

for (i=0; i<combo.Items.Count; i++) {
ListItem item=combo.Items[i];
arrItems.Add(item);
}

arrItems.Sort(comparer);
combo.Items.Clear();

for (i=0; i<arrItems.Count; i++) {
combo.Items.Add((ListItem) arrItems[i]);
}
}


// compare list items by their value
private class ComboValueComparer : IComparer
{

public enum SortOrder
{
Ascending=1,
Descending=-1
}

private int _modifier;

public ComboValueComparer() {
_modifier = (int) SortOrder.Ascending;
}

public ComboValueComparer(SortOrder order) {
_modifier = (int) order;
} //sort by value


public int Compare(Object o1, Object o2) {
ListItem cb1=(ListItem) o1;
ListItem cb2=(ListItem) o2;
return cb1.Value.CompareTo(cb2.Value)*_modifier;
}
} //end class ComboValueComparer


// compare list items by their text.
private class ComboTextComparer : IComparer {
public enum SortOrder {
Ascending=1,
Descending=-1
}

private int _modifier;

public ComboTextComparer()
{
_modifier = (int) SortOrder.Ascending;
}

public ComboTextComparer(SortOrder order) {
_modifier = (int) order;
}

//sort by value p
public int Compare(Object o1, Object o2) {
ListItem cb1=(ListItem) o1; ListItem cb2=(ListItem) o2;
return cb1.Text.CompareTo(cb2.Text)*_modifier;
}
} //end class ComboTextComparer

4 comments:

Unknown said...

We were looking for the same functionality.. only catch is that we need to support variations for the looked-up list (actually a page library). So the items we want to be looked up are a field in a page library and we need to have fields in other page libraries to lookup to it. All works fine for one variation making use of the cross site lookup, however when I create another variation, I see the cross site lookup points to the same variant. Is there a way it can be configued to take the item within the same variant or any ideas to workaround this?

Jason Apergis said...

Jackson,

Off hand I would have to do a little research. I personally have messed with variations too much other than the basics.

If I understand you question correctly is that the column can only point to one look-up data source. That is probably because the content type has a GUID set into the WebLookupID (I think that is the right property). So the content type underneath the hood can only point to one location. Knowing that this seems to be a limitiation. I Wish I could give you a better answer.

Jason

Suora Demokraatti said...

Hi!

Is this resally a lookup field?
All it seems to be doing is copying value (of the lookup field) once to destination. What if this value is updated in source and this will be broken. How to prevent these kind of issues?
Have I missed some functionality?

vipasane

Suora Demokraatti said...

In addition to my last comment.

If I have a source list of animals containing cat, dog.
And cat being selected to some list using cross site lookup.

After that cat is changed to elephant. Yet the referencing value is still being cat and ill broke down if being clicked.

So everytime seleted value is shown it should be fetched from sourcelist like in lookupfield.
How to accomplish this?