Our Blog > Blog Detail

Web Content Viewer- Asponte Custom Skin

Actions

WCM Tools Series: Using JSP to mimic the Apply Template functionality

  • Created By: Chris Knight
  • Updated: August 1, 2016
  • Tags: tools, jsp, wcm, custom

Introduction

There are times when you add a field to an authoring template, and you do no want to apply to All content that is using the authoring template.  Although it is possible to do this within the WCM UI, it is prohibitive to do it on more than 10 items or so at a time, especially if they are disbursed throughout the site.

 

Using this method below, it is possible to use the WCM Query API to find the content you want to add the field to, and then add the new field to the content.  This is slightly different than applying the template directly, however if the field name and type are the same as is in the authoring template, it will allow the content to have the field that matches what is in the authoring template.  This includes custom values about the field, like for example if you have a custom JSP assigned to a text element, that custom JSP property will be assigned to the content as well.  Basically WCM uses the name of the field, as well as the type, as the "lookup" to the authoring template to determine the custom fields.

 

For details on how to leverage custom JSPs from WCM, see the entry:

 

WCM Tools Series: Introduction to using custom JSPs

 

Expected Outcome

After running the code, the fields should be added to the content in question.

Full Code

<%@ page session="false" isELIgnored="false"%><%@ page
    import="java.util.*, java.text.*,java.io.*,java.lang.*,com.ibm.workplace.wcm.api.*, com.ibm.workplace.wcm.api.query.*,com.ibm.workplace.wcm.api.query.WorkflowSelectors.Status"%><%@ taglib
    uri="/WEB-INF/tld/wcm.tld" prefix="wcm"%><%!/*
     * declarations
     */

    static final boolean isDebugEnabled = false;%>     user="<%=request.getUserPrincipal()%>">login fail

<%!public static String getAuthoringURL(String host, int port,
            DocumentId theDoc, Workspace ws) {
        String previewURL = "";

        StringBuilder sb = new StringBuilder();
        String contextRoot = "/internet/myportal/us";

        sb.append(host);
        sb.append(":" + port);
        sb.append(contextRoot);

        sb.append("/wcmAuthoring?wcmAuthoringAction=read&docid=" + theDoc);
        previewURL = sb.toString();

        return previewURL;

    }%>

<%
    /*
     * Get context, content, workspace
     */
    try {
        // get all changed items since x
        int count = 0;
        int savedCount = 0;
        String host = request.getServerName();
        int port = request.getServerPort();
        RenderingContext rc = (RenderingContext) request
                .getAttribute(Workspace.WCM_RENDERINGCONTEXT_KEY);
        //Workspace ws = (Workspace) pageContext
        //    .getAttribute(Workspace.WCM_WORKSPACE_KEY);
        Workspace ws = WCM_API.getRepository().getSystemWorkspace();
        QueryService queryService = ws.getQueryService();
        Query theQuery = queryService.createQuery();

        theQuery.returnIds(); // get the query services to return DocumentId instead of Document objects
        int pageSize = 10;
        int startPage = 1;
    
        DocumentId at = ws
                .createDocumentId("UUID Of an authoring template");

        theQuery.addSelector(Selectors.authoringTemplateEquals(at));
        theQuery.addSort(Sorts.byDateModified(SortDirection.ASCENDING));
        System.out.println("before query");
        PageIterator

            while (currentPage.hasNext()) {
                try {

                    DocumentId contentId = (DocumentId) currentPage
                            .next();
                    // skip links
                    if (contentId.isOfType(DocumentTypes.Content)) {
                        Content theContent = (Content) ws
                                .getById(contentId);
                        // if we don't have the field already, add it

                        if (!theContent.hasComponent("TestTextField")) {
                            TextComponent dc = (TextComponent) theContent
                                    .createComponent("TestTextField",
                                            DocumentTypes.TextComponent);

                            theContent.setComponent("TestTextField", dc);
                            String[] errors = ws.save(theContent);
                            if (errors != null && errors.length < 1) {
                                out.println("Saved " + contentId
                                        + "
");
                                savedCount++;
                                count++;
                                
                                out.println(getAuthoringURL(host, port, contentId, ws) + ",");
                                out.println(contentId.getName() + ",");
                                out.println(idString);
                                out.println("
");
                                if (count > 20) {
                                    count = 0;
                                    ws.logout();
                                    ws.login();
                                }
                            } else {
                                out.println("error saving " + errors[0]
                                        + "
");
                            }
                        }
                    }

                } catch (Exception e) {
                    out.println("got an exception: " + e.getMessage());
                    //e.printStackTrace(response.getWriter());
                }
            }
        }
        out.println("changed count " + savedCount + "
");
    } catch (Exception e) {
        out.println("got an exception: " + e.getMessage());
    
    }
%>

 

Basically, the JSP uses the WCM Query API to retrieve in this case all of the content by a specific authoring template.  It then checks to see if the content has a field called "TestTextField".  If it does not, it adds the field and saves the content.  Note that you could set values in this field here if you wanted to set a default value.

Contact Form- Asponte Custom Skin

Actions

Contact Us

*
*


Submit
Call Us 888-926-9434
Complementary Content
${loading}