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

I Have Created 2 Views for a List. Now I want to set Different views as the default view on different Logins.

I want to do this using c# code

For Example:

Views are as Following:

  1. AllItems
  2. UserView

Then

If Admin Logs in then List's Default View is AllItems

If User Logs in then List's Default View is UserView

How Can I do this using C# code?

Code Snippet:

                    SPUser LoggedInUser = web.CurrentUser;

                    SPList CurrentList = web.Lists[ListName];

                    if (LoggedInUser.LoginName == LoginUser)
                    {
                        web.AllowUnsafeUpdates = true;
                        CurrentList.Views[AllItems].DefaultView = false;
                        CurrentList.Views[UserView].DefaultView = true;
                        CurrentList.Update();              // Error here
                        web.AllowUnsafeUpdates = false;

                    }
                    else if (LoggedInUser.LoginName == LoginAdmin)
                    {
                        web.AllowUnsafeUpdates = true;
                        CurrentList.Views[AllItems].DefaultView = true;
                        CurrentList.Views[UserView].DefaultView = false;
                        CurrentList.Update();               // Error here
                        web.AllowUnsafeUpdates = false;
                    }

Above Code is giving this error:

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.

share|improve this question
add comment

1 Answer

I think the problem here is you're retrieving the view from an indexer. Try something like this for each view:

SPView allItemView = CurrentList.Views[AllItems];
allItemView.DefaultView = false;
allItemView.Update();
CurrentList.Update();

Additionally - this is database-heavy in terms of what you want to do (lots of read/writes just to set a default view depending on the user's group), I can't think at the minute of a better way of doing this but if there is, then try and find a way that limits the amount of data you need to get to/from the database. Think what impact this will have many when users are accessing the page at once.

share|improve this answer
    
An edit was suggested for this answer to put an Update statement for the parent list. I don't actually think this is required as the SPView's update statement should commit changes anyway. I do not have a VM to hand currently so I can't confirm this, however. –  James Love Aug 4 '12 at 16:47
    
I also do not have a VM ready at hand. If anybody can confirm that there is no need of updating the parent list, I can revert the edit. –  Vardhaman Deshpande Aug 4 '12 at 18:28
    
Its not Working Still gives "Error: Access Denied" –  Vikalp Kapadiya Aug 6 '12 at 8:35
    
Can you please explain more ....? –  Vikalp Kapadiya Aug 6 '12 at 8:41
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.