SharePoint 2010: How to display content using managed meta-tag with custom SharePoint user control in layout page

While creating content for SharePoint 2010 Anonymous Publishing site, I have encountered some issues that are related to how the information (list items and web pages) are organized and displayed. All site content are stored in various custom lists and html page libraries throughout the publishing site, and many of them need to be grouped in predefined categories. Interestingly, some content are classified in multiple categories. For example, I have a html page that is classified as a HR event and as a recent article (RECENT). This page should automatically be displayed under HR landing page as well as the main page's Recent Articles section. In fact, any newly created contents (pages or list items), regardless of where the content are created, should be displayed in every category they have been classified with.

At glance, the problem seems to be easily resolved since the SharePoint allows managed meta-tags field. In fact, the built-in Content Query WebPart (CQWP) supports managed  meta-tag search, but there are some problems with this approach to meet our requirements. First, I need a way to display contents from multiple sites' lists and page libraries, and although it is not confirmed, I think CQWP may not work with multi-value managed metadata field to filter query results from multiple lists/libraries. Obviously, the muti-value managed metadata field is needed to meet the requirement of grouping contents in different categories, or  in this case, of tagging the same content with different taxonomy terms (e.g., HR and RECENT). Second, the html markup of CQWP must be handled in ItemStyle.xsl (which is located under Style Libraries) using SharePoint Designer 2010 with appropriate privileges. Our front-end designers prefer a way to change the markup within SharePoint web interface so that he can add/update/remove html markups, ids and classes for styling the web-part zone (by the way, they are Mac users). Finally, the web-part also presents a problem if it is placed in layout page in order to display the same web-part in every page. Any newly created pages from the layout with web-part inherits the default web-part settings without any problem, but if the web-part is added (or modified) to the layout after creating pages with previous version, any previously created pages will not inherit web-part(s) added afterwards. Handling web-part change with a large number of pages becomes a maintenance nightmare although it is not possible (but not easy) to automate the process of touching every page after adding web-part or making changes in web-part default settings in the layout page.

Basically, I cannot use the CWQP or even any custom web-part to address this issue. What I came up with (to address this problem) was simply to create a SharePoint user control, instead of implementing a custom web part, that can be registered in layout page. It shifts the content management approach from a "per-page" based management to a "per-site" based management. The content will be repeated throughout the entire site for those pages that use the same layout (which is configured with this custom user control). This user control uses the current site custom library to find information on how to render contents. Each entry in this library contains fields to handle the custom full text query and markup of the content that is rendered with this control.

Although this post is not intended to explain how to build the SharePoint user control, I think it's worth the effort to reinforce the knowledge of building user control. Let's get started.

1. Open Visual Studio 2010 and create an empty SharePoint Project and set it as a farm solution.

2. Select your project in Solution Explorer window. Right-click on the project and then, you will be prompted with a context menu where you can choose Add->New Item option.

3. Select SharePoint 2010 User Control template and click okay. You will find your custom user control under ControlTemplates folder.

4. In .ascx file, simply add an asp:Literal control as shown below.



5. In .ascx.cs file, add the following codes in Page_Load function. It outputs the content to LiteralRenderedSection literal control.



6. Create a function called LoadFieldValues() and add the following codes. It retrieves SPListItem's columns and save them in class properties.



7. Create a function called "RenderOutputToText(). First, it prepares a html template with special placeholders which will be replaced with results from a full text query.







8. Continue with the following codes. This section of the function retrieves term tags and construct the full text query string.



9. Before continuing with the remaining section of this function, see the following example which shows a sample list entry that holds information for this custom user control. For example, {0} in full text query string will be replaced with the first term value.



10. The remaining piece of this function runs the query, retrieves search result and then, formats the result using a html template specified in EnclosedText and RepeatedText fields.






Here is the end result of this SharePoint user control after applying styles (css) to the html markup. I hope you had a great time reading this post








Comments

Popular Posts