locked
GetItems is empty RRS feed

  • Question

  • List<Language> languages = new List<Language>();

    SPView view = List.Views["LookupView"];

    foreach(SPListItem item in List.GetItems(view))

    languages.Add(new Language(item.ID, item.Title, Convert.ToInt32(item["SortOrder"])));

    return languages;

    The code above is where I'm having a problem. I set up a custom view on one of my list in sharepoint, and when I try to retrieve list items from this view using GetItems, nothing returns. The view exists in the list. List is a SPList object which is created by using the method GetList, which retrieves an object references for the list that I want to use. Any ideas?

    Thanks,


    Love From The Hood
    Friday, March 13, 2009 6:30 PM

Answers

  • Howdy,

    If the user dosent have the permission to view the list items in particular list, your object model code wont retrieve the items in that list.

    If you want to retrieve the item for the user who dosent has the permission, you need to push entire code into SPSecurity.RunWithElevatedPrivileges

    Here is the code sample:

                SPSecurity.RunWithElevatedPrivileges(delegate()  
                {  
                    SPSite site = new SPSite(url);  
     
                    using (site)  
                    {  
                        SPWeb web = site.OpenWeb();  
     
                        using (web)  
                        {  
                            //Type your required code here  
                        }  
                    }  
     
                }); 

    Hope this helps.


    Gopinath D - http://sharepointsnippets.com
    Monday, March 16, 2009 2:26 PM

All replies

  • Howdy,

    Make sure the following things:

    1. Can you to see the list items in the browser for this view?
    2. If you can see the list items for this view; Run this code for the same user context which you used to view the items in the browser.
        I mean login with the server using the same user id and execute the code.

    Thanks,

    Gopinath D - http://sharepointsnippets.com
    Friday, March 13, 2009 8:25 PM
  • Well my code retrieve list items from multiple lists, so its only just this one list that is causing me trouble. The werid thing is that I'm using the same user. Its not a user in Sharepoint, so its consider a guest user?


    Love From The Hood
    Monday, March 16, 2009 1:31 PM
  • Howdy,

    If the user dosent have the permission to view the list items in particular list, your object model code wont retrieve the items in that list.

    If you want to retrieve the item for the user who dosent has the permission, you need to push entire code into SPSecurity.RunWithElevatedPrivileges

    Here is the code sample:

                SPSecurity.RunWithElevatedPrivileges(delegate()  
                {  
                    SPSite site = new SPSite(url);  
     
                    using (site)  
                    {  
                        SPWeb web = site.OpenWeb();  
     
                        using (web)  
                        {  
                            //Type your required code here  
                        }  
                    }  
     
                }); 

    Hope this helps.


    Gopinath D - http://sharepointsnippets.com
    Monday, March 16, 2009 2:26 PM