1

I got a hyperlink column in my list. Then i got a search to the list and when it shows the answer in a textbox i get duplicate. Lets say, i'm saving http://www.google.se to the list, but when i get the answer after searching in the list it shows http://www.google.se http://www.google.se . any solutions?

Code in my search button:

    protected void ButtonChoose_Click(object sender, EventArgs e)
    {
        try
        {
            using (SPSite site = new SPSite("http://knowitintranet:9797/sites/News"))
            {
                LabelAuthor.Text = string.Empty;
                LabelShowTitle.Text = string.Empty;
                LabelShowDate.Text = string.Empty;
                LabelContent.Text = string.Empty;
                LabelLinks.Text = string.Empty;

                SPWeb web = SPContext.Current.Web;
                SPList list = web.Lists.TryGetList(DropDownListCategory2.SelectedItem.Value);

                SPListItemCollection items = list.GetItems();

                foreach (SPListItem item in items)
                {
                    LabelShowTitle.Text = item["Title"].ToString();
                    LabelAuthor.Text = item["Authors"].ToString();
                    LabelContent.Text = item["Contents"].ToString();
                    LabelLinks.Text = item["Links"].ToString();
                    LabelShowDate.Text = item["Date"].ToString();
                }

                site.Dispose();
            }
        }
        catch (Exception x)
        {
            LabelException.Text = x.Message;
        }
    }

2 Answers 2

4

That's because a hyperlink column saves 2 values; Link itself, link description.

If link description is not filled out when creating a new item it will automatically copy the URL from the URL field down to the description field and thus return both when requested.

This can be solved by converting item["Links] to a SPFieldUrlValue field:

LabelLinks.Text = new SPFieldUrlValue(item["Links"].ToString()).Url;
5
  • When changing LabelLinks.Text = item["Links"].ToString(); to LabelLinks.Text = SPFieldUrlValue(item["Links"].ToString()).Url; i get an error: "Method, delegate or event is expected"
    – krisal
    Commented Jan 22, 2013 at 8:58
  • @Kriistiian I forgot that it should new SPFieldUrlValue... instead of just SPFieldUrlValue.... My bad.
    – user2536
    Commented Jan 22, 2013 at 9:06
  • This one solved my problem. Thank you very much, thumbs up.
    – krisal
    Commented Jan 22, 2013 at 9:19
  • Changed LabelLinks.Text = item["Links"].ToString(); to LabelLinks.Text = new SPFieldUrlValue(item["Links"].ToString()).Url; Thanks!
    – krisal
    Commented Jan 22, 2013 at 9:19
  • Any idea which control i should use so i can open the link that shows up? I've tried "linkbutton" but it only refreshes the current page!
    – krisal
    Commented Jan 22, 2013 at 9:22
3

You have to use SPFieldUrlValue:

SPFieldUrlValue value = new SPFieldUrlValue(item["Links"].ToString());
string url= value.Url;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.