locked
How to redirect user to to thier Root site or Subsites RRS feed

  • Question

  • Hello

    I want to redirect user to their respective site.

    1. I have some user who have access of all the sites, so I want them to redirect to root site default page
    2. Some user have only access of subsite(s), so I want to redirect them to their sub site default page whichever sub site comes first

    I have tested following code, which works for point 1 seems to work correctly, but whne it runs for point 2 it redirect to me root page and user will get access denied page rather than its sub site. I am not sure why it is going into first for-next loop in below code.

    Please let me know whats wrong I am doing here

    SPSecurity.RunWithElevatedPrivileges(delegate
    {
         SPSite oSite = SPContext.Current.Site;
         Boolean bolStrflag = false;
         SPWeb oWeb = oSite.OpenWeb();
         foreach (SPUser oUser in oWeb.AllUsers)
         {
              if (bolStrflag == true)
              {
                   break;
              } 
              if (oUser.LoginName.ToUpper() ==  txtUserName.Text.Trim().ToUpper()) 
              {
                   strUserUrl = oSite.Url + oWeb.ServerRelativeUrl.ToString();// 
                   Response.Write(oUser.LoginName+ " Site Users - " + strUserUrl + "<br/>");
                   bolStrflag = true;
                   break;
              }
         }
    
    
         SPWebCollection collWebs = oSite.OpenWeb().GetSubwebsForCurrentUser();
         foreach (SPWeb oWebsite in collWebs)
         {
              if (bolStrflag == true)
              {
                   break;
              }
              SPUserCollection collUsers = oWebsite.AllUsers;
              foreach (SPUser oUser in collUsers)
              {
                   if (oUser.LoginName.ToUpper() ==  txtUserName.Text.Trim().ToUpper()) 
                   {
                        strUserUrl = oSite.Url + oWebsite.ServerRelativeUrl.ToString();
                        Response.Write(oUser.LoginName + " Sub Site Users - " + strUserUrl + "<br/>");
                        bolStrflag = true;
                        break;
                   }
         }
         oWebsite.Dispose();
    
    });
    Tuesday, October 20, 2009 1:46 PM

Answers

  • Avi

    I seen your code and made the correction, make it more simpler. Let me know it works or not
    SPSecurity.RunWithElevatedPrivileges(delegate
                        {
                            SPSite oSite = SPContext.Current.Site;
                            SPWebCollection collWebs = oSite.OpenWeb().GetSubwebsForCurrentUser(); //SUBWEBS
                            foreach (SPWeb oWebsite in collWebs)
                            {
                                if (oWebsite.DoesUserHavePermissions(SPBasePermissions.Open))
                                {
                                    strUserUrl = oSite.Url + oWebsite.ServerRelativeUrl.ToString();
                                    Response.Write(" Sub Site Users - " + strUserUrl + "<br/>");
                                    break;
                                }
                            }
                            SPWeb oWeb = oSite.OpenWeb(); //ROOTWEB
                            if (oWeb.DoesUserHavePermissions(SPBasePermissions.Open))
                            {
                                strUserUrl = oSite.Url + oWeb.ServerRelativeUrl.ToString();// 
                                Response.Write( " Site Users - " + strUserUrl + "<br/>");
                            }
                        });
    • Proposed as answer by Ashish Kanoongo Tuesday, October 20, 2009 4:56 PM
    • Marked as answer by Avian123 Wednesday, October 21, 2009 12:14 AM
    Tuesday, October 20, 2009 3:18 PM
  • In both the loop replace

    (oWebsite.DoesUserHavePermissions(SPBasePermissions.Open))

    with

    (oWebsite.DoesUserHavePermissions(txtUserName.Text,SPBasePermissions.Open))

    Here txtUserName is your username of login page.

    Let me know how it goes?

    Ashish
    • Proposed as answer by Ashish Kanoongo Tuesday, October 20, 2009 4:55 PM
    • Marked as answer by Avian123 Wednesday, October 21, 2009 12:14 AM
    Tuesday, October 20, 2009 4:36 PM

All replies

  • If you look at the documentation for the SPWeb.AllUsers call, it says that it "gets the collection of user objects that represents all users who are either members of the site or who have browsed to the site as authenticated members of a domain group in the site."  So any user that browses to a site where this code is running will be in that list.  (I'm assuming that you are planning to replace txtUserName.text with the login name for the current user)  As such, the current user will always be found in the "foreach (SPUser oUser in oWeb.AllUsers)" loop, and the bolStrFlag will be set to true in the first loop.  As a result, the second loop will never do anything.

    However, I don't understand where all the pieces are.  On what site will this code be running, and how does it relate to the sites you want to redirect users to?

      



    Tuesday, October 20, 2009 3:12 PM
  • If you see the code, I am creating strurl variable which I will use for redirection.
    Tuesday, October 20, 2009 3:16 PM
  • Avi

    I seen your code and made the correction, make it more simpler. Let me know it works or not
    SPSecurity.RunWithElevatedPrivileges(delegate
                        {
                            SPSite oSite = SPContext.Current.Site;
                            SPWebCollection collWebs = oSite.OpenWeb().GetSubwebsForCurrentUser(); //SUBWEBS
                            foreach (SPWeb oWebsite in collWebs)
                            {
                                if (oWebsite.DoesUserHavePermissions(SPBasePermissions.Open))
                                {
                                    strUserUrl = oSite.Url + oWebsite.ServerRelativeUrl.ToString();
                                    Response.Write(" Sub Site Users - " + strUserUrl + "<br/>");
                                    break;
                                }
                            }
                            SPWeb oWeb = oSite.OpenWeb(); //ROOTWEB
                            if (oWeb.DoesUserHavePermissions(SPBasePermissions.Open))
                            {
                                strUserUrl = oSite.Url + oWeb.ServerRelativeUrl.ToString();// 
                                Response.Write( " Site Users - " + strUserUrl + "<br/>");
                            }
                        });
    • Proposed as answer by Ashish Kanoongo Tuesday, October 20, 2009 4:56 PM
    • Marked as answer by Avian123 Wednesday, October 21, 2009 12:14 AM
    Tuesday, October 20, 2009 3:18 PM
  • In both the loop replace

    (oWebsite.DoesUserHavePermissions(SPBasePermissions.Open))

    with

    (oWebsite.DoesUserHavePermissions(txtUserName.Text,SPBasePermissions.Open))

    Here txtUserName is your username of login page.

    Let me know how it goes?

    Ashish
    • Proposed as answer by Ashish Kanoongo Tuesday, October 20, 2009 4:55 PM
    • Marked as answer by Avian123 Wednesday, October 21, 2009 12:14 AM
    Tuesday, October 20, 2009 4:36 PM
  • Ashish

    Thanks, it works very well as I was looking for.

    Wednesday, October 21, 2009 12:14 AM
  • Hi Ashish,

    Thanks for your reply, its very useful to me.

    In my project i used above concept to redirect current Login user to subsites.  But here i have a problem to check weather user have the permission in the subsites or not, i used the following code to check the user permission condition(oWebsite.DoesUserHavePermissions(txtUserName.Text,SPBasePermissions.Open)) but it returns a error like User Does not Exists or is not Unique .

    Note : In my site the users are Form Based Authentication Users

    The Following is my code :

    SPSecurity.RunWithElevatedPrivileges(delegate
                        {
                            SPSite oSite = SPContext.Current.Site;
                           
                            SPWebCollection collWebs = oSite.OpenWeb().GetSubwebsForCurrentUser(); //SUBWEBS
                            foreach (SPWeb oWebsite in collWebs)
                            {
                                try
                                {
                                    if (oWebsite.DoesUserHavePermissions(txtUserName.Text,SPBasePermissions.Open))
                                    {
                                        count++;
                                        string strUserUrl = oSite.Url + oWebsite.ServerRelativeUrl.ToString();
                                        Response.Redirect(strUserUrl);
                                        //Response.Write(" Sub Site Users - " + strUserUrl + "<br/>");
                                        break;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Response.Redirect(site.Url);
                                }
                            }

    Please Help how to resolve this Problem.

    Thanks

    Srinivas

     


    MO(LA)DDU

    Thursday, July 5, 2012 10:43 AM