locked
Custom webpart for redirecting to user (How to query to List) RRS feed

  • Question

  • Hi all,

    Iam creating the custom webpart using( Visual WebPar tProject template in visual studio 2010) . i have created the custom list with

    user Department and Redirect URL

    Now i have to query to List against loged In user and based upon the department of login person i have to redirect to his/her particular site.

    Now How i query to list for department?

    Any Idea or article link will be apperciated

    Regards,

    Tuesday, July 24, 2012 6:14 AM

Answers

  • Hi,

    This can be done like this:

    - get the current user department from the user profiles:

    SPServiceContext serviceContext = SPServiceContext.GetContext(SPContext.Current.Site);
    UserProfileManager upm = new UserProfileManager(serviceContext);
    UserProfile userProfile = upm.GetUserProfile(SPContext.Current.Web.CurrentUser.LoginName);
    string department = Convert.ToString(userProfile["Department"]);
    

    - query the list to get the list item corresponding to your department:

    SPList departmentsList = SPContext.Current.Web.Lists["Departments"];
    SPQuery query = new SPQuery();
    query.Query = String.Format("<Where><Eq><FieldRef Name='Department' /><Value Type='Text'>{0}</Value></Eq></Where>", department);
    query.ViewFields = "<FieldRef Name='Department' />";
    SPListItemCollection items = departmentsList.GetItems(query);
    
    foreach (SPListItem item in items)
    {
    	// your operation here
    }
    

    Of course you need to change this to put the correct field names and user profile properties.

    Cheers,

    Dan.


    You can find my blog here: http://developertrack.blogspot.com

    • Proposed as answer by Borislav Grgić Tuesday, July 24, 2012 7:15 AM
    • Marked as answer by Qiao Wei Friday, August 3, 2012 10:25 AM
    Tuesday, July 24, 2012 7:05 AM
  • Hi,

    >Now suggest me the steps how do i get the current logged in user department and query to list?

    You already have this in Dan post just look at there. This code will give you current user department name from AD then you can pass same department name in list using SPQuery to get respective department name and url.

    Let us know if you any issue


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"

    • Marked as answer by Qiao Wei Friday, August 3, 2012 10:25 AM
    Wednesday, July 25, 2012 10:31 AM

All replies

  • Hi,

    This can be done like this:

    - get the current user department from the user profiles:

    SPServiceContext serviceContext = SPServiceContext.GetContext(SPContext.Current.Site);
    UserProfileManager upm = new UserProfileManager(serviceContext);
    UserProfile userProfile = upm.GetUserProfile(SPContext.Current.Web.CurrentUser.LoginName);
    string department = Convert.ToString(userProfile["Department"]);
    

    - query the list to get the list item corresponding to your department:

    SPList departmentsList = SPContext.Current.Web.Lists["Departments"];
    SPQuery query = new SPQuery();
    query.Query = String.Format("<Where><Eq><FieldRef Name='Department' /><Value Type='Text'>{0}</Value></Eq></Where>", department);
    query.ViewFields = "<FieldRef Name='Department' />";
    SPListItemCollection items = departmentsList.GetItems(query);
    
    foreach (SPListItem item in items)
    {
    	// your operation here
    }
    

    Of course you need to change this to put the correct field names and user profile properties.

    Cheers,

    Dan.


    You can find my blog here: http://developertrack.blogspot.com

    • Proposed as answer by Borislav Grgić Tuesday, July 24, 2012 7:15 AM
    • Marked as answer by Qiao Wei Friday, August 3, 2012 10:25 AM
    Tuesday, July 24, 2012 7:05 AM
  • Hi,

    As per my understanding, you want to redirect user another page based on dropdown value selection. If so then you can use Jquery for same.

    Try below script:

    <script language="javascript" type="text/javascript" src=" /Shared%20Documents/jquery-1.3.2.min.js"></script>
    <script language="javascript" type="text/javascript" src=" /Shared%20Documents/jquery.SPServices-0.5.6.min.js"></script>
    <script>
    jQuery(document).ready(function(){
    jQuery("#dropdown").change(function(e){
    window.location.href = jQuery("#dropdown").val();
    });});
    </script>
    
    <select class="dropdown" id="dropdown">
    <option value="#" selected="selected">Choose a Topic</option>
    <option value="/home.aspx">Introduction</option>
    <option value="/Treatment/OrthodonticAppliances.aspx#Headgear">Headgear</option>
    <option value="/Treatment/OrthodonticAppliances.aspx#Removable">Removable Appliances</option>
    <option value="/Treatment/OrthodonticAppliances.aspx#ExpandersArch">Rapid Palatal Expanders &amp; Lower Lingual Arch</option>
    <option value="/Treatment/OrthodonticAppliances.aspx#Bionator">Bionator and Twin Block Appliance</option>
    <option value="/Treatment/OrthodonticAppliances.aspx#Schwartz">Schwartz &amp; Sagittal Appliances</option>
    </select>

    Let us know your result


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"

    Tuesday, July 24, 2012 7:06 AM
  • Thanks for reply,

    Actually i have to redirect the user based upon the his/her department ( subsites for each department).

    Now on the Top Level site i have to add the custom webpart which will query to list for getting the redirect url(subiste url)  based upon the department.

    i have site collection URL

    http:// server :port /home.apsx

    subsite urls

    http://server:port/sites/HR/home.aspx

    http://server:port/sites/sales/home.aspx

    Now when User HR access the Top site URL he must be redirect to HR subsite. for doing so i have managed the custom list for department and redirect url.

    and using the custom webpart i have to query agaiinst the list and compare the logged in department with the list column department and redirect.

    is this will be right approach? please suggest the best approach.

    Regards,

    Tuesday, July 24, 2012 7:15 AM
  • Hi,

    You can use Dan code, where you can check the current user login group and based on this you can show the department name in dropdown. Perhaps you may use jquery to link all the department for redirection.


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"

    Wednesday, July 25, 2012 4:44 AM
  • Thanks Hemendra for your reply,

    Why i should use the dropdown? i simply have to get the current user department name and based on that i have to query to custom list ( where the department and associate link is stored) and redirect the user .

    Now suggest me the steps how do i get the current logged in user department and query to list?

    Just guide me or prvide me the links from where i can learn these things my self.

    I just start programming in sharepoint thats why not clear about the things any help will be apperciated.

    Regards,

    Wednesday, July 25, 2012 6:45 AM
  • Hi,

    >Now suggest me the steps how do i get the current logged in user department and query to list?

    You already have this in Dan post just look at there. This code will give you current user department name from AD then you can pass same department name in list using SPQuery to get respective department name and url.

    Let us know if you any issue


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"

    • Marked as answer by Qiao Wei Friday, August 3, 2012 10:25 AM
    Wednesday, July 25, 2012 10:31 AM