In my sharepoint site I want to check whether a user is in sharepoint group or not and then only allow it to go to the home page. This should happen when a user goes to my sharepoint site for the first time. I know that it can be achieved through code behind in asp.net but sharepoint does not allow code behind files. How can I achieve this functionality? And if at all I have to use code behind how can I add code behind file to the already existing .aspx pages in sharepoint?
2 Answers
Two options come to my mind:
Implementing a Delegate Control and deploying it to the
AdditionalPageHead
control ID (see here and here for reference)a custom action that inject a piece of javascript, through which you check if the user belongs to some SharePoint group (see here an example of how to do that) and otherwise redirect to the homepage.
I had implemented a similar functionality in one of our Intranet portal where I had to check if the currently login user is a Contractor or a permanent employee. I had done this with a visual studio web part.
You can create a visual studio web part and deploy it to your SharePoint farm. Then insert that web part on your home page and make that web part is hidden through web part settings.
In the web part code, in your Page Load event you can check if the currently login user is part of a group.
Just an example to share with you.
protected void Page_Load(object sender, EventArgs e)
{
//get currently login user
SPUser currentUser = SPContext.Current.Web.CurrentUser;
bool checkUserGrp = isUserMemberOfGroup(currentUser, "Visitors");
if(checkUserGrp)
{
Response.Redirect("youHomePageURL");
}
else //your code goes here
}
//Method to check if current user is member of Visitors group
public bool IsUserMemberOfGroup(SPUser user, string groupName)
{
bool result = false;
if (!String.IsNullOrEmpty(groupName) && user != null)
{
foreach (SPGroup group in user.Groups)
{
if (group.Name == groupName)
{
result = true;
break;
}
}
}
return result;
}
-
this is easily eluded. If the user goes to another page (say, some list DispForm.aspx), then your control won't be triggered Commented Apr 22, 2015 at 7:45