Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm currently making some customizations to a Document Library in SharePoint 2013, such as a custom filtering webpart. In this Library, I have two fields that originally were (multi)lookup fields containing various labels. The items in these fields are displayed as hyperlinks leading to their respective item.

This have me the idea to, instead, create hyperlinks which automatically filter the Document Library according to the label that was clicked.

My first attempt at this consisted of changing the field type to SPFieldMultiLineText and then generating some hyperlinks that call a C# method, applying the filter.

Examples I found while googling didn't seem to work, though. A lot of people suggest creating LinkButtons, but I think the SPFieldMultiLineText can't handle ASP, only HTML. On the other hand, the Hyperlink column type seems to support only a single item.

I was hoping someone here with more experience in SharePoint might have an idea how I could realize this!

Thanks in advance!

David

EDIT:

Here's an example of the type of code I've tried

SPFieldMultiLineText richTextField = docItem.Fields.GetField("Personal Labels") as SPFieldMultiLineText;
richTextField.AllowHyperlink = true;
richTextField.RichTextMode = SPRichTextMode.FullHtml;                                        
String richTextFieldAsHTML = richTextField.GetFieldValueAsHtml(docItem["Personal Labels"], docItem);

String linkHTML = "<a href='#' runat='server' onclick='MethodToCall()'>" + labelItem.DisplayName + "</a>";                             

richTextFieldAsHTML += linkHTML;

currentDocumentLibrary.ParentWeb.AllowUnsafeUpdates = true;
docItem["Personal Labels"] = richTextFieldAsHTML;
docItem.Update();
currentDocumentLibrary.ParentWeb.AllowUnsafeUpdates = false;

With the linkHTML string being the most important part. This example only gives a Hyperlink with the desired name, that does nothing when clicked.

share|improve this question
    
where is MethodToCall() implemented? –  L.V. Sharepoint Architect Apr 19 '13 at 11:42
    
On the codebehind page, just like the example code. –  David Apr 19 '13 at 11:43
    
in which event are you executing this code? –  L.V. Sharepoint Architect Apr 19 '13 at 11:44
    
the a tag does not need runatserver, thats for asp net controls only, after MethodToCall() put ; –  L.V. Sharepoint Architect Apr 19 '13 at 11:46

3 Answers 3

  1. You need to add a Javascript file somewhere, probably in the master page would be the bst idea. in that javascript file you add your MethodToCall()
share|improve this answer
    
I was considering an approach like that, although it seemed a bit hackish to me. Nonetheless, I'll give it a try! Thanks. –  David Apr 19 '13 at 11:46
    
So I've tried out this approach, unfortunately it doesn't seem to work as intended. When adding a hyperlink to the field, the onclick event seems to get stripped somewhere along the way, for some unknown reason. Viewing the list in IE and pressing F12 shows that the required javascript is present, as well as the hyperlink, but the onclick event is no longer there. –  David Apr 19 '13 at 14:10

This is an excellent case for JSLink!

You will just add an JSLink reference to a .js file in the ListView Webpart settings.

This .js file then needs to look something like this.

window.Something = window.Something || {};

window.Something.JSLinkTemplates = window.Something.JSLinkTemplates || {};

// Anonymous function registering the template on load
(function () {
    var overrideCtx = {};
    overrideCtx.Templates = {};

    overrideCtx.Templates.Fields = {
        'Title': { 'View': window.Something.JSLinkTemplates.DoStuff }
    };

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
})();

window.Something.JSLinkTemplates.DoStuff = function (ctx) {
        var title = ctx.CurrentItem.Title;

        var itemHTML = "<a href onclick='javascript:myCoolFunction(); return false;'>" + title + "</a>";
        return itemHtml;
    }
share|improve this answer
up vote 0 down vote accepted

Thank you for your suggestions. After a lot of fiddling around, trying to get Javascript to work for me. I've managed to solve this problem without it. A major problem with Javascript was the onclick event simply disappearing somewhere along the line. Although this new approach may seem a bit "quick and dirty", it works like it should and is quite fast and simple.

I've kept the SPFieldMultiLineText fields, and now insert hyperlinks into this which lead to the current URL, plus an artificially added part "?LabelFilter=" + Label name. Then, on Page_Load, the C# code checks if the HTTP request contains a query. From this point, filtering the list through C# was easy.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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