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;
        }
    }
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;
  • 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 Jan 22 '13 at 8:58
  • @Kriistiian I forgot that it should new SPFieldUrlValue... instead of just SPFieldUrlValue.... My bad. – user2536 Jan 22 '13 at 9:06
  • This one solved my problem. Thank you very much, thumbs up. – krisal Jan 22 '13 at 9:19
  • Changed LabelLinks.Text = item["Links"].ToString(); to LabelLinks.Text = new SPFieldUrlValue(item["Links"].ToString()).Url; Thanks! – krisal Jan 22 '13 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 Jan 22 '13 at 9:22
3

You have to use SPFieldUrlValue:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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