06
Apr
sharepoint 2003 OM code to sort Link List items
weekending in east texas
and ive had just about enough of the internets and asp.net and sharepoint and seo and all that for a few days.
so thank you for keeping up with me here and have a very happy Easter. see you next week.
by the way, i wrote a little class yesterday. might be useful if you want to sort or order the SPListItems in a "Links" List in sharepoint 2003.
yes that's a float - a float not an int! =)
#region SPListItemOrderer : IComparer public class SPListItemOrderer : IComparer { public int Compare(object x, object y) { SPListItem sx; SPListItem sy; if (x is SPListItem) { sx = x as SPListItem; } else throw new ArgumentException("Object is not of type SPListItem"); if (y is SPListItem) { sy = y as SPListItem; } else throw new ArgumentException("Object is not of type SPListItem"); if (sx["Order"] != null || sy["Order"] != null) return float.Parse(sx["Order"].ToString()).CompareTo(float.Parse(sy["Order"].ToString())); else throw new ArgumentException("SPListItem does not have an order field"); } } #endregion
gah, oh ok, ok.. here's the usage:
// get an array from the collection so we can sort it ArrayList items = new ArrayList(); foreach (SPListItem item in theList.Items) { items.Add(item); } SPListItem[] sortableItems = (SPListItem[]) items.ToArray(typeof(SPListItem)); try { Array.Sort(sortableItems, new SPListItemOrderer()); } catch { /* do not sort */ }
just in case you want to do something like this ( by the way that inumerating the fields code was a pain!):
foreach (SPListItem si in sortableItems) { try { // debugging stuff here //foreach (SPField f in si.Fields) //{ // try // { // strOut += string.Format("<!– {0} : {1} –>\n", f.Title, si[f.Title].ToString()); // } // catch { } //} // the URL item has two fields, comma separated string itemurl = System.Web.HttpUtility.UrlPathEncode(si["URL"].ToString().Split(',')[0].Trim()); string itemname = si["URL"].ToString().TrimStart(itemurl.ToCharArray()).TrimStart(',').Trim(); strOut += string.Format("<tr><td><a href=\"{0}\">{1}</a></td></tr>\n", itemurl, itemname); } catch { /* do not create markup for this link */ } }
May 14th, 2007 at 9:37 am
i needed an example of how to do just this exactly, thank you for posting.
July 18th, 2008 at 5:08 am
Hi,
this is a great posting - but I need some help to get this working.
And I need to order the SPFieldChoice, a column called "Kats" in the "Pages" document library. I am working with WSS 3.0.
Thanks