0

I want to search through my lists. And show it on a repeater control. On my dropdown i got 3 options, (All, List1 and List2). But my If statement wont work. The else is working fine, but it hangs when i click on "All". Any suggestions? My code below.

        using (SPSite site = new SPSite("http://knowitintranet:9797/sites/News"))
        {
            SPWeb web = SPContext.Current.Web;

            if (DropDownListNewsFeed.SelectedItem.Value == "All")
            {
                SPSiteDataQuery query = new SPSiteDataQuery();
                    query.Lists = "<Lists " +
                                  "ServerTemplate=\"10000\"" +
                                  "ServerTemplate=\"10001\"" +
                                  "ServerTemplate=\"10002\">" +
                                  "</Lists>";

                query.Query = string.Format("<OrderBy><FieldRef Name='Modified' Ascending='FALSE'></FieldRef></OrderBy>");
                query.Webs = "<Webs Scope=\"SiteCollection\" />";

                query.RowLimit = 10;

                DataTable dataTable = web.GetSiteData(query);

                RepeaterNewsFeed.DataSource = dataTable;

                RepeaterNewsFeed.DataBind();
            }
            else
            {
                SPList list = web.Lists.TryGetList(DropDownListNewsFeed.SelectedItem.Value);

                SPQuery query = new SPQuery();
                query.Query = "<OrderBy><FieldRef Name='Modified' Ascending='FALSE' /></OrderBy>";
                query.RowLimit = 10;

                SPListItemCollection items = list.GetItems(query);

                RepeaterNewsFeed.DataSource = items;
                RepeaterNewsFeed.DataBind();
            }
        }
5
  • Are you assigning string.Format("<OrderBy><FieldRef Name='Modified' Ascending='FALSE'></FieldRef></OrderBy>"); this to query.Query???
    – Flowerking
    Jan 29, 2013 at 14:21
  • @Flowerking . My bad, don't know how it disappeared xD My bad!
    – krisal
    Jan 29, 2013 at 14:23
  • What do you mean by "Hangs", did you manage to debug this, do you get any exceptions?
    – Flowerking
    Jan 29, 2013 at 14:26
  • @Flowerking. Well it really don't hangs. It just don't give me any output when i select "All" in my dropdownselect. But when i select list1 or list2, it shows output without problems. I don't even get any exceptions and i've tried to debug, doesn't give me any answer..
    – krisal
    Jan 29, 2013 at 14:28
  • @Flowerking, updated the code
    – krisal
    Jan 29, 2013 at 14:30

1 Answer 1

0

I don't see anything wrong with your code, but you should really check to see if this works..

What you get from web.Lists.TryGetList("Staff News").ID is a guid wrapped in {} like {Some Guid} eg. {700de2ad-9bde-432b-b7a7-11fdc5168c4b} So if you can take these Guids and strip the curly braces out before assigning them inside the xml for "<List ID=" + web.Lists.TryGetList("Staff News").ID + " />". As this can cause problems with caml queries.

Just add .ToString() to the end as shown:

"<List ID=" + web.Lists.TryGetList("Staff News").ID.ToString()" />
19
  • So how should i change the code? Quite confused right now hehe.. It would be nice if you tried to take my code and change it here?
    – krisal
    Jan 29, 2013 at 14:44
  • Updated the code, do you mean like that?
    – krisal
    Jan 29, 2013 at 14:46
  • web.Lists.TryGetList("Staff News").ID will return guid.. change it to return string as shown. This will work.
    – Flowerking
    Jan 29, 2013 at 14:49
  • "Redundant 'Object.ToString()' call for value type." is what i get now and i still don't get a output ;/
    – krisal
    Jan 29, 2013 at 14:54
  • Thats strange! Object.ToString() for value type? I really don't get it. Can you try using the list ID's directly as you have shown just now and try to see if it works? Like I said I really don't see anything wrong with your code (unless I am wrong :( )
    – Flowerking
    Jan 29, 2013 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.