Custom list field changing URL to anchor tag

I'm trying to figure out exactly what is doing this, whether it is SharePoint 2013, or perhaps the browser.

If I create a custom list and I have a field "Single Line of Text" and enter something in the line that contains a URL reference, such as "The URL to Facebook is http://www.facebook.com", when I read the data back from that SharePoint list (using the client objects for example), the field contains:

The URL to Facebook is <a href="http://www.facebook.com">http://www.facebook.com</a>

I want to get back exactly what I typed! Is it SharePoint that is changing it to an anchor tag in the text or is it something else? How can I get it to store exactly what I typed?

September 3rd, 2015 5:06pm

Hi,

According to your description, I also tested in my environment.

I created a new Single Line of Text column named html and type http://www.bing.com. Then I retrieved values of html column using Client Object Model via VS. And the result is http://www.bing.com.

The code is:

static void Main(string[] args)
        {
            ClientContext clientContext = new ClientContext("SiteURL");
            Microsoft.SharePoint.Client.List spList = clientContext.Web.Lists.GetByTitle("ListName");
            clientContext.Load(spList);
            clientContext.ExecuteQuery();
            if (spList != null && spList.ItemCount > 0)
            {
                Microsoft.SharePoint.Client.CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml =
                  @"<View>
             <ViewFields><FieldRef Name='Title' /><FieldRef Name='html' /></ViewFields> 
      </View>";  
                ListItemCollection listItems = spList.GetItems(camlQuery);
                clientContext.Load(listItems);
                clientContext.ExecuteQuery();
                foreach (ListItem item in listItems)
                {
                    clientContext.Load(item);
                    clientContext.ExecuteQuery();
                    Console.WriteLine((string)item["ColumnName"]);
                }
                Console.ReadKey();
            }
        }

Did you copy the URL reference and paste it in SharePoint list? It may contain <a> tag in original data. Please try to type URL manually.

And please refer to the code I provided, then test whether the <a> tag occurs.

Best Regards,

Dean Wang

Free Windows Admin Tool Kit Click here and download it now
September 3rd, 2015 11:18pm

A few clarifications:

1) I created a custom list and added a field called "URL" as a Single Line of Text.

2) I added a new item to the list and I manually typed http://www.facebook.com into the URL field.

3) When the list view displays, the URL column now shows http://www.facebook.com as a clickable hyperlink.

4) When I said Client Object Model, I was not clear/incorrect. I am actually using JavaScript and JSLink to access the data using a template override.

My code is like:

myOverride.Template.Item = myView.itemHtml;
..
..
myView.itemHtml = function(ctx) {
   var returnHtml = ctx.CurrentItem.URL;
   alert(returnHtml);  // testing
   return returnHtml;
}

When I refer to this code in the JSLink of the list view web part, the "alert" function displays the value of the URL field as <a href="http://www.facebook.com">http://www.facebook.com</a>, not what I typed - at some point, it gets converted to an anchor tag.

September 4th, 2015 12:38pm

Hi,

Since the code you provided is fragmented, I tested in my environment using my code and the result is without <a> tag. Please refer to the code below:

<script type="text/javascript">  

var siteUrl = 'SiteURL';

 $(document).ready(function () {

SP.SOD.executeFunc('sp.js', null, function () {

    retrieveListItems();

});

});

 function retrieveListItems() {

    var clientContext = new SP.ClientContext(siteUrl);

    var oList = clientContext.get_web().get_lists().getByTitle('ListName');        

    var camlQuery = new SP.CamlQuery();

    camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' + 

        '<Value Type=\'Number\'>1</Value></Geq></Where></Query><RowLimit>10</RowLimit></View>');

    this.collListItem = oList.getItems(camlQuery);        

    clientContext.load(collListItem,'Include(ColumnName)');

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));               

}


function onQuerySucceeded(sender, args) {

    var listItemInfo = '';

    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {

        var oListItem = listItemEnumerator.get_current();

        listItemInfo = oListItem.get_item('ColumnName');

    }

    alert(listItemInfo.toString());

}


function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());

}

</script>

The result:

And how to retrieve list items using JavaScript for your reference:

https://msdn.microsoft.com/en-us/library/office/hh185007(v=office.14).aspx

Best Regards,

Dean Wang

Free Windows Admin Tool Kit Click here and download it now
September 7th, 2015 1:06am

Thanks, but as I said in my reply, I am using JSLink in a list view web part to do this.

Create a file named links.js in the site's Site Assets library with this code in it:

var myViews = myViews || {};

myViews.itemHtml = function (ctx) {
 	alert(ctx.CurrentItem.URL);

	return ctx.CurrentItem.URL;
};

(function () {

  var myOverrides = {};
  myOverrides.Templates = {};

  myOverrides.Templates.Header = "<div id='LinksView'>";
  myOverrides.Templates.Item = myViews.itemHtml;
  myOverrides.Templates.Footer = "</div>";
  
  myOverrides.ListTemplateType = 100;
  myOverrides.BaseViewID = 1;
  
  SPClientTemplates.TemplateManager.RegisterTemplateOverrides(myOverrides);
})();

Add your custom list's list view web part to the site's page and edit the web part's properties and open the Miscellaneous section and type "~site/SiteAssets/links.js" in that field and save the web part properties and the page.

Then refresh the page.

You should get a message box with "The URL to Facebook is <a href="http://www.facebook.com">http://www.facebook.com</a>". Which is not what is in the field.

September 8th, 2015 2:49pm

Hi,

Yes, if you retrieve list items using JSLink, the results will return <a> tag.

I suggest you retrieve text of hyperlinks using JSLink regular expression.

Here is a demo for your reference:

http://www.mkyong.com/regular-expressions/how-to-extract-html-links-with-regular-expression/

Best Regards,

Dean Wang

Free Windows Admin Tool Kit Click here and download it now
September 10th, 2015 2:02am

Yes, regex is one way to do it. I worked around it by using a Links list instead of a custom list.

The plain URL is in ctx.CurrentItem.URL and the link description is in ctx.CurrentItem["URL.desc"].

(Also in my sample code, the ListTemplateType has to change to 103.)

September 10th, 2015 9:25am

Hi,

Thanks for you shared your workaround. And it would be helpful to those who have similar requirement.

Best Regards,

Dean Wang

Free Windows Admin Tool Kit Click here and download it now
September 10th, 2015 9:52pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics