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:
- AllItems
- 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.