Welcome to Office Zealot Sign in | Join | Help

Code Snippet for SharePoint Links List Query Web Part

Recently, I had to “throw together” a quick web part (this is SharePoint 2007) that queries a links list (located on another site) and returns the associated items as a bulleted list.  The goal was to use this web part in a site definition so all sites created would have a dynamic reference to hyperlinks managed in a single source.  I thought it might be helpful to share the code (nothing overly complex here).  Two things of note: (1) SharePoint stores the description and hyperlink for a URL in a single field.  It provides an object to help you parse these (so you don’t have to write the extra code).  (2) We included a custom sort column to control how the items are presented.  There is some extra code below to show how I did that.

My next task is to create a version of this where the list referenced is on another SharePoint server.  I’ll use the SharePoint web services to get the list information.  I’ll share that snippet when done.  Happy coding!

 

    public class SharePointTips : System.Web.UI.WebControls.WebParts.WebPart

    {

        public SharePointTips()

        {

            this.ExportMode = WebPartExportMode.All;

        }

 

        public class SharePointTip

        {

            public int sortOrder;

            public string tipLink;

 

            public SharePointTip(int sortOrder, string tipLink)

            {

                this.sortOrder = sortOrder;

                this.tipLink = tipLink;

            }

        }

 

        protected override void Render(HtmlTextWriter writer)

        {

            string SharePointTipsListSite = System.Configuration.ConfigurationManager.AppSettings["SharePointTipsSite"].ToString();

            string SharePointTipsListName = System.Configuration.ConfigurationManager.AppSettings["SharePointTipsList"].ToString();

 

            try

            {

                using (SPSite site = new SPSite(SharePointTipsListSite))

                {

                    using (SPWeb web = site.OpenWeb())

                    {

                        SPList list = web.Lists[SharePointTipsListName];

                        SPListItemCollection itemCollection;

                        itemCollection = list.Items;

 

                        List<SharePointTip> SharePointTips = new List<SharePointTip>();

                        foreach (SPListItem item in itemCollection)

                        {

                            SPFieldUrlValue listField = new SPFieldUrlValue(item["URL"].ToString());

                            SharePointTips.Add(new SharePointTip(System.Convert.ToInt32(item["SortOrder"]), "<img src='/_layouts/images/square.gif'>&nbsp;<a href='" + listField.Url + "'>" + listField.Description + "</a><br>"));

                        }

 

                        SharePointTips.Sort(delegate(SharePointTip p1, SharePointTip p2) { return p1.tipLink.CompareTo(p2.tipLink); });

                        SharePointTips.ForEach(delegate(SharePointTip tip) { writer.Write(tip.tipLink); });

                    }

                }

            }

            catch (Exception e)

            {

                writer.Write(e.Message);

            }

        }

 

Published Thursday, April 01, 2010 2:09 AM by Mauro
Filed under: ,

Comments

# Twitter Trackbacks for Mauro Cardarelli : Code Snippet for SharePoint Links List Query Web Part [officezealot.com] on Topsy.com

# Mimosa Restaurant Santa Barbara, Banco Santander Brasil Home

# D100 Parts Buy Dodge Oxygen Sensor, D100 Pickup Click Here 1970 Dodge

Thursday, May 20, 2010 6:20 PM by Find C330 C340 Streetpilot C320, Okidata C3200 Fuser

# Find C330 C340 Streetpilot C320, Okidata C3200 Fuser

Thursday, May 20, 2010 9:09 PM by Nsx Replacement 2010 Toyota, 2nd Hand Acura Nsx Parts

# Nsx Replacement 2010 Toyota, 2nd Hand Acura Nsx Parts

Thursday, May 20, 2010 10:20 PM by 1991 Chevy Caprice Accessories C, Virgine Caprice Dvd

# 1991 Chevy Caprice Accessories C, Virgine Caprice Dvd

Thursday, May 20, 2010 10:24 PM by Sierra Vista Ptt, Corgi Vista Cruiser

# Sierra Vista Ptt, Corgi Vista Cruiser

Friday, May 21, 2010 12:05 AM by Isuzu Parts Brake, Isuzu Trucks Price

# Isuzu Parts Brake, Isuzu Trucks Price

# Galant Replacement High Quality Dot And Sae, Montero 2010 Mitsubishi Galant Fog Lights

# Sephia Coolant Sorry, Free Shipping Kia Sephia Parts Strut Assembly

# 1995 Bmw 318ti Parts Oem, 325e Parts Decor Bmw 318ti

Friday, May 21, 2010 4:35 AM by 350e Part Embroidery Machine, Youtube E350

# 350e Part Embroidery Machine, Youtube E350

# 328 Convertible Radiator 740i Bmw 320i, 740i Volvo Headlight Tail Light

Friday, May 21, 2010 12:33 PM by 328is Discount Alternative, 328is Ball

# 328is Discount Alternative, 328is Ball

# Lincoln Versailles Luxury Disc Brakes, Versailles Problems

# D450 Cooling System Core Ram 2500, P35 P2500 Van Repair Yukon Denali

# 2009 Ford F450 Super Duty Specifications, New F450 F 150 Explorer Sport Trac

# Nissan 350z Replacement Aftermarket Infiniti G35, Buy Stanza Xterra Used Nissan 350z

# 1972 Ford Pinto Used Cars, Nel Blu Dipinto Di Blu Kar Gipsy Kings

# Bmw 330i Sport For Sale Pistonheads, Order 330i Package

Saturday, May 22, 2010 1:28 AM by Ford E450 Car Parts, Ford E 250 E450 Part

# Ford E450 Car Parts, Ford E 250 E450 Part

# Chevrolet Aveo Zoe Replacement, Bumper Chevrolet Aveo Get

# Mercury Mystique Mileage Automatic Transmission, Buy Mystique Lowest

# K30 Pickup Replacement 1969 Chevrolet Suburban 2500, Amazon Chevrolet Monza

# 1996 Jeep Cherokee Football Review, 2011 Grand Cherokee Msrp

Saturday, May 22, 2010 2:47 PM by Bt 909 Battery Uniden Tcx905, Dvd Cx90

# Bt 909 Battery Uniden Tcx905, Dvd Cx90

# Centurion Pool Pump Motor, Centurion Transmission Oil Pan

# 1991 Nissan Stanza Radiator Fuel Pump Relay, Nissan Stanza Used Salvage Yard

# Sigma Headlight Bulbs Mitsubishi 3000gt, Mitsubishi International Inc - 34.akemet.com

# 85 Mr2 Front Bumper, Mr2 Database Auto Parts - 463.binggreen.com

# Gl 10 Price Subaru Forester, New Subaru Forester Xtr - 202.myipgirl.com

# Impreza 2nd Hand Subaru Justy, Subaru Impreza Clutch Replacement Auto Parts - 359.luna-atra.net

Sunday, May 23, 2010 5:17 AM by Sell Gl550 Our, Clearance S550 - 65.rkwrh.com

# Sell Gl550 Our, Clearance S550 - 65.rkwrh.com

# Xg300 Sale Xg350 Used Hyundai Tiburon, Chevrolet G30 Part Fuel Injector - 57.zapstreaming.com

# 300d Radiator 1987 Mercedes Benz 300cd, Order 280e 300d - 131.tijuanareader.com

# Chrysler Executive Limousine Amc Eagle Lebaron, Discount Chrysler Lebaron Parts Trailer - 394.rkwrh.com

# G30 Market, Xg300 Fog Light Napa - 119.zapstreaming.com

# Trailblazer Wholesale Tail Light, 2006 Chevy Trailblazer Tail Light - 176.myipgirl.com

PingBack from http://176.myipgirl.com/

# Restaurants Near Concorde Career College, Wna Comet Webstaurant Store - 312.tijuanareader.com

# Headlight 1987 Isuzu Trooper, Isuzu Trooper Aftermarket Shock Absorber - 344.ja3ra.com

# Volvo 262 Vehix For Sale, Replacement Lamp 915p026010 62627 Wd 62725 - 171.eumreborn.com

# E 100 Econoline Club Wagon Replacement Super Duty, Beast Supertest Vitex Agnus Castus - 331.myipgirl.com

# 1 Price 2004 Saturn L300, 2001saturn L300 2 Part Axle Assembly - 469.luna-atra.net

# Gmc C3500 Replacement Spark Plug S15 Jimmy, S15 Japanese Used Cars - 210.mfbattle.com

# Buick Roadmaster Repair Manual Cargo Liner, Roadmaster Motor - 418.rkwrh.com

# Excursion Sale 2004 Ford, Barnes And Noble Excursion - 453.1fh.org

# Toronado Replacement High Quality Bumper Assembly, P25 2500 Van Pt Oldsmobile Toronado - 68.tvshowzone.com

# Durango Reliability, Dodge Durango Coolant System - 324.jeepsunlimted.com

# 620 Pickup Fog Light Crystal Lens Eye Surgery, Olympus E620 Dz Kit - 125.dlmreza.net

Monday, May 24, 2010 10:42 PM by 530i Promo Off, Full 530i Game - 268.eumreborn.com

# 530i Promo Off, Full 530i Game - 268.eumreborn.com

# Ap 810 Samsung, 810 Zone Fan Leawood - 437.defutbolazo.com

# Mitsubishi Outlander Sport, Mitsubishi Outlander Headlight Piaa Automotive Lights - 428.renters.ws

# D400 Pricegrabber, Canon D400 Test - 338.tijuanareader.com

# 1994 Camaro Stinger Hood, 1966 Camaro - 225.rkwrh.com

# 1991 Acura Integra Ls Parts, 1994 Acura Integra Rs Service Manual Honda Civic Si - 485.rkwrh.com

# 2007 Mercedes Benz Clk550 Coupe, Clk550 Episode Discount - 127.rkwrh.com

# Rmz450 06, E325 Parts T Rex 450 - 24.jeepsunlimted.com

# Gmc C15 C1500 Suburban Replacement Steering Column Dash Panel, Gmc C2500 Suburban Bulb Direct Fit Oe 8cyl - 292.eumreborn.com

# Tires 325 60 15 All Terrain Ta Ko Lt265 75r16, 265 75 R15 - 476.myipgirl.com

# Riviera Headlight Motor 1966 Buick, 1983 Buick Riviera Convertible Parts - 258.ja3ra.com

# Mazda Mx5 Radiator Rx 8, Mx5 Used - 257.luna-atra.net

# Aries Container, D80 Repair Dodge Aries - 132.akemet.com

# B Protege B 5 Headlights 2003 Mazda Protege5, Headlight Mazda Protege5 Company - 484.dlmreza.net

# 1989 - 2000 @ Cj6a Used Cj 5 Piston Car, Cj6a Military Tanks Yak 18 - 437.animejin.com

# 1999 - 2002 @ 1995 Cadillac Fleetwood Headlight Assembly, Cadillac Escalade Aftermarket Top Speed - 221.zapstreaming.com

# 1999 - 2002 @ Cheap New Volvo Xc90, 940 Used Volvo Xc90 Recalls Awd - 233.zapstreaming.com

# 2005 - 2002 @ Buy Used Pontiac Gto Turbo, Embassy Rent Washington Dc - 293.tgrconversions.com

# 1993 - 1989 @ S550 Replacement Chloride, Blue Ray S550 - 8.animejin.com

Anonymous comments are disabled