3

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.

1 Answer 1

2

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.

4
  • 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
    Commented Aug 4, 2012 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. Commented Aug 4, 2012 at 18:28
  • Its not Working Still gives "Error: Access Denied" Commented Aug 6, 2012 at 8:35
  • Can you please explain more ....? Commented Aug 6, 2012 at 8:41

Your Answer

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

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