none
how to hide the existing links in my webpart RRS feed

  • Question

  • I am new for sharepoint 2010 technology.

    My Requirement is like.

    I have 5 links exist for my webpart.I want to hide one link for particular user.

    Please suggest me how to proceed the above task.


    Rama

    Saturday, September 27, 2014 6:34 AM

Answers

All replies

  • Hi,

    Below code help you to accomplish your task

    string groupName = "Site Visitors";

    SPUser userName = SPContext.Current.Web.CurrentUser;

    string strconcat=string.Empty;

    using (SPSite spSite = new SPSite("http://sp/"))

    {

     using (SPWeb spWeb = spSite.OpenWeb())

      {

        SPUser user = spWeb.EnsureUser(userName.LoginName);

        strconcat = "<html><a href="+"http://www.google.com"+">First</a>";

        if (user.Groups.Cast<SPGroup>().Any(g => g.Name.Equals(groupName)))

        {

          strconcat+="</br></br><a href="+"http://social.msdn.microsoft.com/Forums/sharepoint/en-US/home"+">Second</a>";

        }

        strconcat += "</html>";

        links.InnerHtml = strconcat;

      }

    }



    Thank You,

    Somnath Matere


    Saturday, September 27, 2014 6:01 PM
  • Hi,

    According to your description, you want to hide specific content in a web part for a specific user.

    We can do it in two steps as below:

    1. Get the information of the current logged in user;

    2. Hide controls or content accordingly.

    Here is a link with code demo about how to get information of current login user:

    http://www.c-sharpcorner.com/uploadfile/Chandresh.P/display-information-of-current-login-user-in-sharepoint-site/

    More information about SPUser object:

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spuser_members(v=office.14).aspx

    Best regards,

    Patrick


    Patrick Liang
    TechNet Community Support

    Tuesday, September 30, 2014 7:39 AM
    Moderator
  • Hi,

    what information of that user do you have?

    Regards.

    Tuesday, September 30, 2014 7:58 AM
  • Hi Jorge Munoz Carballo,

    we are fetching the url links data from my custom list.

    And we are dislaying thes URL Hyperlinks,based on condition we are shown some links for particular users.

    in my Custom webpart

    Regards,

    Rama


    Rama

    Tuesday, September 30, 2014 9:52 AM
  • Hi,

    use this after after fetch the links

                switch (SPContext.Current.Web.CurrentUser.LoginName)
                { 
                    case "loginA":
                        Page.FindControl("LinkToHideA").Visible = false;
                        break;
    
                    case "loginB":
                        Page.FindControl("LinkToHideB").Visible = false;
                        break;
    
                    case "loginC":
                        Page.FindControl("LinkToHideC").Visible = false;
                        break;
                }

    Tuesday, September 30, 2014 10:30 AM
  • You have custom web part, so I believe you achieve this requirement with lot of option.
    Already 2 peoples provide you different approaches.
    One more option is you can manage permission in
    list (List Item level permission). Based on access to list item you can fetch
    data and display in your web part.

    Tuesday, September 30, 2014 11:57 AM
  • If the links are in your custom webpart, you cannot use out of box audience targeting to hide or show a link based on the user. Obviously you need to write your own custom code for that.

    You can get the current user details inside SharePoint web part using SPContext class

    SPContext.Current.Web.CurrentUser

    Based on this object you can hide or show a link in your web part


    Please mark the replies as answers if they help or unmark if not.

    Tuesday, September 30, 2014 12:14 PM