locked
To retrieve the name of the list using List ID RRS feed

  • Question

  • Hi All,

       I want to display the list names(linked to the specific list) which is updated recently.

       Using Caml query i retrieved the list ID's and  list items from various lists.

       But I am not able to display the list name and get the URL of the list.

      Code to retrieve the list id's and title of the list items  

       SPSiteDataQuery siteQuery = new SPSiteDataQuery(); 
       siteQuery.Webs =
    "<Webs Scope=\"Recursive\">"; 
       siteQuery.Lists =
    "<Lists ServerTemplate=\"101\" />"; 
       siteQuery.ViewFields =
    "<FieldRef Name=\"Title\" /><FieldRef Name=\"Modified\" /><FieldRef Name=\"GUID\" />"; 
       siteQuery.Query =
    "<Where><Geq><FieldRef Name=\"Modified\"/><Value Type=\"DateTime\"><Today OffsetDays= \"-15 \"/></Value></Geq></Where><OrderBy><FieldRef Name='Modified' Ascending='True'/></OrderBy>"; 
      dtTable = web.GetSiteData(siteQuery);
    

        

         Thanks in Advance

    • Moved by Mike Walsh FIN Monday, April 25, 2011 9:51 AM not general (From:SharePoint - General Question and Answers and Discussion (pre-SharePoint 2010))
    Monday, April 25, 2011 8:22 AM

Answers

  • Hi Vanitha A,

    If you can access List Id, you can get List Name by this code:

    SPSite  site = new SPSite("[site url]"

    );

     

    SPWeb web = site.OpenWeb();

    String listName = "" 

    ;

    foreach (SPList list in 

    web.Lists)

    {

    if (list.ID == '[your list Id]')

    listName = list.Title;

    }

    listname is your list name

     

     


    • Marked as answer by Vanitha A Tuesday, April 26, 2011 4:45 AM
    Monday, April 25, 2011 10:25 AM
  • What is control you are using while binding the data.

    Lets say if you are using gridview then on row data bind events you can get the title of the list as follows :

    string sListTitle=SPContext.Current.Web.Lists[new Guid(sGuid)].Title.ToString();

     


    pankaj
    • Marked as answer by Vanitha A Tuesday, April 26, 2011 4:45 AM
    Monday, April 25, 2011 4:11 PM

All replies

  • Hi Vanitha A,

    If you can access List Id, you can get List Name by this code:

    SPSite  site = new SPSite("[site url]"

    );

     

    SPWeb web = site.OpenWeb();

    String listName = "" 

    ;

    foreach (SPList list in 

    web.Lists)

    {

    if (list.ID == '[your list Id]')

    listName = list.Title;

    }

    listname is your list name

     

     


    • Marked as answer by Vanitha A Tuesday, April 26, 2011 4:45 AM
    Monday, April 25, 2011 10:25 AM
  • Hi Alireza,

       Thanks for your reply.

        But when i use this code,the list id in the result set does not match with any of the list ID's.

        I Dont know where i am going wrong.

     

     string listname = null;
    
      int rowcount = dtTable3.Rows.Count;
    
      Guid guid = new Guid();
    
      for (int i = 0; i < rowcount; i++)
      {
    
       guid = new Guid(dtTable3.Rows[i].ItemArray[0].ToString());
       
       foreach (SPList list in web.Lists)
       {
        if (list.ID == guid)
        {
         listname = list.Title.ToString();
        }
       }
      }
    

     

    Monday, April 25, 2011 1:15 PM
  • please tell me, type dtTable3, is this Datatable ?
    Monday, April 25, 2011 1:31 PM
  • Yes.Its a datatable.
    Monday, April 25, 2011 1:35 PM
  • debug your code then add watch dtTable3.Rows[i].ItemArray[0].ToString() and new Guid(dtTable3.Rows[i].ItemArray[0].ToString()) , Does watch return correct, by the way i suggest you, you use my code, i test my code in VS before,

    Regards.

    Monday, April 25, 2011 1:49 PM
  • What is control you are using while binding the data.

    Lets say if you are using gridview then on row data bind events you can get the title of the list as follows :

    string sListTitle=SPContext.Current.Web.Lists[new Guid(sGuid)].Title.ToString();

     


    pankaj
    • Marked as answer by Vanitha A Tuesday, April 26, 2011 4:45 AM
    Monday, April 25, 2011 4:11 PM