Skip to main content

SharepointLabs.com Blog on MOSS - WSS 3.0 sharing experience, technology, news, views and more

Go Search
Home
Buy Webparts
SharePoint Training
Contact Us for Consulting Inquiry
  

SharepointLabs.com Blog on MOSS - WSS 3.0 sharing experience, technology, news, views and more > Posts > Filter list view using invisible webpart where list will get filter according to current logged in user : wss 3.0 (SPQuery)
Filter list view using invisible webpart where list will get filter according to current logged in user : wss 3.0 (SPQuery)

In below code case is : we have "method type" field in list methods are method1,method2, method3. Now for each method we have separate groups and if you want to filter view according method for logged in user then create one webpart without interface and place this webpart on just above list view webpart on screen and debug check filteration.

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace ViewWebPart
{
public class ViewOnRights : System.Web.UI.WebControls.WebParts.WebPart
{

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWeb w = SPContext.Current.Web;
SPUser currentUser = w.CurrentUser;

if (!currentUser.IsSiteAdmin)
{
using (SPSite ElevatedSiteColl = new SPSite(w.Site.ID))
{
SPUser targetUser = ElevatedSiteColl.SystemAccount;
SPUserToken token = targetUser.UserToken;
using (SPSite SiteColl = new SPSite(ElevatedSiteColl.ID, token))
{
using (SPWeb web = SiteColl.OpenWeb(w.ID))
{
web.AllowUnsafeUpdates = true;

string method1 = string.Empty;
string method2 = string.Empty;
string method3 = string.Empty;
string strQuery = string.Empty;

SPGroupCollection userGroups = currentUser.Groups;
foreach (SPGroup grp in userGroups)
{
if (grp.Name.Substring(0, 3).ToLower().Equals("method1"))
{
method1 = "method1";
}
else if (grp.Name.Substring(0, 7).ToLower().Equals("method3"))
{
method3 = "method3";
}
else if (grp.Name.Substring(0, 8).ToLower().Equals("method2"))
{
method2 = "method2";
}
}
SPQuery query = new SPQuery();
strQuery = "";
strQuery += "" + method1 + "";
strQuery += "" + method3 + "";
strQuery += "" + method2 + "";
strQuery += "";

query.Query = strQuery;
SPView view = web.Lists["List name on which you want to do filteration"].Views["All Items"];
view.Query = query.Query;
view.Update();

if (Page.Session["User"] == null)
{
Page.Session["User"] = currentUser.LoginName.ToString();
Page.Response.Redirect(Page.Request.Url.ToString());
}
else if (Page.Session["User"].ToString() != currentUser.LoginName.ToString())
{
Page.Session["User"] = currentUser.LoginName.ToString();
Page.Response.Redirect(Page.Request.Url.ToString());
}
}
}
}
}
else
{
SPWeb web = SPContext.Current.Web;
web.AllowUnsafeUpdates = true;
SPQuery query = new SPQuery();
query.Query = "" + "" + "method1" + "" + "" + "method3" + "" + "" + "method2" + "";
//query.Query = "" + "" + "method3" + "";
SPView view = web.Lists["List name on which you want to do filteration"].Views["All Items"];
view.Query = query.Query;
view.Update();


if (Page.Session["User"] == null)
{
Page.Session["User"] = currentUser.LoginName.ToString();
// string script = "";
// Page.RegisterStartupScript("Refresh", script);


}
else if (Page.Session["User"].ToString() != currentUser.LoginName.ToString())
{
Page.Session["User"] = currentUser.LoginName.ToString();
// string script = "";
// Page.RegisterStartupScript("test", script);
}


}//currentUser.IsSiteAdmin
}//RunWithElevatedPrivileges
); //delegate ends
}//Render
}//class
}//namespace

Posted by Vaibhav Jain (Success is always one step ahead)

Comments

There are no comments yet for this post.
Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Body *


Attachments