Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

The following bit of code throws an Unauthorized Operation exception when I try to enumerate the items in the list. Any ideas?

        SPSecurity.RunWithElevatedPrivileges(delegate() {
            using (SPSite site = new SPSite("http://asiteurl"))
            {
            SPList profileList = null;

            try
            {
                profileList = site.RootWeb.Lists["Profiles"];
            }
            catch 
            {
                System.Diagnostics.Debug.WriteLine("An error occurred while locating the 'Profiles' list in the RootWeb of the site collection.");
            }

            var events = new List<Event>();

            if (profileList != null)
            {
                foreach (SPListItem item in profileList.Items) //Error occurs here
                {
            //Do Stuff
                }
            }
            }            
        });
share|improve this question
1  
Do you have any problems using this list through the UI? Have you verified that the permissions to access the external datasource are setup properly? –  MBSurf Jun 21 '11 at 16:47
    
Is asiteurl within the web application that this code is running inside? –  Andy Burns Jun 21 '11 at 17:43
    
@MBSurf the forms in the UI work without issues –  jjr2527 Jun 21 '11 at 19:08
    
@andy I removed the real url, in the actual code yes it is –  jjr2527 Jun 21 '11 at 19:09
    
Besides the question, but you can use Lists.TryGetList("Profiles") which won't throw an exception. –  tarjeieo Jun 22 '11 at 7:06
show 1 more comment

3 Answers

Does the application pool account have access to the external source ? Because you are elevating privileges means that you will be running under the app pool account context. Try removing the elevated code and run it with a user that you are 100% sure that can update from the UI.

Hope this helps

share|improve this answer
add comment

Are you sure you're not hitting the list throttling limits? 2000 items if I recall correctly.

share|improve this answer
    
No, there are only 4 items in the list. I was setting up a POC to show how easy it is to work with external lists of your business data, so far not so easy :) –  jjr2527 Jun 22 '11 at 14:39
add comment

Few things:

You are using SPRunWithElevatedPrivileges where the code will use the application pool account to access the list.

This will depend totally on the authentication option chosen for external system. If chosen current user's identity, then you have to give appropriate rights to application pool account to acces the external system. That said, if it is current user's identity, the code should work without SPRunWithElevatedPrivileges.

It would be helpful if you are able to explain the authentication optin chosen for the external system.

Here is something that might help you visualise (even though it is sandbox solution, it is very similar for a farm solution) what is happening: http://msdn.microsoft.com/en-us/library/ff798426.aspx

P.S: As a cood code practice, it is better not to enumerate the SPList.Items directly in a loop, instead save it in a object and enumerate the object.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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